Search in sources :

Example 1 with SparkShellSaveException

use of com.thinkbiganalytics.spark.shell.SparkShellSaveException in project kylo by Teradata.

the class SparkShellProxyController method saveError.

/**
 * Generates an error response for a failed save.
 *
 * <p>Example:
 * <code>
 * throw saveError(Response.Status.BAD_REQUEST, "save.error", e);
 * </code></p>
 *
 * @param status HTTP response status
 * @param key    resource key or error message
 * @param cause  the cause
 * @return the error response
 */
@Nonnull
private WebApplicationException saveError(@Nonnull final Response.Status status, @Nonnull final String key, @Nullable final Throwable cause) {
    // Create entity
    final SaveResponse entity = new SaveResponse();
    entity.setId(cause instanceof SparkShellSaveException ? ((SparkShellSaveException) cause).getId() : null);
    entity.setStatus(SaveResponse.Status.ERROR);
    try {
        entity.setMessage(STRINGS.getString(key));
    } catch (final MissingResourceException e) {
        log.warn("Missing resource message: {}", key, e);
        entity.setMessage(key);
    }
    // Generate the response
    final Response response = Response.status(status).entity(entity).header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).build();
    if (cause != null) {
        return new WebApplicationException(cause, response);
    } else {
        return new WebApplicationException(response);
    }
}
Also used : Response(javax.ws.rs.core.Response) SaveResponse(com.thinkbiganalytics.spark.rest.model.SaveResponse) TransformResponse(com.thinkbiganalytics.spark.rest.model.TransformResponse) ApiResponse(io.swagger.annotations.ApiResponse) WebApplicationException(javax.ws.rs.WebApplicationException) SaveResponse(com.thinkbiganalytics.spark.rest.model.SaveResponse) MissingResourceException(java.util.MissingResourceException) SparkShellSaveException(com.thinkbiganalytics.spark.shell.SparkShellSaveException) Nonnull(javax.annotation.Nonnull)

Example 2 with SparkShellSaveException

use of com.thinkbiganalytics.spark.shell.SparkShellSaveException in project kylo by Teradata.

the class SparkShellProxyController method getSaveResponse.

/**
 * Gets the save response from the specified supplier.
 */
@Nonnull
private Response getSaveResponse(@Nonnull final Supplier<Optional<SaveResponse>> supplier) {
    // Get the result
    final Optional<SaveResponse> response;
    try {
        response = supplier.get();
    } catch (final SparkShellSaveException e) {
        final String message = (e.getMessage() != null) ? EXCEPTION.matcher(e.getMessage()).replaceAll("") : SparkShellProxyResources.SAVE_ERROR;
        throw saveError(Response.Status.INTERNAL_SERVER_ERROR, message, e);
    } catch (final Exception e) {
        throw saveError(Response.Status.INTERNAL_SERVER_ERROR, SparkShellProxyResources.SAVE_ERROR, e);
    }
    // Return response
    final SaveResponse saveResponse = response.orElseThrow(() -> transformError(Response.Status.NOT_FOUND, SparkShellProxyResources.SAVE_NOT_FOUND, null));
    return Response.ok(saveResponse).build();
}
Also used : SaveResponse(com.thinkbiganalytics.spark.rest.model.SaveResponse) SparkShellSaveException(com.thinkbiganalytics.spark.shell.SparkShellSaveException) BadRequestException(javax.ws.rs.BadRequestException) MissingResourceException(java.util.MissingResourceException) WebApplicationException(javax.ws.rs.WebApplicationException) SparkShellSaveException(com.thinkbiganalytics.spark.shell.SparkShellSaveException) SparkShellTransformException(com.thinkbiganalytics.spark.shell.SparkShellTransformException) Nonnull(javax.annotation.Nonnull)

Aggregations

SaveResponse (com.thinkbiganalytics.spark.rest.model.SaveResponse)2 SparkShellSaveException (com.thinkbiganalytics.spark.shell.SparkShellSaveException)2 MissingResourceException (java.util.MissingResourceException)2 Nonnull (javax.annotation.Nonnull)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 TransformResponse (com.thinkbiganalytics.spark.rest.model.TransformResponse)1 SparkShellTransformException (com.thinkbiganalytics.spark.shell.SparkShellTransformException)1 ApiResponse (io.swagger.annotations.ApiResponse)1 BadRequestException (javax.ws.rs.BadRequestException)1 Response (javax.ws.rs.core.Response)1