How to throw 404 error with some custom data fields
- Dominant language
- No language data
- Stars
- 68
- Forks
- 8
- Avg merge
- 11h 2m
- Merged PRs (30d)
- 2
Description
I would like to ask gurus here. I has a router which looks for a user. If it does not find the user, it throws a 404 error with a message. That is no problem. But if I want to throw an error as an object. It does not work
{
statusCode: 404,
name: 'LookForUser',
internalCode: 'E610014',
correlationId: '00-905e24caee8691b74be2c3ae6d50178c-d38df853b95a5868-00'
}
If i change the statusCode to 400 or 40x but not 404, it work.
This is my codes
```
import routes from './constatnt'
import { Schema, schema } from './schema'
server.route({
method: routes.method,
url: routes.url,
schema,
errorHandler: (error, request, reply) => {
server.errorHandler({ ...error }, request, reply)
},
handler: async (request, reply) => {
try {
const user = await clients.authService.checkUser(request.body?.userDetails)
reply.send(user)
} catch (e) {
throw ({
statusCode: 404,
name: 'LookForUser',
internalCode: 'E610014',
correlationId: '00-905e24caee8691b74be2c3ae6d50178c-d38df853b95a5868-00'
})
}
}
```
Thanks!
Yonglin
The not found handler is triggered when no route matches, you need to return a 404 manually inside your route if a user cannot be found.
_Originally posted by @mcollina in https://github.com/fastify/fastify/issues/402#issuecomment-340369712_
Contributor guide
Assessment
This issue has not been assessed yet.