Permalink
Browse files

exceptions - Updated RestxRouter template to :

- rethrow RuntimeExceptions
- rethrow IOException if underlying resource is likely to throw it
- wrap any other thrown exception into a new WrappedCheckedException (runtime exception), unboxed in StdRestxMainRouter to show its cause in the error message.
This fixes #121
  • Loading branch information...
fcamblor committed Aug 24, 2017
1 parent d777270 commit e61bcf2035ba51e3a8131a2b6ba8fe1fe609ca53
@@ -334,11 +334,11 @@ private void buildResourceRoutesCodeChunks(ResourceClass resourceClass, List<Imm
String call = "resource." + resourceMethod.name + "(\n" +
" " +
Joiner.on(",\n ").join(callParameters) + "\n" +
" )";
" )";
if (resourceMethod.returnType.equalsIgnoreCase("void")) {
call = call + ";\n" +
" return Optional.of(Empty.EMPTY);";
" return Optional.of(Empty.EMPTY);";
} else {
if (resourceMethod.returnTypeGuavaOptional) {
call = call ;
@@ -360,6 +360,7 @@ private void buildResourceRoutesCodeChunks(ResourceClass resourceClass, List<Imm
.put("resource", resourceClass.name)
.put("securityCheck", "securityManager.check(request, match, " + resourceMethod.permission + ");")
.put("queryParametersDefinition", Joiner.on(",\n").join(queryParametersDefinition))
.put("throwsIOException", resourceMethod.throwsIOException())
.put("call", call)
.put("responseClass", toTypeDescription(resourceMethod.returnType))
.put("sourceLocation", resourceMethod.sourceLocation)
@@ -12,6 +12,7 @@ import restx.*;
import restx.entity.*;
import restx.http.*;
import restx.endpoint.*;
import restx.exceptions.WrappedCheckedException;
import restx.factory.*;
import restx.security.*;
import restx.security.PermissionFactory;
@@ -52,7 +53,11 @@ public class {{router}} extends RestxRouter {
@Override
protected Optional<{{outEntity}}> doRoute(RestxRequest request, RestxRequestMatch match, {{inEntity}} body) throws IOException {
{{securityCheck}}
{{call}}
try {
{{call}}
} catch(RuntimeException e) { throw e; }
{{#throwsIOException}}catch(IOException e) { throw e; }
{{/throwsIOException}}catch(Exception e) { throw new WrappedCheckedException(e); }
}
@Override
@@ -13,6 +13,7 @@
import restx.common.metrics.api.Monitor;
import restx.common.metrics.dummy.DummyMetricRegistry;
import restx.exceptions.RestxError;
import restx.exceptions.WrappedCheckedException;
import restx.factory.Factory;
import restx.factory.NamedComponent;
import restx.http.HttpStatus;
@@ -219,6 +220,14 @@ public void route(RestxRequest restxRequest, final RestxResponse restxResponse)
PrintWriter out = restxResponse.getWriter();
out.println("UNEXPECTED CLIENT ERROR:");
out.print(ex.getMessage());
} catch (WrappedCheckedException wrappedException) {
Throwable ex = wrappedException.getCause();
logger.error("request raised " + ex.getClass().getSimpleName() + ": " + ex.getMessage(), ex);
restxResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
restxResponse.setContentType("text/plain");
PrintWriter out = restxResponse.getWriter();
out.println("UNEXPECTED SERVER ERROR:");
out.print(ex.getMessage());
} catch (Throwable ex) {
logger.error("request raised " + ex.getClass().getSimpleName() + ": " + ex.getMessage(), ex);
restxResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
@@ -0,0 +1,7 @@
package restx.exceptions;
public class WrappedCheckedException extends RuntimeException {
public WrappedCheckedException(Exception cause) {
super(cause);
}
}

0 comments on commit e61bcf2

Please sign in to comment.