forked from botpress/botpress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.ts
148 lines (129 loc) · 4.15 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { isApiError, ApiError, UnknownError } from '@botpress/client'
import axios, { AxiosError } from 'axios'
import { VError } from 'verror'
import * as consts from './consts'
type KnownApiError = Exclude<ApiError, UnknownError>
const isKnownApiError = (e: unknown): e is KnownApiError => isApiError(e) && !(e instanceof UnknownError)
export class BotpressCLIError extends VError {
public static wrap(thrown: unknown, message: string): BotpressCLIError {
const err = BotpressCLIError.map(thrown)
return new BotpressCLIError(err, message ?? '')
}
public static map(thrown: unknown): BotpressCLIError {
if (thrown instanceof BotpressCLIError) {
return thrown
}
if (thrown instanceof UnknownError) {
const inst = new HTTPError(500, 'An unknown error has occurred.')
inst.debug = thrown.message
return inst
}
if (isKnownApiError(thrown)) {
return HTTPError.fromApi(thrown)
}
if (axios.isAxiosError(thrown)) {
return HTTPError.fromAxios(thrown)
}
if (thrown instanceof Error) {
const { message } = thrown
return new BotpressCLIError(message)
}
return new BotpressCLIError(`${thrown}`)
}
private readonly _debug: string[]
constructor(error: BotpressCLIError, message: string)
constructor(message: string)
public constructor(first: BotpressCLIError | string, second?: string) {
if (typeof first === 'string') {
super(first)
this._debug = []
return
}
super(first, second!)
this._debug = [...first._debug]
}
public set debug(msg: string) {
this._debug.push(msg)
}
public get debug(): string {
const dbgMsgs = this._debug.filter((s) => s.length)
if (!dbgMsgs.length) {
return ''
}
return 'Error: \n' + dbgMsgs.map((s) => ` ${s}`).join('\n')
}
}
export class ExclusiveBotFeatureError extends BotpressCLIError {
constructor() {
const message = 'This feature is only available for bots. This project is an integration'
super(message)
}
}
export class ExclusiveIntegrationFeatureError extends BotpressCLIError {
constructor() {
const message = 'This feature is only available for integration. This project is a bot'
super(message)
}
}
export class HTTPError extends BotpressCLIError {
constructor(public readonly status: number | undefined, message: string) {
super(message)
}
public static fromAxios(e: AxiosError<{ message?: string }>): HTTPError {
const message = this._axiosMsg(e)
return new HTTPError(e.response?.status, message)
}
public static fromApi(e: KnownApiError): HTTPError {
const { message, code } = e
return new HTTPError(code, message)
}
private static _axiosMsg(e: AxiosError<{ message?: string }>): string {
let message = e.message
if (e.response?.statusText) {
message += `\n ${e.response?.statusText}`
}
if (e.response?.status && e.request?.method && e.request?.path) {
message += `\n (${e.response?.status}) ${e.request.method} ${e.request.path}`
}
if (e.response?.data?.message) {
message += `\n ${e.response?.data?.message}`
}
return message
}
}
export class NoBundleFoundError extends BotpressCLIError {
constructor() {
const message = 'No bundle found. Please run `bp bundle` first.'
super(message)
}
}
export class NoBotsFoundError extends BotpressCLIError {
constructor() {
const message = `No Bot found in your Workspace. Please create one first at ${consts.defaultBotpressAppUrl}.`
super(message)
}
}
export class NoWorkspacesFoundError extends BotpressCLIError {
constructor() {
const message = 'No Workspace found. Please create one first.'
super(message)
}
}
export class NotLoggedInError extends BotpressCLIError {
constructor() {
const message = 'Not logged in. Please run `bp login` first.'
super(message)
}
}
export class ParamRequiredError extends BotpressCLIError {
constructor(param: string) {
const message = `${param} is required.`
super(message)
}
}
export class InvalidIntegrationReferenceError extends BotpressCLIError {
constructor(ref: string) {
const message = `Invalid integration reference "${ref}".`
super(message)
}
}