Skip to content

Commit

Permalink
[feature]add protected method for extract exception in error decoder …
Browse files Browse the repository at this point in the history
…to override in customErrorDecoder
  • Loading branch information
A.Alimohammadi authored and A.Alimohammadi committed Aug 21, 2023
1 parent f0f72e0 commit eace5b0
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public CustomErrorDecoder(CustomErrorDecoderConfig customErrorDecoderConfig) {
this.customErrorDecoderConfig = customErrorDecoderConfig;
}

public ObjectMapper getObjectMapper() {
return objectMapper;
}

@Override
public Exception decode(String methodKey, Response response) {
try {
Expand All @@ -48,51 +52,57 @@ public Exception decode(String methodKey, Response response) {
responseBody);
Map<String, Class<? extends Exception>> exceptionMap = customErrorDecoderConfig.getExceptionMap();
if (status >= 400 && status < 500) {
ErrorObject errorObject = jsonToObject(responseBody, ErrorObject.class);
String errorType = errorObject.getErrorType();
String errorCode = errorObject.getErrorCode();
String errorKey = errorType + "." + errorCode;
Class<? extends Exception> exceptionClass = exceptionMap.get(errorKey);
if (exceptionClass != null) {
return jsonToObject(responseBody, exceptionClass);
} else {
UnknownException unknownException = jsonToObject(responseBody, UnknownException.class);
unknownException.setJsonResponse(responseBody);
return unknownException;
}
}
InternalServerException internalServerException = jsonToObject(responseBody, InternalServerException.class);
if (internalServerException.getErrorParam() == null) {
Map<String, Object> errorMap = new HashMap<>();
internalServerException.setErrorParam(errorMap);
return extractBadRequestErrorException(responseBody, exceptionMap);
}
String errorType = internalServerException.getErrorType();
String errorCode = internalServerException.getErrorCode();
String errorKey = errorType + "." + errorCode;
Class<? extends Exception> cause = exceptionMap.get(errorKey);
internalServerException.getErrorParam().put("httpStatusCode", status);
internalServerException.getErrorParam().put("cause", cause);
internalServerException.setJsonResponse(responseBody);
return internalServerException;
return extractInternalServerErrorException(responseBody, status, exceptionMap);
} catch (Exception e) {
LOGGER.warn("ServerInternalRuntimeException", e);
InternalServerException internalServerException = new InternalServerException("Internal error", e);
internalServerException.setErrorType("customErrorDecoder");
internalServerException.setErrorCode(e.getClass().getSimpleName());
Map<String, Object> exceptionErrorMap = new HashMap<>();
exceptionErrorMap.put("localizedMessage", e.getLocalizedMessage());
internalServerException.setErrorParam(exceptionErrorMap);
internalServerException.setMessage(e.getMessage());
return internalServerException;
return extractCustomErrorDecoderException(e);
}
}

private <T> T jsonToObject(String string, Class<T> type) {
try {
return objectMapper.readValue(string, type);
} catch (Exception e) {
throw new JsonConvertException("error in converting Json to object");
protected Exception extractBadRequestErrorException(String responseBody,
Map<String, Class<? extends Exception>> exceptionMap) {
ErrorObject errorObject = jsonToObject(responseBody, ErrorObject.class);
String errorType = errorObject.getErrorType();
String errorCode = errorObject.getErrorCode();
String errorKey = errorType + "." + errorCode;
Class<? extends Exception> exceptionClass = exceptionMap.get(errorKey);
if (exceptionClass != null) {
return jsonToObject(responseBody, exceptionClass);
} else {
UnknownException unknownException = jsonToObject(responseBody, UnknownException.class);
unknownException.setJsonResponse(responseBody);
return unknownException;
}
}

protected Exception extractCustomErrorDecoderException(Exception e) {
LOGGER.warn("ServerInternalRuntimeException", e);
InternalServerException internalServerException = new InternalServerException("Internal error", e);
internalServerException.setErrorType("customErrorDecoder");
internalServerException.setErrorCode(e.getClass().getSimpleName());
Map<String, Object> exceptionErrorMap = new HashMap<>();
exceptionErrorMap.put("localizedMessage", e.getLocalizedMessage());
internalServerException.setErrorParam(exceptionErrorMap);
internalServerException.setMessage(e.getMessage());
return internalServerException;
}

protected Exception extractInternalServerErrorException(String responseBody, int status,
Map<String, Class<? extends Exception>> exceptionMap) {
InternalServerException internalServerException = jsonToObject(responseBody, InternalServerException.class);
if (internalServerException.getErrorParam() == null) {
Map<String, Object> errorMap = new HashMap<>();
internalServerException.setErrorParam(errorMap);
}
String errorType = internalServerException.getErrorType();
String errorCode = internalServerException.getErrorCode();
String errorKey = errorType + "." + errorCode;
Class<? extends Exception> cause = exceptionMap.get(errorKey);
internalServerException.getErrorParam().put("httpStatusCode", status);
internalServerException.getErrorParam().put("cause", cause);
internalServerException.setJsonResponse(responseBody);
return internalServerException;
}

@Override
Expand Down Expand Up @@ -131,6 +141,14 @@ public void afterPropertiesSet() {
}
}

protected <T> T jsonToObject(String string, Class<T> type) {
try {
return objectMapper.readValue(string, type);
} catch (Exception e) {
throw new JsonConvertException("error in converting Json to object");
}
}

private ObjectMapper getDefaultObjectMapper() {
ObjectMapper defaultObjectMapper = new ObjectMapper();
defaultObjectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public Map<String, Object> getErrorParam() {
return errorParam;
}

public void setErrorParam(Map<String, Object> errorParam) {
this.errorParam = errorParam;
}

public TosanWebServiceException addErrorParam(String key, Object value) {
if (errorParam == null) {
errorParam = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public Map<String, Object> getErrorParam() {
return errorParam;
}

public void setErrorParam(Map<String, Object> errorParam) {
this.errorParam = errorParam;
}
public TosanWebServiceRuntimeException addErrorParam(String key, Object value) {
if (errorParam == null) {
errorParam = new HashMap<>();
Expand Down

0 comments on commit eace5b0

Please sign in to comment.