mirror of
https://github.com/wukko/cobalt.git
synced 2025-03-23 01:29:22 +01:00
api/core: fix link parsing error handling
This commit is contained in:
parent
cc05833c6a
commit
7041d61d80
3 changed files with 20 additions and 7 deletions
|
@ -33,8 +33,8 @@ const corsConfig = env.corsWildcard ? {} : {
|
||||||
optionsSuccessStatus: 200
|
optionsSuccessStatus: 200
|
||||||
}
|
}
|
||||||
|
|
||||||
const fail = (res, code) => {
|
const fail = (res, code, context) => {
|
||||||
const { status, body } = createResponse("error", { code });
|
const { status, body } = createResponse("error", { code, context });
|
||||||
res.status(status).json(body);
|
res.status(status).json(body);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -206,8 +206,16 @@ export const runAPI = (express, app, __dirname) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const parsed = extract(normalizedRequest.url);
|
const parsed = extract(normalizedRequest.url);
|
||||||
|
|
||||||
|
if (!parsed) {
|
||||||
|
return fail(res, "error.api.link.invalid");
|
||||||
|
}
|
||||||
if ("error" in parsed) {
|
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 {
|
try {
|
||||||
|
|
|
@ -56,7 +56,7 @@ export default async function(host, patternMatch, obj) {
|
||||||
}
|
}
|
||||||
if (!(testers[host](patternMatch))) {
|
if (!(testers[host](patternMatch))) {
|
||||||
return createResponse("error", {
|
return createResponse("error", {
|
||||||
code: "error.api.link.invalid",
|
code: "error.api.link.unsupported",
|
||||||
context: {
|
context: {
|
||||||
service: host
|
service: host
|
||||||
}
|
}
|
||||||
|
|
|
@ -163,11 +163,11 @@ export function extract(url) {
|
||||||
const host = getHostIfValid(url);
|
const host = getHostIfValid(url);
|
||||||
|
|
||||||
if (!host) {
|
if (!host) {
|
||||||
return { error: "unsupported" };
|
return { error: "link.invalid" };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!env.enabledServices.has(host)) {
|
if (!env.enabledServices.has(host)) {
|
||||||
return { error: "disabled" };
|
return { error: "service.disabled" };
|
||||||
}
|
}
|
||||||
|
|
||||||
let patternMatch;
|
let patternMatch;
|
||||||
|
@ -182,7 +182,12 @@ export function extract(url) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!patternMatch) {
|
if (!patternMatch) {
|
||||||
return null;
|
return {
|
||||||
|
error: "link.unsupported",
|
||||||
|
context: {
|
||||||
|
service: host
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return { host, patternMatch };
|
return { host, patternMatch };
|
||||||
|
|
Loading…
Reference in a new issue