-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patherror.ts
114 lines (96 loc) · 3.23 KB
/
error.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import {CLIError} from '@oclif/core/errors'
import {AxiosError} from 'axios'
import chalk from 'chalk'
import d from 'debug'
import {InvalidDefinitionError} from './models.js'
type MessagesAndExitCode = [string[], number]
const debug = d('bump-cli:api-client')
export default class APIError extends CLIError {
constructor(httpError?: AxiosError | undefined, info: string[] = [], exit = 100) {
const status = httpError?.response?.status
debug(httpError)
if (httpError) {
switch (status) {
case 422: {
;[info, exit] = APIError.invalidDefinition(httpError.response?.data as InvalidDefinitionError)
break
}
case 401: {
;[info, exit] = APIError.unauthenticated()
break
}
case 404:
case 400: {
;[info, exit] = APIError.notFound(httpError.response?.data as Error)
break
}
}
if (info.length > 0) {
super(info.join('\n'), {exit})
} else {
super(`Unhandled API error (status: ${status}) (error: ${httpError})`, {exit})
}
} else {
super('Unhandled API error', {exit})
}
}
static humanAttributeError(attribute: string, messages: unknown): string[] {
let info: string[] = []
if (Array.isArray(messages)) {
const allMessages = (messages as unknown[])
.map((message, idx) => {
if (message instanceof Object) {
return this.humanAttributeError(idx.toString(), message)
}
return message
})
.join(', ')
info.push(`${chalk.underline(attribute)} ${allMessages}`)
} else if (messages instanceof Object) {
for (const [child, childMessages] of Object.entries(messages)) {
const childErrors = this.humanAttributeError(`${attribute}.${child}`, childMessages)
info = [...info, ...childErrors]
}
} else if (messages) {
info.push(`${chalk.underline(attribute)} ${messages}`)
}
return info
}
static invalidDefinition(error: InvalidDefinitionError): MessagesAndExitCode {
let info: string[] = []
const genericMessage = error.message || 'Invalid definition file'
const exit = 122
if (error && 'errors' in error) {
for (const [attr, message] of Object.entries(error.errors)) {
const humanErrors = APIError.humanAttributeError(attr, message)
info = [...info, ...humanErrors]
}
} else {
info.push(genericMessage)
}
return [info, exit]
}
static is(error: Error): error is APIError {
return error instanceof CLIError && 'http' in error
}
static notFound(error: Error): MessagesAndExitCode {
const genericMessage = error.message || "It seems the documentation provided doesn't exist."
return [
[
genericMessage,
`In a hub context you might want to try the ${chalk.dim(
'--auto-create',
)} flag.\nOtherwise, please check the given ${chalk.dim('--doc')}, ${chalk.dim(
'--token',
)} or ${chalk.dim('--hub')} flags`,
],
104,
]
}
static unauthenticated(): MessagesAndExitCode {
return [
['You are not allowed to deploy to this documentation.', 'please check your --token flag or BUMP_TOKEN variable'],
101,
]
}
}