Skip to content

Commit

Permalink
chore: fix error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberalien committed Oct 16, 2024
1 parent e44822a commit a02dd19
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 9 deletions.
10 changes: 10 additions & 0 deletions src/http/helpers/errors.ts
Original file line number Diff line number Diff line change
@@ -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';
}
5 changes: 3 additions & 2 deletions src/http/helpers/send.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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);
}
Expand Down
5 changes: 3 additions & 2 deletions src/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
}
});

Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/http/responses/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,15 +27,15 @@ 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;
}

// Get icon set
const iconSet = iconSets[prefix];
if (!iconSet) {
// No such icon set
res.send(404);
res.code(404).send(errorText(404));
return;
}

Expand Down
7 changes: 4 additions & 3 deletions src/http/responses/svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,23 +17,23 @@ 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;
}

// Check if icon exists
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;
}

// Get icon
getStoredIconData(iconSetItem, name, (data) => {
if (!data) {
// Invalid icon
res.send(404);
res.code(404).send(errorText(404));
return;
}

Expand Down

0 comments on commit a02dd19

Please sign in to comment.