mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-06 23:32:12 +03:00
feat: initial OmniRoute release (rebranded from 9router)
This project is inspired by and originally forked from 9router by decolua (https://github.com/decolua/9router). Full rebrand: 9router → OmniRoute across all source code, configuration, Docker, documentation, and assets.
This commit is contained in:
52
src/shared/utils/upstreamError.js
Normal file
52
src/shared/utils/upstreamError.js
Normal file
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* Normalize upstream error bodies to a JSON-safe payload.
|
||||
* Accepts unknown/object/string inputs and guarantees an { error: { ... } } shape.
|
||||
*/
|
||||
export function toJsonErrorPayload(rawError, fallbackMessage = "Upstream provider error") {
|
||||
const fallback = {
|
||||
error: {
|
||||
message: fallbackMessage,
|
||||
type: "upstream_error",
|
||||
code: "upstream_error",
|
||||
},
|
||||
};
|
||||
|
||||
if (rawError && typeof rawError === "object") {
|
||||
const errorObj = rawError.error;
|
||||
if (typeof errorObj === "string") {
|
||||
return {
|
||||
error: {
|
||||
message: errorObj,
|
||||
type: "upstream_error",
|
||||
code: "upstream_error",
|
||||
},
|
||||
};
|
||||
}
|
||||
if (errorObj && typeof errorObj === "object") {
|
||||
return rawError;
|
||||
}
|
||||
return { error: rawError };
|
||||
}
|
||||
|
||||
if (typeof rawError === "string") {
|
||||
const trimmed = rawError.trim();
|
||||
if (!trimmed) {
|
||||
return fallback;
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(trimmed);
|
||||
return toJsonErrorPayload(parsed, fallbackMessage);
|
||||
} catch {
|
||||
return {
|
||||
error: {
|
||||
message: trimmed,
|
||||
type: "upstream_error",
|
||||
code: "upstream_error",
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return fallback;
|
||||
}
|
||||
Reference in New Issue
Block a user