diff options
| author | OpeOginni <[email protected]> | 2026-02-24 15:48:59 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2026-02-24 14:48:59 +0000 |
| commit | cc02476ea5e02d3c827006dcd0c830f7673556e5 (patch) | |
| tree | fe5a89e39d66351ee00f20df86fe6dffbae40cef /packages/app/src/utils/server-errors.ts | |
| parent | 5190589632c97b570bb6f9035aa5c80c0fe833e7 (diff) | |
| download | opencode-cc02476ea5e02d3c827006dcd0c830f7673556e5.tar.gz opencode-cc02476ea5e02d3c827006dcd0c830f7673556e5.zip | |
refactor: replace error handling with serverErrorMessage utility and checks for if error is ConfigInvalidError (#14685)
Diffstat (limited to 'packages/app/src/utils/server-errors.ts')
| -rw-r--r-- | packages/app/src/utils/server-errors.ts | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/packages/app/src/utils/server-errors.ts b/packages/app/src/utils/server-errors.ts new file mode 100644 index 000000000..4b9727e61 --- /dev/null +++ b/packages/app/src/utils/server-errors.ts @@ -0,0 +1,32 @@ +export type ConfigInvalidError = { + name: "ConfigInvalidError" + data: { + path?: string + message?: string + issues?: Array<{ message: string; path: string[] }> + } +} + +export function formatServerError(error: unknown) { + if (isConfigInvalidErrorLike(error)) return parseReabaleConfigInvalidError(error) + if (error instanceof Error && error.message) return error.message + if (typeof error === "string" && error) return error + return "Unknown error" +} + +function isConfigInvalidErrorLike(error: unknown): error is ConfigInvalidError { + if (typeof error !== "object" || error === null) return false + const o = error as Record<string, unknown> + return o.name === "ConfigInvalidError" && typeof o.data === "object" && o.data !== null +} + +export function parseReabaleConfigInvalidError(errorInput: ConfigInvalidError) { + const head = "Invalid configuration" + const file = errorInput.data.path && errorInput.data.path !== "config" ? errorInput.data.path : "" + const detail = errorInput.data.message?.trim() ?? "" + const issues = (errorInput.data.issues ?? []).map((issue) => { + return `${issue.path.join(".")}: ${issue.message}` + }) + if (issues.length) return [head, file, "", ...issues].filter(Boolean).join("\n") + return [head, file, detail].filter(Boolean).join("\n") +} |
