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);
}
}
Aggregations