use of bio.terra.workspace.service.job.exception.ExceptionSerializerException in project terra-workspace-manager by DataBiosphere.
the class StairwayExceptionSerializer method serialize.
@Override
public String serialize(Exception rawException) {
if (rawException == null) {
return StringUtils.EMPTY;
}
Exception exception = rawException;
// Wrap non-runtime exceptions so they can be rethrown later
if (!(exception instanceof RuntimeException)) {
exception = new JobResponseException(exception.getMessage(), exception);
}
StairwayExceptionFields fields = new StairwayExceptionFields().setClassName(exception.getClass().getName()).setMessage(exception.getMessage());
if (exception instanceof ErrorReportException) {
fields.setApiErrorReportException(true).setErrorDetails(((ErrorReportException) exception).getCauses()).setErrorCode(((ErrorReportException) exception).getStatusCode().value());
} else {
fields.setApiErrorReportException(false);
}
try {
return objectMapper.writeValueAsString(fields);
} catch (JsonProcessingException ex) {
// JSON processing to fail.
throw new ExceptionSerializerException("This should never happen", ex);
}
}
use of bio.terra.workspace.service.job.exception.ExceptionSerializerException in project terra-workspace-manager by DataBiosphere.
the class StairwayExceptionSerializer method deserialize.
@Override
public Exception deserialize(String serializedException) {
if (StringUtils.isEmpty(serializedException)) {
return null;
}
// Decode the exception fields from JSON
StairwayExceptionFields fields;
try {
fields = objectMapper.readValue(serializedException, StairwayExceptionFields.class);
} catch (IOException ex) {
// objectMapper exceptions
return new ExceptionSerializerException("Failed to deserialize exception data: " + serializedException, ex);
}
// Find the class from the class name
Class<?> clazz;
try {
clazz = Class.forName(fields.getClassName());
} catch (ClassNotFoundException ex) {
return new ExceptionSerializerException("Exception class not found: " + fields.getClassName() + "; Exception message: " + fields.getMessage());
}
// status code.
if (fields.isApiErrorReportException()) {
try {
Constructor<?> ctor = clazz.getConstructor(String.class, List.class, HttpStatus.class);
Object object = ctor.newInstance(fields.getMessage(), fields.getErrorDetails(), HttpStatus.valueOf(fields.getErrorCode()));
return (Exception) object;
} catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
// We didn't find a constructor with error these error details or construction failed.
}
}
// try again with another common pattern of message + causes.
if (fields.isApiErrorReportException()) {
try {
Constructor<?> ctor = clazz.getConstructor(String.class, List.class);
Object object = ctor.newInstance(fields.getMessage(), fields.getErrorDetails());
return (Exception) object;
} catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
// We didn't find a constructor with error details or construction failed. Fall through
}
}
// runtime exception
try {
Constructor<?> ctor = clazz.getConstructor(String.class);
Object object = ctor.newInstance(fields.getMessage());
return (Exception) object;
} catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
// We didn't find a vanilla constructor or construction failed. Fall through
}
return new ExceptionSerializerException("Failed to construct exception: " + fields.getClassName() + "; Exception message: " + fields.getMessage());
}
Aggregations