Skip to content

Commit b2adda6

Browse files
committed
improve error handler docs
1 parent 888355a commit b2adda6

File tree

2 files changed

+48
-10
lines changed

2 files changed

+48
-10
lines changed

error-handler/index.md

+29-10
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,31 @@ function handleError(
2525
- `status` is integer value of HTTP code to send to client.
2626
- `msg` is string value of HTTP message to send to client
2727

28-
If you take a look at Fano Framework sample applications, e.g.,
29-
[Fano App](https://github.com/fanoframework/fano-app), you can observe
30-
that error handler instance is injected during application instance creation.
28+
Error handler are registered in application service provider `buildErrorHandler()` method [as shown below](#display-verbose-error-message-as-json).
3129

32-
The reason that error handler instance is injected directly in constructor and
33-
not relying on dependency container is to ensure that when you create application
34-
you must provide the error handler. It is mandatory. While when using
35-
dependency container, developer may forget to inject error handler instance.
30+
## Exceptions
31+
32+
Fano Framework defines some exception classes which corresponds to [HTTP error code](https://datatracker.ietf.org/doc/html/rfc7231).
33+
34+
- `EBadRequest`, HTTP 400 Bad Request.
35+
- `EUnauthorized`, HTTP 401 Unauthorized.
36+
- `EForbidden`, HTTP 403 Forbidden
37+
- `ENotFound`, HTTP 404 Not Found.
38+
- `EMethodNotAllowed`, HTTP 405 Method Not Allowed.
39+
- `ENotAcceptable`, HTTP 406 Not Acceptable.
40+
- `EGone`, HTTP 410 Gone.
41+
- `EUnsupportedMediaType`, HTTP 415 Unsupported Media Type.
42+
- `EUnprocessableEntity`, HTTP 422 Unprocessable Entity.
43+
- `ETooManyRequest`, HTTP 429 Too Many Request.
44+
- `EInternalServerError`, HTTP 500 Internal Server Error.
45+
- `ENotImplemented`, HTTP 501 Not Implemented.
46+
- `EBadGateway`, HTTP 502 Bad Gateway.
47+
- `EServiceUnavailable`, HTTP 503 Service Unavailable.
48+
- `EGatewayTimeout`, HTTP 504 Gateway Timeout.
49+
50+
If you raise any of execption above, Fano Framework returns its corresponding HTPP error as response. All exception classes above derived from `EHttpException` class.
51+
52+
Fano Framework also defines other exceptions not related to HTTP error code. Any of these exceptions will result in HTTP 500 error response except `ERouteHandlerNotFound` which result in HTTP 404 error.
3653

3754
## Built-in error handler implementation
3855

@@ -49,9 +66,11 @@ Fano Framework comes with several `IErrorHandler` implementation.
4966
- `TDecoratorErrorHandler` abstract error handler that is decorate other error handler.
5067
- `TConditionalErrorHandler` abstract error handler that is select one from two error handlers based on a condition. Descendant must implement its `condition()` abstract method.
5168
- `TBoolErrorHandler` error handler that is select one from two error handlers based on a condition specified in constructor parameter.
52-
- `TNotFoundErrorHandler` error handler that is select one from two error handlers based on a condition if the the exception is `ERouteHandlerNotFound`.
69+
- `TNotFoundErrorHandler` error handler that is select one from two error handlers based on a condition if the the exception is `ERouteHandlerNotFound`. This is provided so you can handle HTTP 404 error separately, for example, to display different HTML template for HTTP 404 error.
70+
- `TMethodNotAllowedErrorHandler` error handler that is select one from two error handlers based on a condition if the the exception is `EMethodNotAllowed`. This is provided so you can handle HTTP 405 error separately, for example, to use different HTML template for HTTP 405 error.
71+
- `TInternalServerErrorHandler` error handler that is select one from two error handlers based on a condition if the the exception is `EInternalServerError`. This is provided so you can handle HTTP 500 error separately, for example, to use different HTML template for HTTP 500 error.
5372

54-
## Display verbose error message
73+
## <a name="display-verbose-error-message"></a>Display verbose error message
5574

5675
`TFancyErrorHandler` and `TErrorHandler` are basic implementation of `IErrorHandler`, which
5776
display basic error information in HTML content type such as type of exception
@@ -73,7 +92,7 @@ type
7392

7493
To register different error handler type, you need to override `buildErrorHandler()` method.
7594

76-
## Display verbose error message as JSON
95+
## <a name="display-verbose-error-message-as-json">Display verbose error message as JSON
7796

7897
```
7998
type

middlewares/index.md

+19
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,26 @@ begin
144144
result := next.handleRequest(request, response, args);
145145
end;
146146
end;
147+
```
148+
149+
You can also raise an exception to stop execution. Fano Framework will handle exception and call apropriate [error handler](/error-handler).
147150

151+
```
152+
function TAjaxOnlyMiddleware.handleRequest(
153+
const request : IRequest;
154+
const response : IResponse;
155+
const args : IRouteArgsReader;
156+
const next : IRequestHandler
157+
) : IResponse;
158+
begin
159+
if (not request.isXhr()) then
160+
begin
161+
raise EForbidden.create('Not Ajax Request');
162+
end else
163+
begin
164+
result := next.handleRequest(request, response, args);
165+
end;
166+
end;
148167
```
149168

150169
## Type of middlewares

0 commit comments

Comments
 (0)