Skip to content

Commit e61bcf2

Browse files
committed
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
1 parent d777270 commit e61bcf2

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

restx-core-annotation-processor/src/main/java/restx/annotations/processor/RestxAnnotationProcessor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,11 +334,11 @@ private void buildResourceRoutesCodeChunks(ResourceClass resourceClass, List<Imm
334334
String call = "resource." + resourceMethod.name + "(\n" +
335335
" " +
336336
Joiner.on(",\n ").join(callParameters) + "\n" +
337-
" )";
337+
" )";
338338

339339
if (resourceMethod.returnType.equalsIgnoreCase("void")) {
340340
call = call + ";\n" +
341-
" return Optional.of(Empty.EMPTY);";
341+
" return Optional.of(Empty.EMPTY);";
342342
} else {
343343
if (resourceMethod.returnTypeGuavaOptional) {
344344
call = call ;
@@ -360,6 +360,7 @@ private void buildResourceRoutesCodeChunks(ResourceClass resourceClass, List<Imm
360360
.put("resource", resourceClass.name)
361361
.put("securityCheck", "securityManager.check(request, match, " + resourceMethod.permission + ");")
362362
.put("queryParametersDefinition", Joiner.on(",\n").join(queryParametersDefinition))
363+
.put("throwsIOException", resourceMethod.throwsIOException())
363364
.put("call", call)
364365
.put("responseClass", toTypeDescription(resourceMethod.returnType))
365366
.put("sourceLocation", resourceMethod.sourceLocation)

restx-core-annotation-processor/src/main/resources/restx/annotations/processor/RestxRouter.mustache

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import restx.*;
1212
import restx.entity.*;
1313
import restx.http.*;
1414
import restx.endpoint.*;
15+
import restx.exceptions.WrappedCheckedException;
1516
import restx.factory.*;
1617
import restx.security.*;
1718
import restx.security.PermissionFactory;
@@ -52,7 +53,11 @@ public class {{router}} extends RestxRouter {
5253
@Override
5354
protected Optional<{{outEntity}}> doRoute(RestxRequest request, RestxRequestMatch match, {{inEntity}} body) throws IOException {
5455
{{securityCheck}}
55-
{{call}}
56+
try {
57+
{{call}}
58+
} catch(RuntimeException e) { throw e; }
59+
{{#throwsIOException}}catch(IOException e) { throw e; }
60+
{{/throwsIOException}}catch(Exception e) { throw new WrappedCheckedException(e); }
5661
}
5762

5863
@Override

restx-core/src/main/java/restx/StdRestxMainRouter.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import restx.common.metrics.api.Monitor;
1414
import restx.common.metrics.dummy.DummyMetricRegistry;
1515
import restx.exceptions.RestxError;
16+
import restx.exceptions.WrappedCheckedException;
1617
import restx.factory.Factory;
1718
import restx.factory.NamedComponent;
1819
import restx.http.HttpStatus;
@@ -219,6 +220,14 @@ public void route(RestxRequest restxRequest, final RestxResponse restxResponse)
219220
PrintWriter out = restxResponse.getWriter();
220221
out.println("UNEXPECTED CLIENT ERROR:");
221222
out.print(ex.getMessage());
223+
} catch (WrappedCheckedException wrappedException) {
224+
Throwable ex = wrappedException.getCause();
225+
logger.error("request raised " + ex.getClass().getSimpleName() + ": " + ex.getMessage(), ex);
226+
restxResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
227+
restxResponse.setContentType("text/plain");
228+
PrintWriter out = restxResponse.getWriter();
229+
out.println("UNEXPECTED SERVER ERROR:");
230+
out.print(ex.getMessage());
222231
} catch (Throwable ex) {
223232
logger.error("request raised " + ex.getClass().getSimpleName() + ": " + ex.getMessage(), ex);
224233
restxResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package restx.exceptions;
2+
3+
public class WrappedCheckedException extends RuntimeException {
4+
public WrappedCheckedException(Exception cause) {
5+
super(cause);
6+
}
7+
}

0 commit comments

Comments
 (0)