Search in sources :

Example 1 with ApiErrorReport

use of bio.terra.workspace.generated.model.ApiErrorReport in project terra-workspace-manager by DataBiosphere.

the class FlightUtils method setErrorResponse.

/**
 * Build an error model and set it as the response
 *
 * @param context flight context
 * @param message error message
 * @param responseStatus status
 */
public static void setErrorResponse(FlightContext context, String message, HttpStatus responseStatus) {
    ApiErrorReport errorModel = new ApiErrorReport().message(message);
    setResponse(context, errorModel, responseStatus);
}
Also used : ApiErrorReport(bio.terra.workspace.generated.model.ApiErrorReport)

Example 2 with ApiErrorReport

use of bio.terra.workspace.generated.model.ApiErrorReport in project terra-workspace-manager by DataBiosphere.

the class GlobalExceptionHandler method validationExceptionHandler.

// -- validation exceptions - we don't control the exception raised
@ExceptionHandler({ MethodArgumentNotValidException.class, MethodArgumentTypeMismatchException.class, HttpMessageNotReadableException.class, IllegalArgumentException.class, NoHandlerFoundException.class })
public ResponseEntity<ApiErrorReport> validationExceptionHandler(Exception ex) {
    logger.error("Global exception handler: catch stack", ex);
    // For security reasons, we generally don't want to include the user's invalid (and potentially
    // malicious) input in the error response, which also means we don't include the full exception.
    // Instead, we return a generic error message about input validation.
    String validationErrorMessage = "Request could not be parsed or was invalid: " + ex.getClass().getSimpleName() + ". Ensure that all types are correct and that enums have valid values.";
    ApiErrorReport errorReport = new ApiErrorReport().message(validationErrorMessage).statusCode(HttpStatus.BAD_REQUEST.value());
    return new ResponseEntity<>(errorReport, HttpStatus.BAD_REQUEST);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) ApiErrorReport(bio.terra.workspace.generated.model.ApiErrorReport) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 3 with ApiErrorReport

use of bio.terra.workspace.generated.model.ApiErrorReport in project terra-workspace-manager by DataBiosphere.

the class GlobalExceptionHandler method buildApiErrorReport.

private ResponseEntity<ApiErrorReport> buildApiErrorReport(@NotNull Throwable ex, HttpStatus statusCode, List<String> causes) {
    StringBuilder combinedCauseString = new StringBuilder();
    for (Throwable cause = ex; cause != null; cause = cause.getCause()) {
        combinedCauseString.append("cause: ").append(cause.toString()).append(", ");
    }
    logger.error("Global exception handler: " + combinedCauseString.toString(), ex);
    ApiErrorReport errorReport = new ApiErrorReport().message(ex.getMessage()).statusCode(statusCode.value()).causes(causes);
    return new ResponseEntity<>(errorReport, statusCode);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) ApiErrorReport(bio.terra.workspace.generated.model.ApiErrorReport)

Example 4 with ApiErrorReport

use of bio.terra.workspace.generated.model.ApiErrorReport in project terra-workspace-manager by DataBiosphere.

the class GlobalExceptionHandler method retryBackoffExceptionHandler.

// Exception thrown by Spring Retry code when interrupted.
@ExceptionHandler({ BackOffInterruptedException.class })
public ResponseEntity<ApiErrorReport> retryBackoffExceptionHandler(BackOffInterruptedException ex) {
    String errorMessage = "Unexpected interrupt while retrying database logic. This may succeed on a retry. " + ex.getMessage();
    ApiErrorReport errorReport = new ApiErrorReport().message(errorMessage).statusCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
    return new ResponseEntity<>(errorReport, HttpStatus.INTERNAL_SERVER_ERROR);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) ApiErrorReport(bio.terra.workspace.generated.model.ApiErrorReport) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 5 with ApiErrorReport

use of bio.terra.workspace.generated.model.ApiErrorReport in project terra-workspace-manager by DataBiosphere.

the class JobService method retrieveAsyncJobResult.

/**
 * Retrieves the result of an asynchronous job.
 *
 * <p>Stairway has no concept of synchronous vs asynchronous flights. However, MC Terra has a
 * service-level standard result for asynchronous jobs which includes a ApiJobReport and either a
 * result or error if the job is complete. This is a convenience for callers who would otherwise
 * need to construct their own AsyncJobResult object.
 *
 * <p>Unlike retrieveJobResult, this will not throw for a flight in progress. Instead, it will
 * return a ApiJobReport without a result or error.
 */
public <T> AsyncJobResult<T> retrieveAsyncJobResult(String jobId, Class<T> resultClass, AuthenticatedUserRequest userRequest) {
    try {
        // jobId=flightId
        verifyUserAccess(jobId, userRequest);
        ApiJobReport jobReport = retrieveJob(jobId, userRequest);
        if (jobReport.getStatus().equals(StatusEnum.RUNNING)) {
            return new AsyncJobResult<T>().jobReport(jobReport);
        }
        JobResultOrException<T> resultOrException = retrieveJobResultWorker(jobId, resultClass);
        final ApiErrorReport errorReport;
        if (jobReport.getStatus().equals(StatusEnum.FAILED)) {
            errorReport = ErrorReportUtils.buildApiErrorReport(resultOrException.getException());
        } else {
            errorReport = null;
        }
        return new AsyncJobResult<T>().jobReport(jobReport).result(resultOrException.getResult()).errorReport(errorReport);
    } catch (StairwayException | InterruptedException stairwayEx) {
        throw new InternalStairwayException(stairwayEx);
    }
}
Also used : ApiJobReport(bio.terra.workspace.generated.model.ApiJobReport) ApiErrorReport(bio.terra.workspace.generated.model.ApiErrorReport) StairwayException(bio.terra.stairway.exception.StairwayException) InternalStairwayException(bio.terra.workspace.service.job.exception.InternalStairwayException) InternalStairwayException(bio.terra.workspace.service.job.exception.InternalStairwayException)

Aggregations

ApiErrorReport (bio.terra.workspace.generated.model.ApiErrorReport)5 ResponseEntity (org.springframework.http.ResponseEntity)3 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)2 StairwayException (bio.terra.stairway.exception.StairwayException)1 ApiJobReport (bio.terra.workspace.generated.model.ApiJobReport)1 InternalStairwayException (bio.terra.workspace.service.job.exception.InternalStairwayException)1