forked from makenotion/notion-sdk-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.ts
286 lines (253 loc) · 7.68 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import { isObject } from "./utils"
import { Assert } from "./type-utils"
/**
* Error codes returned in responses from the API.
*/
export enum APIErrorCode {
Unauthorized = "unauthorized",
RestrictedResource = "restricted_resource",
ObjectNotFound = "object_not_found",
RateLimited = "rate_limited",
InvalidJSON = "invalid_json",
InvalidRequestURL = "invalid_request_url",
InvalidRequest = "invalid_request",
ValidationError = "validation_error",
ConflictError = "conflict_error",
InternalServerError = "internal_server_error",
ServiceUnavailable = "service_unavailable",
}
/**
* Error codes generated for client errors.
*/
export enum ClientErrorCode {
RequestTimeout = "notionhq_client_request_timeout",
ResponseError = "notionhq_client_response_error",
}
/**
* Error codes on errors thrown by the `Client`.
*/
export type NotionErrorCode = APIErrorCode | ClientErrorCode
/**
* Base error type.
*/
abstract class NotionClientErrorBase<
Code extends NotionErrorCode
> extends Error {
abstract code: Code
}
/**
* Error type that encompasses all the kinds of errors that the Notion client will throw.
*/
export type NotionClientError =
| RequestTimeoutError
| UnknownHTTPResponseError
| APIResponseError
// Assert that NotionClientError's `code` property is a narrow type.
// This prevents us from accidentally regressing to `string`-typed name field.
type _assertCodeIsNarrow = Assert<NotionErrorCode, NotionClientError["code"]>
// Assert that the type of `name` in NotionErrorCode is a narrow type.
// This prevents us from accidentally regressing to `string`-typed name field.
type _assertNameIsNarrow = Assert<
"RequestTimeoutError" | "UnknownHTTPResponseError" | "APIResponseError",
NotionClientError["name"]
>
/**
* @param error any value, usually a caught error.
* @returns `true` if error is a `NotionClientError`.
*/
export function isNotionClientError(
error: unknown
): error is NotionClientError {
return isObject(error) && error instanceof NotionClientErrorBase
}
/**
* Narrows down the types of a NotionClientError.
* @param error any value, usually a caught error.
* @param codes an object mapping from possible error codes to `true`
* @returns `true` if error is a `NotionClientError` with a code in `codes`.
*/
function isNotionClientErrorWithCode<Code extends NotionErrorCode>(
error: unknown,
codes: { [C in Code]: true }
): error is NotionClientError & { code: Code } {
return isNotionClientError(error) && error.code in codes
}
/**
* Error thrown by the client if a request times out.
*/
export class RequestTimeoutError extends NotionClientErrorBase<ClientErrorCode.RequestTimeout> {
readonly code = ClientErrorCode.RequestTimeout
readonly name = "RequestTimeoutError"
constructor(message = "Request to Notion API has timed out") {
super(message)
}
static isRequestTimeoutError(error: unknown): error is RequestTimeoutError {
return isNotionClientErrorWithCode(error, {
[ClientErrorCode.RequestTimeout]: true,
})
}
static rejectAfterTimeout<T>(
promise: Promise<T>,
timeoutMS: number
): Promise<T> {
return new Promise<T>((resolve, reject) => {
const timeoutId = setTimeout(() => {
reject(new RequestTimeoutError())
}, timeoutMS)
promise
.then(resolve)
.catch(reject)
.then(() => clearTimeout(timeoutId))
})
}
}
type HTTPResponseErrorCode = ClientErrorCode.ResponseError | APIErrorCode
class HTTPResponseError<
Code extends HTTPResponseErrorCode
> extends NotionClientErrorBase<Code> {
readonly name: string = "HTTPResponseError"
readonly code: Code
readonly status: number
readonly headers: Headers
readonly body: string
constructor(args: {
code: Code
status: number
message: string
headers: Headers
rawBodyText: string
}) {
super(args.message)
const { code, status, headers, rawBodyText } = args
this.code = code
this.status = status
this.headers = headers
this.body = rawBodyText
}
}
const httpResponseErrorCodes: { [C in HTTPResponseErrorCode]: true } = {
[ClientErrorCode.ResponseError]: true,
[APIErrorCode.Unauthorized]: true,
[APIErrorCode.RestrictedResource]: true,
[APIErrorCode.ObjectNotFound]: true,
[APIErrorCode.RateLimited]: true,
[APIErrorCode.InvalidJSON]: true,
[APIErrorCode.InvalidRequestURL]: true,
[APIErrorCode.InvalidRequest]: true,
[APIErrorCode.ValidationError]: true,
[APIErrorCode.ConflictError]: true,
[APIErrorCode.InternalServerError]: true,
[APIErrorCode.ServiceUnavailable]: true,
}
export function isHTTPResponseError(
error: unknown
): error is UnknownHTTPResponseError | APIResponseError {
if (!isNotionClientErrorWithCode(error, httpResponseErrorCodes)) {
return false
}
type _assert = Assert<
UnknownHTTPResponseError | APIResponseError,
typeof error
>
return true
}
/**
* Error thrown if an API call responds with an unknown error code, or does not respond with
* a property-formatted error.
*/
export class UnknownHTTPResponseError extends HTTPResponseError<ClientErrorCode.ResponseError> {
readonly name = "UnknownHTTPResponseError"
constructor(args: {
status: number
message: string | undefined
headers: Headers
rawBodyText: string
}) {
super({
...args,
code: ClientErrorCode.ResponseError,
message:
args.message ??
`Request to Notion API failed with status: ${args.status}`,
})
}
static isUnknownHTTPResponseError(
error: unknown
): error is UnknownHTTPResponseError {
return isNotionClientErrorWithCode(error, {
[ClientErrorCode.ResponseError]: true,
})
}
}
const apiErrorCodes: { [C in APIErrorCode]: true } = {
[APIErrorCode.Unauthorized]: true,
[APIErrorCode.RestrictedResource]: true,
[APIErrorCode.ObjectNotFound]: true,
[APIErrorCode.RateLimited]: true,
[APIErrorCode.InvalidJSON]: true,
[APIErrorCode.InvalidRequestURL]: true,
[APIErrorCode.InvalidRequest]: true,
[APIErrorCode.ValidationError]: true,
[APIErrorCode.ConflictError]: true,
[APIErrorCode.InternalServerError]: true,
[APIErrorCode.ServiceUnavailable]: true,
}
/**
* A response from the API indicating a problem.
* Use the `code` property to handle various kinds of errors. All its possible values are in `APIErrorCode`.
*/
export class APIResponseError extends HTTPResponseError<APIErrorCode> {
readonly name = "APIResponseError"
static isAPIResponseError(error: unknown): error is APIResponseError {
return isNotionClientErrorWithCode(error, apiErrorCodes)
}
}
export function buildRequestError(
response: Response,
bodyText: string
): APIResponseError | UnknownHTTPResponseError {
const apiErrorResponseBody = parseAPIErrorResponseBody(bodyText)
if (apiErrorResponseBody !== undefined) {
return new APIResponseError({
code: apiErrorResponseBody.code,
message: apiErrorResponseBody.message,
headers: response.headers,
status: response.status,
rawBodyText: bodyText,
})
}
return new UnknownHTTPResponseError({
message: undefined,
headers: response.headers,
status: response.status,
rawBodyText: bodyText,
})
}
function parseAPIErrorResponseBody(
body: string
): { code: APIErrorCode; message: string } | undefined {
if (typeof body !== "string") {
return
}
let parsed: unknown
try {
parsed = JSON.parse(body)
} catch (parseError) {
return
}
if (
!isObject(parsed) ||
typeof parsed["message"] !== "string" ||
!isAPIErrorCode(parsed["code"])
) {
return
}
return {
...parsed,
code: parsed["code"],
message: parsed["message"],
}
}
function isAPIErrorCode(code: unknown): code is APIErrorCode {
return typeof code === "string" && code in apiErrorCodes
}