Skip to content

Commit

Permalink
feat: Handling pluginNot found Issue
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Sep 26, 2023
1 parent 0a6f263 commit a48ff02
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.example.plugin.strategyplugin.exception;

import org.springframework.http.HttpStatus;
import org.springframework.http.ProblemDetail;
import org.springframework.web.ErrorResponseException;

import java.net.URI;
import java.time.Instant;

public class PluginNotFoundException extends ErrorResponseException {
public PluginNotFoundException(String message) {
super(
HttpStatus.BAD_REQUEST,
asProblemDetail(message),
null);
}

private static ProblemDetail asProblemDetail(String message) {
ProblemDetail problemDetail =
ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, message);
problemDetail.setTitle("Product Not Found");
problemDetail.setType(URI.create("https://api.service.com/errors/bad-request"));
problemDetail.setProperty("errorCategory", "Generic");
problemDetail.setProperty("timestamp", Instant.now());
return problemDetail;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.example.plugin.strategyplugin.service;

import com.example.plugin.strategyplugin.domain.GenericDTO;
import com.example.plugin.strategyplugin.exception.PluginNotFoundException;
import com.example.plugin.strategyplugin.plugin.WriterPlugin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.plugin.core.PluginRegistry;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class StrategyService {

Expand All @@ -20,6 +23,9 @@ public StrategyService(PluginRegistry<WriterPlugin, String> plugins) {

public GenericDTO fetchData(String type) {
logger.info("fetching data for type :{}", type);
return plugins.getPluginFor(type).get().write("Hello ");
return plugins.getPluginFor(type)
.orElseThrow(() -> new PluginNotFoundException("Plugin not found for type: " + type))
.write("Hello ");
}

}

0 comments on commit a48ff02

Please sign in to comment.