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) => {
const { status, body } = createResponse("error", {
code: "error.rate_exceeded",
code: "error.api.rate_exceeded",
context: {
limit: env.rateLimitWindow
}
@ -122,11 +122,11 @@ export function runAPI(express, app, __dirname) {
}
if (!acceptRegex.test(req.header('Accept'))) {
return fail(res, 'ErrorInvalidAcceptHeader');
return fail(res, "error.api.header.accept");
}
if (!acceptRegex.test(req.header('Content-Type'))) {
return fail(res, 'ErrorInvalidContentType');
return fail(res, "error.api.header.content_type");
}
req.authorized = true;
@ -152,10 +152,7 @@ export function runAPI(express, app, __dirname) {
app.use('/', (err, _, res, next) => {
if (err) {
const { status, body } = createResponse("error", {
code: "error.body_invalid",
context: {
limit: env.rateLimitWindow
}
code: "error.api.invalid_body",
});
return res.status(status).json(body);
}
@ -195,7 +192,7 @@ export function runAPI(express, app, __dirname) {
const lang = languageCode(req);
if (!request.url) {
return fail(res, 'ErrorNoLink');
return fail(res, "error.api.link.missing");
}
if (request.youtubeDubBrowserLang) {
@ -204,12 +201,12 @@ export function runAPI(express, app, __dirname) {
const { success, data: normalizedRequest } = await normalizeRequest(request);
if (!success) {
return fail(res, 'ErrorCantProcess');
return fail(res, "error.api.invalid_body");
}
const parsed = extract(normalizedRequest.url);
if (parsed === null) {
return fail(res, 'ErrorUnsupported');
return fail(res, "error.api.service.unsupported");
}
try {
@ -219,7 +216,7 @@ export function runAPI(express, app, __dirname) {
res.status(result.status).json(result.body);
} 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) {
default:
return createResponse("error", {
code: "ErrorEmptyDownload"
code: "error.api.fetch.empty"
});
case "photo":
@ -149,7 +149,7 @@ export default function({ r, host, audioFormat, isAudioOnly, isAudioMuted, disab
case "audio":
if (audioIgnore.includes(host) || (host === "reddit" && r.typeId === "redirect")) {
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]) {
return createResponse("error", {
code: "ErrorUnsupported"
code: "error.api.service.unsupported"
});
}
if (!(testers[host](patternMatch))) {
return createResponse("error", {
code: "ErrorBrokenLink",
code: "error.api.link.invalid",
context: {
service: host
}
@ -230,7 +230,7 @@ export default async function(host, patternMatch, obj) {
default:
return createResponse("error", {
code: "ErrorUnsupported"
code: "error.api.service.unsupported"
});
}
@ -265,7 +265,7 @@ export default async function(host, patternMatch, obj) {
})
} catch {
return createResponse("error", {
code: "ErrorBadFetch",
code: "error.api.fetch.critical",
context: {
service: host
}

View file

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