mirror of https://github.com/iconify/api.git
chore: fix error codes
This commit is contained in:
parent
e44822a40d
commit
a02dd19d51
|
|
@ -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';
|
||||||
|
}
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import type { FastifyReply, FastifyRequest } from 'fastify';
|
import type { FastifyReply, FastifyRequest } from 'fastify';
|
||||||
import { checkJSONPQuery, sendJSONResponse } from './json.js';
|
import { checkJSONPQuery, sendJSONResponse } from './json.js';
|
||||||
|
import { errorText } from './errors.js';
|
||||||
|
|
||||||
type CallbackResult = object | number;
|
type CallbackResult = object | number;
|
||||||
|
|
||||||
|
|
@ -17,14 +18,14 @@ export function handleJSONResponse(
|
||||||
const wrap = checkJSONPQuery(q);
|
const wrap = checkJSONPQuery(q);
|
||||||
if (!wrap) {
|
if (!wrap) {
|
||||||
// Invalid JSONP callback
|
// Invalid JSONP callback
|
||||||
res.send(400);
|
res.code(400).send(errorText(400));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to send response
|
// Function to send response
|
||||||
const respond = (result: CallbackResult) => {
|
const respond = (result: CallbackResult) => {
|
||||||
if (typeof result === 'number') {
|
if (typeof result === 'number') {
|
||||||
res.send(result);
|
res.code(result).send(errorText(result));
|
||||||
} else {
|
} else {
|
||||||
sendJSONResponse(result, q, wrap, res);
|
sendJSONResponse(result, q, wrap, res);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ import { generateUpdateResponse } from './responses/update.js';
|
||||||
import { initVersionResponse, versionResponse } from './responses/version.js';
|
import { initVersionResponse, versionResponse } from './responses/version.js';
|
||||||
import { generateIconsStyleResponse } from './responses/css.js';
|
import { generateIconsStyleResponse } from './responses/css.js';
|
||||||
import { handleJSONResponse } from './helpers/send.js';
|
import { handleJSONResponse } from './helpers/send.js';
|
||||||
|
import { errorText } from './helpers/errors.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start HTTP server
|
* Start HTTP server
|
||||||
|
|
@ -82,7 +83,7 @@ export async function startHTTPServer() {
|
||||||
generateSVGResponse(name.prefix, name.name, req.query, res);
|
generateSVGResponse(name.prefix, name.name, req.query, res);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
res.send(404);
|
res.code(404).send(errorText(404));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -178,7 +179,7 @@ export async function startHTTPServer() {
|
||||||
|
|
||||||
// Options
|
// Options
|
||||||
server.options('/*', (req, res) => {
|
server.options('/*', (req, res) => {
|
||||||
res.send(200);
|
res.code(204).header('Content-Length', '0').send();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Robots
|
// Robots
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import type { IconCSSIconSetOptions } from '@iconify/utils/lib/css/types';
|
||||||
import { getStoredIconsData } from '../../data/icon-set/utils/get-icons.js';
|
import { getStoredIconsData } from '../../data/icon-set/utils/get-icons.js';
|
||||||
import { iconSets } from '../../data/icon-sets.js';
|
import { iconSets } from '../../data/icon-sets.js';
|
||||||
import { paramToBoolean } from '../../misc/bool.js';
|
import { paramToBoolean } from '../../misc/bool.js';
|
||||||
|
import { errorText } from '../helpers/errors.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check selector for weird stuff
|
* Check selector for weird stuff
|
||||||
|
|
@ -26,7 +27,7 @@ export function generateIconsStyleResponse(prefix: string, query: FastifyRequest
|
||||||
|
|
||||||
if (!names || !names.length) {
|
if (!names || !names.length) {
|
||||||
// Missing or invalid icons parameter
|
// Missing or invalid icons parameter
|
||||||
res.send(404);
|
res.code(404).send(errorText(404));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -34,7 +35,7 @@ export function generateIconsStyleResponse(prefix: string, query: FastifyRequest
|
||||||
const iconSet = iconSets[prefix];
|
const iconSet = iconSets[prefix];
|
||||||
if (!iconSet) {
|
if (!iconSet) {
|
||||||
// No such icon set
|
// No such icon set
|
||||||
res.send(404);
|
res.code(404).send(errorText(404));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import { defaultIconCustomisations, IconifyIconCustomisations } from '@iconify/u
|
||||||
import type { FastifyReply, FastifyRequest } from 'fastify';
|
import type { FastifyReply, FastifyRequest } from 'fastify';
|
||||||
import { getStoredIconData } from '../../data/icon-set/utils/get-icon.js';
|
import { getStoredIconData } from '../../data/icon-set/utils/get-icon.js';
|
||||||
import { iconSets } from '../../data/icon-sets.js';
|
import { iconSets } from '../../data/icon-sets.js';
|
||||||
|
import { errorText } from '../helpers/errors.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate SVG
|
* Generate SVG
|
||||||
|
|
@ -16,7 +17,7 @@ export function generateSVGResponse(prefix: string, name: string, query: Fastify
|
||||||
const iconSetItem = iconSets[prefix]?.item;
|
const iconSetItem = iconSets[prefix]?.item;
|
||||||
if (!iconSetItem) {
|
if (!iconSetItem) {
|
||||||
// No such icon set
|
// No such icon set
|
||||||
res.send(404);
|
res.code(404).send(errorText(404));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -24,7 +25,7 @@ export function generateSVGResponse(prefix: string, name: string, query: Fastify
|
||||||
const icons = iconSetItem.icons;
|
const icons = iconSetItem.icons;
|
||||||
if (!(icons.visible[name] || icons.hidden[name]) && !iconSetItem.icons.chars?.[name]) {
|
if (!(icons.visible[name] || icons.hidden[name]) && !iconSetItem.icons.chars?.[name]) {
|
||||||
// No such icon
|
// No such icon
|
||||||
res.send(404);
|
res.code(404).send(errorText(404));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -32,7 +33,7 @@ export function generateSVGResponse(prefix: string, name: string, query: Fastify
|
||||||
getStoredIconData(iconSetItem, name, (data) => {
|
getStoredIconData(iconSetItem, name, (data) => {
|
||||||
if (!data) {
|
if (!data) {
|
||||||
// Invalid icon
|
// Invalid icon
|
||||||
res.send(404);
|
res.code(404).send(errorText(404));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue