MCPcopy Index your code
hub / github.com/freeCodeCamp/freeCodeCamp / fourOhFour

Function fourOhFour

api/src/plugins/not-found.ts:14–34  ·  view source on GitHub ↗
(fastify, _options, done)

Source from the content-addressed store, hash-verified

12 * @param done Callback to signal that the logic has completed.
13 */
14const 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
36export default fp(fourOhFour, {
37 dependencies: ['redirect-with-message', '@fastify/accepts']

Callers

nothing calls this directly

Calls 3

getRedirectParamsFunction · 0.85
doneFunction · 0.85
sendMethod · 0.45

Tested by

no test coverage detected