api: update error codes in api core functions

This commit is contained in:
wukko 2024-08-19 21:51:45 +06:00
parent a4d57f175e
commit 1f3509db07
No known key found for this signature in database
GPG key ID: 3E30B3F26C7B4AA2
4 changed files with 15 additions and 18 deletions

View file

@ -65,7 +65,7 @@ export function runAPI(express, app, __dirname) {
}, },
handler: (req, res) => { handler: (req, res) => {
const { status, body } = createResponse("error", { const { status, body } = createResponse("error", {
code: "error.rate_exceeded", code: "error.api.rate_exceeded",
context: { context: {
limit: env.rateLimitWindow limit: env.rateLimitWindow
} }
@ -122,11 +122,11 @@ export function runAPI(express, app, __dirname) {
} }
if (!acceptRegex.test(req.header('Accept'))) { if (!acceptRegex.test(req.header('Accept'))) {
return fail(res, 'ErrorInvalidAcceptHeader'); return fail(res, "error.api.header.accept");
} }
if (!acceptRegex.test(req.header('Content-Type'))) { if (!acceptRegex.test(req.header('Content-Type'))) {
return fail(res, 'ErrorInvalidContentType'); return fail(res, "error.api.header.content_type");
} }
req.authorized = true; req.authorized = true;
@ -152,10 +152,7 @@ export function runAPI(express, app, __dirname) {
app.use('/', (err, _, res, next) => { app.use('/', (err, _, res, next) => {
if (err) { if (err) {
const { status, body } = createResponse("error", { const { status, body } = createResponse("error", {
code: "error.body_invalid", code: "error.api.invalid_body",
context: {
limit: env.rateLimitWindow
}
}); });
return res.status(status).json(body); return res.status(status).json(body);
} }
@ -195,7 +192,7 @@ export function runAPI(express, app, __dirname) {
const lang = languageCode(req); const lang = languageCode(req);
if (!request.url) { if (!request.url) {
return fail(res, 'ErrorNoLink'); return fail(res, "error.api.link.missing");
} }
if (request.youtubeDubBrowserLang) { if (request.youtubeDubBrowserLang) {
@ -204,12 +201,12 @@ export function runAPI(express, app, __dirname) {
const { success, data: normalizedRequest } = await normalizeRequest(request); const { success, data: normalizedRequest } = await normalizeRequest(request);
if (!success) { if (!success) {
return fail(res, 'ErrorCantProcess'); return fail(res, "error.api.invalid_body");
} }
const parsed = extract(normalizedRequest.url); const parsed = extract(normalizedRequest.url);
if (parsed === null) { if (parsed === null) {
return fail(res, 'ErrorUnsupported'); return fail(res, "error.api.service.unsupported");
} }
try { try {
@ -219,7 +216,7 @@ export function runAPI(express, app, __dirname) {
res.status(result.status).json(result.body); res.status(result.status).json(result.body);
} catch { } catch {
fail(res, 'ErrorSomethingWentWrong'); fail(res, "error.api.generic");
} }
}) })

View file

@ -39,7 +39,7 @@ export default function({ r, host, audioFormat, isAudioOnly, isAudioMuted, disab
switch (action) { switch (action) {
default: default:
return createResponse("error", { return createResponse("error", {
code: "ErrorEmptyDownload" code: "error.api.fetch.empty"
}); });
case "photo": case "photo":
@ -149,7 +149,7 @@ export default function({ r, host, audioFormat, isAudioOnly, isAudioMuted, disab
case "audio": case "audio":
if (audioIgnore.includes(host) || (host === "reddit" && r.typeId === "redirect")) { if (audioIgnore.includes(host) || (host === "reddit" && r.typeId === "redirect")) {
return createResponse("error", { return createResponse("error", {
code: "ErrorEmptyDownload" code: "error.api.fetch.empty"
}) })
} }

View file

@ -51,12 +51,12 @@ export default async function(host, patternMatch, obj) {
if (!testers[host]) { if (!testers[host]) {
return createResponse("error", { return createResponse("error", {
code: "ErrorUnsupported" code: "error.api.service.unsupported"
}); });
} }
if (!(testers[host](patternMatch))) { if (!(testers[host](patternMatch))) {
return createResponse("error", { return createResponse("error", {
code: "ErrorBrokenLink", code: "error.api.link.invalid",
context: { context: {
service: host service: host
} }
@ -230,7 +230,7 @@ export default async function(host, patternMatch, obj) {
default: default:
return createResponse("error", { return createResponse("error", {
code: "ErrorUnsupported" code: "error.api.service.unsupported"
}); });
} }
@ -265,7 +265,7 @@ export default async function(host, patternMatch, obj) {
}) })
} catch { } catch {
return createResponse("error", { return createResponse("error", {
code: "ErrorBadFetch", code: "error.api.fetch.critical",
context: { context: {
service: host service: host
} }

View file

@ -10,7 +10,7 @@ export function createResponse(responseType, responseData) {
body: { body: {
status: "error", status: "error",
error: { error: {
code: code || "Internal Server Error", code: code || "error.api.fetch.critical",
}, },
critical: true critical: true
} }