Skip to content

Commit a02dd19

Browse files
committed
chore: fix error codes
1 parent e44822a commit a02dd19

File tree

5 files changed

+23
-9
lines changed

5 files changed

+23
-9
lines changed

src/http/helpers/errors.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function errorText(code: number) {
2+
switch (code) {
3+
case 404:
4+
return 'Not found';
5+
6+
case 400:
7+
return 'Bad request';
8+
}
9+
return 'Internal server error';
10+
}

src/http/helpers/send.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { FastifyReply, FastifyRequest } from 'fastify';
22
import { checkJSONPQuery, sendJSONResponse } from './json.js';
3+
import { errorText } from './errors.js';
34

45
type CallbackResult = object | number;
56

@@ -17,14 +18,14 @@ export function handleJSONResponse(
1718
const wrap = checkJSONPQuery(q);
1819
if (!wrap) {
1920
// Invalid JSONP callback
20-
res.send(400);
21+
res.code(400).send(errorText(400));
2122
return;
2223
}
2324

2425
// Function to send response
2526
const respond = (result: CallbackResult) => {
2627
if (typeof result === 'number') {
27-
res.send(result);
28+
res.code(result).send(errorText(result));
2829
} else {
2930
sendJSONResponse(result, q, wrap, res);
3031
}

src/http/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { generateUpdateResponse } from './responses/update.js';
1515
import { initVersionResponse, versionResponse } from './responses/version.js';
1616
import { generateIconsStyleResponse } from './responses/css.js';
1717
import { handleJSONResponse } from './helpers/send.js';
18+
import { errorText } from './helpers/errors.js';
1819

1920
/**
2021
* Start HTTP server
@@ -82,7 +83,7 @@ export async function startHTTPServer() {
8283
generateSVGResponse(name.prefix, name.name, req.query, res);
8384
});
8485
} else {
85-
res.send(404);
86+
res.code(404).send(errorText(404));
8687
}
8788
});
8889

@@ -178,7 +179,7 @@ export async function startHTTPServer() {
178179

179180
// Options
180181
server.options('/*', (req, res) => {
181-
res.send(200);
182+
res.code(204).header('Content-Length', '0').send();
182183
});
183184

184185
// Robots

src/http/responses/css.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { IconCSSIconSetOptions } from '@iconify/utils/lib/css/types';
55
import { getStoredIconsData } from '../../data/icon-set/utils/get-icons.js';
66
import { iconSets } from '../../data/icon-sets.js';
77
import { paramToBoolean } from '../../misc/bool.js';
8+
import { errorText } from '../helpers/errors.js';
89

910
/**
1011
* Check selector for weird stuff
@@ -26,15 +27,15 @@ export function generateIconsStyleResponse(prefix: string, query: FastifyRequest
2627

2728
if (!names || !names.length) {
2829
// Missing or invalid icons parameter
29-
res.send(404);
30+
res.code(404).send(errorText(404));
3031
return;
3132
}
3233

3334
// Get icon set
3435
const iconSet = iconSets[prefix];
3536
if (!iconSet) {
3637
// No such icon set
37-
res.send(404);
38+
res.code(404).send(errorText(404));
3839
return;
3940
}
4041

src/http/responses/svg.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { defaultIconCustomisations, IconifyIconCustomisations } from '@iconify/u
77
import type { FastifyReply, FastifyRequest } from 'fastify';
88
import { getStoredIconData } from '../../data/icon-set/utils/get-icon.js';
99
import { iconSets } from '../../data/icon-sets.js';
10+
import { errorText } from '../helpers/errors.js';
1011

1112
/**
1213
* Generate SVG
@@ -16,23 +17,23 @@ export function generateSVGResponse(prefix: string, name: string, query: Fastify
1617
const iconSetItem = iconSets[prefix]?.item;
1718
if (!iconSetItem) {
1819
// No such icon set
19-
res.send(404);
20+
res.code(404).send(errorText(404));
2021
return;
2122
}
2223

2324
// Check if icon exists
2425
const icons = iconSetItem.icons;
2526
if (!(icons.visible[name] || icons.hidden[name]) && !iconSetItem.icons.chars?.[name]) {
2627
// No such icon
27-
res.send(404);
28+
res.code(404).send(errorText(404));
2829
return;
2930
}
3031

3132
// Get icon
3233
getStoredIconData(iconSetItem, name, (data) => {
3334
if (!data) {
3435
// Invalid icon
35-
res.send(404);
36+
res.code(404).send(errorText(404));
3637
return;
3738
}
3839

0 commit comments

Comments
 (0)