Search in sources :

Example 11 with BadRequestException

use of com.google.api.server.spi.response.BadRequestException in project endpoints-java by cloudendpoints.

the class SystemService method invokeServiceMethod.

/**
 * Invokes a {@code method} on a {@code service} given a {@code paramReader} to read parameters
 * and a {@code resultWriter} to write result.
 */
public void invokeServiceMethod(Object service, Method method, ParamReader paramReader, ResultWriter resultWriter) throws IOException {
    try {
        Object[] params = paramReader.read();
        logger.atFine().log("params=%s (String)", Arrays.toString(params));
        Object response = method.invoke(service, params);
        resultWriter.write(response);
    } catch (IllegalArgumentException | IllegalAccessException e) {
        logger.atSevere().withCause(e).log("exception occurred while calling backend method");
        resultWriter.writeError(new BadRequestException(e));
    } catch (InvocationTargetException e) {
        Throwable cause = e.getCause();
        Level level = Level.INFO;
        if (cause instanceof ServiceException) {
            resultWriter.writeError((ServiceException) cause);
        } else if (cause instanceof IllegalArgumentException) {
            resultWriter.writeError(isIllegalArgumentBackendError ? new InternalServerErrorException(cause) : new BadRequestException(cause));
        } else if (isOAuthRequestException(cause.getClass())) {
            resultWriter.writeError(new UnauthorizedException(cause));
        } else if (cause.getCause() != null && cause.getCause() instanceof ServiceException) {
            ServiceException serviceException = (ServiceException) cause.getCause();
            level = serviceException.getLogLevel();
            resultWriter.writeError(serviceException);
        } else {
            level = Level.SEVERE;
            resultWriter.writeError(new InternalServerErrorException(cause));
        }
        logger.at(level).withCause(cause).log("exception occurred while calling backend method");
    } catch (ServiceException e) {
        logger.at(e.getLogLevel()).withCause(e).log("exception occurred while calling backend method");
        resultWriter.writeError(e);
    }
}
Also used : UnauthorizedException(com.google.api.server.spi.response.UnauthorizedException) BadRequestException(com.google.api.server.spi.response.BadRequestException) InternalServerErrorException(com.google.api.server.spi.response.InternalServerErrorException) Level(java.util.logging.Level) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

BadRequestException (com.google.api.server.spi.response.BadRequestException)11 ApiMethod (com.google.api.server.spi.config.ApiMethod)7 NotFoundException (com.google.api.server.spi.response.NotFoundException)5 UnauthorizedException (com.google.api.server.spi.response.UnauthorizedException)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 EndpointMethod (com.google.api.server.spi.EndpointMethod)2 Gson (com.google.gson.Gson)2 IOException (java.io.IOException)2 Date (java.util.Date)2 CryptonomicaUser (net.cryptonomica.entities.CryptonomicaUser)2 Verification (net.cryptonomica.entities.Verification)2 StringWrapperObject (net.cryptonomica.returns.StringWrapperObject)2 VerificationGeneralView (net.cryptonomica.returns.VerificationGeneralView)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 User (com.google.api.server.spi.auth.common.User)1 ApiParameterConfig (com.google.api.server.spi.config.model.ApiParameterConfig)1 ForbiddenException (com.google.api.server.spi.response.ForbiddenException)1 InternalServerErrorException (com.google.api.server.spi.response.InternalServerErrorException)1