Skip to content

Commit d30c7f1

Browse files
committed
Improve "Error handling" section
1 parent 0cf13d0 commit d30c7f1

File tree

1 file changed

+72
-2
lines changed

1 file changed

+72
-2
lines changed

_posts/2015-03-27-error-handling.md

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ Otherwise ErrorHandler will fallback to itself to handle the exception.
1313
By default each Application instance comes with a [DefaultErrorHandler]({{ site.coreurl }}/src/main/java/ro/pippo/core/DefaultErrorHandler.java)
1414
that integrates with the `TemplateEngine` & `ContentTypeEngine`. It generates a representation of an exception or error result.
1515

16-
1716
See below an custom ErrorHandler in action:
1817
```java
1918
public class BasicApplication extends Application {
@@ -54,4 +53,75 @@ public class BasicApplication extends Application {
5453
}
5554
```
5655

57-
If you want to customize the template content for the default error templates (see the list of templates names in [TemplateEngine]({{ site.coreurl }}/src/main/java/ro/pippo/core/TemplateEngine.java)) then create a template with the same name in `src/main/resources/templates` folder.
56+
If you want to customize the template content for the default error templates (see the list of templates names in [TemplateEngine]({{ site.coreurl }}/src/main/java/ro/pippo/core/TemplateEngine.java)) then create a template with the same name in `src/main/resources/templates` folder.
57+
58+
So, by default, the following templates are available (copy/paste from `TemplateEngine.java`):
59+
60+
```java
61+
public final static String BAD_REQUEST_400 = "pippo/400badRequest";
62+
public final static String UNAUTHORIZED_401 = "pippo/401unauthorized";
63+
public final static String PAYMENT_REQUIRED_402 = "pippo/402paymentRequired";
64+
public final static String FORBIDDEN_403 = "pippo/403forbidden";
65+
public final static String NOT_FOUND_404 = "pippo/404notFound";
66+
public final static String METHOD_NOT_ALLOWED_405 = "pippo/405methodNotAllowed";
67+
public final static String CONFLICT_409 = "pippo/409conflict";
68+
public final static String GONE_410 = "pippo/410gone";
69+
public final static String INTERNAL_ERROR_500 = "pippo/500internalError";
70+
public final static String NOT_IMPLEMENTED_501 = "pippo/501notImplemented";
71+
public final static String OVERLOADED_502 = "pippo/502overloaded";
72+
public final static String SERVICE_UNAVAILABLE_503 = "pippo/503serviceUnavailable";
73+
```
74+
75+
Each template engine implementation comes with above templates. For example if we take a look at `pippo-template-parent/pippo-freemarker/src/main/resources` we can see:
76+
```
77+
$ tree .
78+
.
79+
└── templates
80+
└── pippo
81+
├── 000base.ftl
82+
├── 400badRequest.ftl
83+
├── 401unauthorized.ftl
84+
├── 402paymentRequired.ftl
85+
├── 403forbidden.ftl
86+
├── 404notFound.ftl
87+
├── 405methodNotAllowed.ftl
88+
├── 409conflict.ftl
89+
├── 410gone.ftl
90+
├── 500internalError.ftl
91+
├── 501notImplemented.ftl
92+
├── 502overloaded.ftl
93+
└── 503serviceUnavailable.ftl
94+
```
95+
96+
If you use `FreemarkerTemplateEngine` in your application and you want to replace/override the content of `404notFound.ftl` template,
97+
then place your variant of `404notFound.ftl` in your `src/main/resources/templates/pippo` directory.
98+
99+
If you are not happy with this fine granularity (a template for each HTTP error code), then you can create your custom `ErrorHandler` that returns the same template for any status code:
100+
```java
101+
public class MyErrorHandler extends DefaultErrorHandler {
102+
103+
public ExampleErrorHandler(Application application) {
104+
super(application);
105+
}
106+
107+
@Override
108+
protected String getTemplateForStatusCode(int statusCode) {
109+
return "error.ftl"; // or simple "error" without extension
110+
}
111+
112+
}
113+
```
114+
115+
and use your custom error handler in your application:
116+
```java
117+
public class MyApplication extends Application {
118+
119+
@Override
120+
protected void onInit() {
121+
setErrorHandler(new MyErrorHandler(this));
122+
123+
// add routes below
124+
}
125+
126+
}
127+
```

0 commit comments

Comments
 (0)