(fastify, _options, done)
| 12 | * @param done Callback to signal that the logic has completed. |
| 13 | */ |
| 14 | const fourOhFour: FastifyPluginCallback = (fastify, _options, done) => { |
| 15 | // If the request accepts JSON and does not specifically prefer text/html, |
| 16 | // this will return a 404 JSON response. Everything else will be redirected. |
| 17 | fastify.setNotFoundHandler((req, reply) => { |
| 18 | const logger = fastify.log.child({ req, res: reply }); |
| 19 | logger.info('User requested path that does not exist'); |
| 20 | |
| 21 | const accepted = req.accepts().type(['json', 'html']); |
| 22 | if (accepted == 'json') { |
| 23 | void reply.code(404).send({ error: 'path not found' }); |
| 24 | } else { |
| 25 | const { origin } = getRedirectParams(req); |
| 26 | void reply.status(302); |
| 27 | void reply.redirectWithMessage(`${origin}/404`, { |
| 28 | type: 'danger', |
| 29 | content: `We couldn't find path ${req.url}` |
| 30 | }); |
| 31 | } |
| 32 | }); |
| 33 | done(); |
| 34 | }; |
| 35 | |
| 36 | export default fp(fourOhFour, { |
| 37 | dependencies: ['redirect-with-message', '@fastify/accepts'] |
nothing calls this directly
no test coverage detected