From a02dd19d511603e33d33f4104d38bcfaf5ea3005 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Wed, 16 Oct 2024 20:20:08 +0300 Subject: [PATCH] chore: fix error codes --- src/http/helpers/errors.ts | 10 ++++++++++ src/http/helpers/send.ts | 5 +++-- src/http/index.ts | 5 +++-- src/http/responses/css.ts | 5 +++-- src/http/responses/svg.ts | 7 ++++--- 5 files changed, 23 insertions(+), 9 deletions(-) create mode 100644 src/http/helpers/errors.ts diff --git a/src/http/helpers/errors.ts b/src/http/helpers/errors.ts new file mode 100644 index 0000000..330bcf5 --- /dev/null +++ b/src/http/helpers/errors.ts @@ -0,0 +1,10 @@ +export function errorText(code: number) { + switch (code) { + case 404: + return 'Not found'; + + case 400: + return 'Bad request'; + } + return 'Internal server error'; +} diff --git a/src/http/helpers/send.ts b/src/http/helpers/send.ts index 292632f..28d7459 100644 --- a/src/http/helpers/send.ts +++ b/src/http/helpers/send.ts @@ -1,5 +1,6 @@ import type { FastifyReply, FastifyRequest } from 'fastify'; import { checkJSONPQuery, sendJSONResponse } from './json.js'; +import { errorText } from './errors.js'; type CallbackResult = object | number; @@ -17,14 +18,14 @@ export function handleJSONResponse( const wrap = checkJSONPQuery(q); if (!wrap) { // Invalid JSONP callback - res.send(400); + res.code(400).send(errorText(400)); return; } // Function to send response const respond = (result: CallbackResult) => { if (typeof result === 'number') { - res.send(result); + res.code(result).send(errorText(result)); } else { sendJSONResponse(result, q, wrap, res); } diff --git a/src/http/index.ts b/src/http/index.ts index c83c58e..7004697 100644 --- a/src/http/index.ts +++ b/src/http/index.ts @@ -15,6 +15,7 @@ import { generateUpdateResponse } from './responses/update.js'; import { initVersionResponse, versionResponse } from './responses/version.js'; import { generateIconsStyleResponse } from './responses/css.js'; import { handleJSONResponse } from './helpers/send.js'; +import { errorText } from './helpers/errors.js'; /** * Start HTTP server @@ -82,7 +83,7 @@ export async function startHTTPServer() { generateSVGResponse(name.prefix, name.name, req.query, res); }); } else { - res.send(404); + res.code(404).send(errorText(404)); } }); @@ -178,7 +179,7 @@ export async function startHTTPServer() { // Options server.options('/*', (req, res) => { - res.send(200); + res.code(204).header('Content-Length', '0').send(); }); // Robots diff --git a/src/http/responses/css.ts b/src/http/responses/css.ts index d1abb56..46c6a84 100644 --- a/src/http/responses/css.ts +++ b/src/http/responses/css.ts @@ -5,6 +5,7 @@ import type { IconCSSIconSetOptions } from '@iconify/utils/lib/css/types'; import { getStoredIconsData } from '../../data/icon-set/utils/get-icons.js'; import { iconSets } from '../../data/icon-sets.js'; import { paramToBoolean } from '../../misc/bool.js'; +import { errorText } from '../helpers/errors.js'; /** * Check selector for weird stuff @@ -26,7 +27,7 @@ export function generateIconsStyleResponse(prefix: string, query: FastifyRequest if (!names || !names.length) { // Missing or invalid icons parameter - res.send(404); + res.code(404).send(errorText(404)); return; } @@ -34,7 +35,7 @@ export function generateIconsStyleResponse(prefix: string, query: FastifyRequest const iconSet = iconSets[prefix]; if (!iconSet) { // No such icon set - res.send(404); + res.code(404).send(errorText(404)); return; } diff --git a/src/http/responses/svg.ts b/src/http/responses/svg.ts index 4faceea..0a0fb40 100644 --- a/src/http/responses/svg.ts +++ b/src/http/responses/svg.ts @@ -7,6 +7,7 @@ import { defaultIconCustomisations, IconifyIconCustomisations } from '@iconify/u import type { FastifyReply, FastifyRequest } from 'fastify'; import { getStoredIconData } from '../../data/icon-set/utils/get-icon.js'; import { iconSets } from '../../data/icon-sets.js'; +import { errorText } from '../helpers/errors.js'; /** * Generate SVG @@ -16,7 +17,7 @@ export function generateSVGResponse(prefix: string, name: string, query: Fastify const iconSetItem = iconSets[prefix]?.item; if (!iconSetItem) { // No such icon set - res.send(404); + res.code(404).send(errorText(404)); return; } @@ -24,7 +25,7 @@ export function generateSVGResponse(prefix: string, name: string, query: Fastify const icons = iconSetItem.icons; if (!(icons.visible[name] || icons.hidden[name]) && !iconSetItem.icons.chars?.[name]) { // No such icon - res.send(404); + res.code(404).send(errorText(404)); return; } @@ -32,7 +33,7 @@ export function generateSVGResponse(prefix: string, name: string, query: Fastify getStoredIconData(iconSetItem, name, (data) => { if (!data) { // Invalid icon - res.send(404); + res.code(404).send(errorText(404)); return; }