/
errors.ts
40 lines (36 loc) · 1.3 KB
/
errors.ts
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
33
34
35
36
37
38
39
40
import type { ErrorResponse } from '@oxide/api'
import { capitalize } from '@oxide/util'
export function getParseError(message: string | undefined): string | undefined {
if (!message) return undefined
const inner = /^unable to parse body: (.+) at line \d+ column \d+$/.exec(
message
)?.[1]
return inner && capitalize(inner)
}
// Generic messages that work anywhere. There will probably be few or none of
// these, but it's convenient for now.
const globalCodeMap: Record<string, string> = {
Forbidden: 'Action not authorized',
}
export const getServerError = (
error: ErrorResponse | null,
codeMap: Record<string, string> = {}
) => {
if (!error) return null
const code = error.error?.errorCode
const codeMsg = code && (codeMap[code] || globalCodeMap[code])
const serverMsg = error.error?.message
return (
// first try to get friendly message based on code
codeMsg ||
// then try to pull out parse error from ugly server error
// TODO: probably needs a server fix
getParseError(serverMsg) ||
// then just display the server error if it's there
// TODO: we don't want to do this long-term, there is zero change these are
// user-friendly. but it's handy for debugging for now
serverMsg ||
// finally, a generic fallback
'Unknown error from server'
)
}