Skip to content

Commit 68342e2

Browse files
committed
Update readme
1 parent 390ba77 commit 68342e2

File tree

1 file changed

+15
-25
lines changed

1 file changed

+15
-25
lines changed

README.md

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ Send a 200 response with the data and meta object.
2020

2121
Can be disabled by setting the `app.disableReplyHelperFunctions` to `true`.
2222

23-
`res.ok({ok: 'It works!'}, {addition: 'someValue'})`
23+
`res.ok({baz: 'It works!'}, {foo: 'bar'})`
2424

2525
```json
2626
{
2727
"status": "ok",
2828
"data": {
29-
"ok": "It works!"
29+
"baz": "It works!"
3030
},
3131
"meta": {
32-
"addition": "someValue"
32+
"foo": "bar"
3333
}
3434
}
3535
```
@@ -38,18 +38,25 @@ Can be disabled by setting the `app.disableReplyHelperFunctions` to `true`.
3838

3939
Send an error object to the client with message and code. The HTTP status code will be set to statusCode(defaults to 200).
4040

41-
Can be disabled by setting the `app.disableApiErrorHandler` to `true`.
42-
43-
`throw new ApiError('User Not found', 'err_code_user_not_found', 404, {'error_field':'user'})` will return with http status code 404:
41+
```javascript
42+
import { ApiError } from 'fastify-app';
43+
// and in your route handler:
44+
// ...
45+
throw new ApiError('User Not found', 'err_code_user_not_found', 404, {'error_field':'user'})
46+
```
47+
will return with http status code 404:
4448

4549
```json
4650
{
4751
"status": "error",
4852
"message": "User Not found",
4953
"code": "err_code_user_not_found",
50-
"error_field": "user"
54+
"data": {
55+
"error_field": "username"
56+
}
5157
}
5258
```
59+
This feature can be disabled by setting the `app.disableApiErrorHandler` to `true`.
5360

5461
## Health-checking endpoints
5562

@@ -81,7 +88,7 @@ By default, the server will add the CORS headers to the response. This behavior
8188

8289
### Logging enabled
8390

84-
By default, the server will log the request and response to console. This behavior can be disabled by setting the `fastify.disableRequestLogging` to `true`, or by setting the `fastify.logger`. All the settings under the `fastify` key will be passed to the `fastify` construct function.
91+
By default, the server will log the request and response to console. This behavior can be disabled by setting the `fastify.disableRequestLogging` to `true`. All the settings under the `fastify` key will be passed to the `fastify` construct function.
8592

8693
### Log request body and headers
8794

@@ -95,23 +102,6 @@ By default, the server will log the request body and headers. These behaviors ca
95102

96103
By default, the server will send a `Request-Id` header to the client. This behavior can be disabled by setting the `app.disableSendRequestIdHeader` to `true`.
97104

98-
### ApiError handler and response
99-
100-
By default, if `ApiError(message, code, statusCode, data)` is not caught by local error handler, the server will handle this ApiError and return the error object to the client, with HTTP status code `statusCode` which defaults to `200`.
101-
102-
The response format is:
103-
104-
```json
105-
{
106-
"status": "error",
107-
"message": "custom error messsage string or object",
108-
"code": "custom error code",
109-
"data": "additional data"
110-
}
111-
```
112-
113-
This behavior can be disabled by setting the `app.disableApiErrorHandler` to `true`.
114-
115105
### Other errors handler and default error response
116106

117107
By default, the server will handle the uncaught errors and return the error object to the client. The HTTP status code defaults to 200 which also can be changed by settting `app.InternalServerErrorCode`. If `process.env.NODE_ENV === 'development'`, the error detail will be sent to the client.

0 commit comments

Comments
 (0)