From 7041d61d8038d2ee3cfdc5a8076811b54a8b80a8 Mon Sep 17 00:00:00 2001 From: wukko Date: Sat, 24 Aug 2024 16:13:42 +0600 Subject: [PATCH] api/core: fix link parsing error handling --- api/src/core/api.js | 14 +++++++++++--- api/src/processing/match.js | 2 +- api/src/processing/url.js | 11 ++++++++--- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/api/src/core/api.js b/api/src/core/api.js index 1a11d1ce..f8e7724b 100644 --- a/api/src/core/api.js +++ b/api/src/core/api.js @@ -33,8 +33,8 @@ const corsConfig = env.corsWildcard ? {} : { optionsSuccessStatus: 200 } -const fail = (res, code) => { - const { status, body } = createResponse("error", { code }); +const fail = (res, code, context) => { + const { status, body } = createResponse("error", { code, context }); res.status(status).json(body); } @@ -206,8 +206,16 @@ export const runAPI = (express, app, __dirname) => { } const parsed = extract(normalizedRequest.url); + + if (!parsed) { + return fail(res, "error.api.link.invalid"); + } if ("error" in parsed) { - return fail(res, `error.api.service.${parsed.error}`); + let context; + if (parsed?.context) { + context = parsed.context; + } + return fail(res, `error.api.${parsed.error}`, context); } try { diff --git a/api/src/processing/match.js b/api/src/processing/match.js index 09d4cffb..cc58565b 100644 --- a/api/src/processing/match.js +++ b/api/src/processing/match.js @@ -56,7 +56,7 @@ export default async function(host, patternMatch, obj) { } if (!(testers[host](patternMatch))) { return createResponse("error", { - code: "error.api.link.invalid", + code: "error.api.link.unsupported", context: { service: host } diff --git a/api/src/processing/url.js b/api/src/processing/url.js index d9520eee..bd18ee79 100644 --- a/api/src/processing/url.js +++ b/api/src/processing/url.js @@ -163,11 +163,11 @@ export function extract(url) { const host = getHostIfValid(url); if (!host) { - return { error: "unsupported" }; + return { error: "link.invalid" }; } if (!env.enabledServices.has(host)) { - return { error: "disabled" }; + return { error: "service.disabled" }; } let patternMatch; @@ -182,7 +182,12 @@ export function extract(url) { } if (!patternMatch) { - return null; + return { + error: "link.unsupported", + context: { + service: host + } + }; } return { host, patternMatch };