Search in sources :

Example 1 with ExceptionSerializerException

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);
    }
}
Also used : ErrorReportException(bio.terra.common.exception.ErrorReportException) ExceptionSerializerException(bio.terra.workspace.service.job.exception.ExceptionSerializerException) JobResponseException(bio.terra.workspace.service.job.exception.JobResponseException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) JobResponseException(bio.terra.workspace.service.job.exception.JobResponseException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) ExceptionSerializerException(bio.terra.workspace.service.job.exception.ExceptionSerializerException) ErrorReportException(bio.terra.common.exception.ErrorReportException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with ExceptionSerializerException

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());
}
Also used : IOException(java.io.IOException) JobResponseException(bio.terra.workspace.service.job.exception.JobResponseException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) ExceptionSerializerException(bio.terra.workspace.service.job.exception.ExceptionSerializerException) ErrorReportException(bio.terra.common.exception.ErrorReportException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ExceptionSerializerException(bio.terra.workspace.service.job.exception.ExceptionSerializerException)

Aggregations

ErrorReportException (bio.terra.common.exception.ErrorReportException)2 ExceptionSerializerException (bio.terra.workspace.service.job.exception.ExceptionSerializerException)2 JobResponseException (bio.terra.workspace.service.job.exception.JobResponseException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 IOException (java.io.IOException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2