chore: fix error codes

This commit is contained in:
Vjacheslav Trushkin 2024-10-16 20:20:08 +03:00
parent e44822a40d
commit a02dd19d51
5 changed files with 23 additions and 9 deletions

View File

@ -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';
}

View File

@ -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);
}

View File

@ -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

View File

@ -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;
}

View File

@ -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;
}