/
vasya2
/
var
Обзор
Документация
Войти
/
vasya2
/
var
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
api/src/plugins/not-found.ts
38 строк
1 KB
Mrugesh Mohapatra
fix(api): update logging (#60187)
06 май 2025, 18:16
Не верифицирован
06 май 2025, 18:16
008e35d
Код
Авторство
О чём код?
import type { FastifyPluginCallback } from 'fastify'; import fp from 'fastify-plugin'; import { getRedirectParams } from '../utils/redirection'; /** * Plugin for handling missing endpoints. * * @param fastify The Fastify instance. * @param _options Options passed to the plugin via `fastify.register(plugin, options)`. * @param done Callback to signal that the logic has completed. */ const fourOhFour: FastifyPluginCallback = (fastify, _options, done) => { // If the request accepts JSON and does not specifically prefer text/html, // this will return a 404 JSON response. Everything else will be redirected. fastify.setNotFoundHandler((req, reply) => { const logger = fastify.log.child({ req, res: reply }); logger.info('User requested path that does not exist'); const accepted = req.accepts().type(['json', 'html']); if (accepted == 'json') { void reply.code(404).send({ error: 'path not found' }); } else { const { origin } = getRedirectParams(req); void reply.status(302); void reply.redirectWithMessage(`${origin}/404`, { type: 'danger', content: `We couldn't find path ${req.url}` }); } }); done(); }; export default fp(fourOhFour, { dependencies: ['redirect-with-message', '@fastify/accepts'] });