summaryrefslogtreecommitdiffhomepage
path: root/packages/app/src/utils/server-errors.ts
blob: 4b9727e61d8f1054ee13639fede8629daa838a53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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")
}