-
Notifications
You must be signed in to change notification settings - Fork 1
/
errors.ts
123 lines (110 loc) · 3.63 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
import { IAxiodResponse } from "../deps.ts"
import { WebAPICallResult } from './WebClient.ts'
/**
* Deno polyfill for ErrnoException
*/
interface ErrnoException {
code?: string,
errno?: number
message: string,
name: string,
path?: string,
syscall?: string
}
/**
* All errors produced by this package adhere to this interface
*/
export interface CodedError extends ErrnoException {
code: ErrorCode
}
/**
* A dictionary of codes for errors produced by this package
*/
export enum ErrorCode {
RequestError = 'slack_webapi_request_error',
HTTPError = 'slack_webapi_http_error',
PlatformError = 'slack_webapi_platform_error',
RateLimitedError = 'slack_webapi_rate_limited_error',
}
export type WebAPICallError = WebAPIPlatformError | WebAPIRequestError | WebAPIHTTPError | WebAPIRateLimitedError
export interface WebAPIPlatformError extends CodedError {
code: ErrorCode.PlatformError
data: WebAPICallResult & {
error: string
}
}
export interface WebAPIRequestError extends CodedError {
code: ErrorCode.RequestError
original: Error
}
export interface WebAPIHTTPError extends CodedError {
code: ErrorCode.HTTPError
statusCode: number
statusMessage: string
headers: Headers
body?: string
}
export interface WebAPIRateLimitedError extends CodedError {
code: ErrorCode.RateLimitedError
retryAfter: number
}
/**
* Factory for producing a {@link CodedError} from a generic error
*/
function errorWithCode(error: Error, code: ErrorCode): CodedError {
// NOTE: might be able to return something more specific than a CodedError with conditional typing
const codedError = error as Partial<CodedError>
codedError.code = code
return codedError as CodedError
}
/**
* A factory to create WebAPIRequestError objects
* @param original - original error
*/
export function requestErrorWithOriginal(original: Error): WebAPIRequestError {
const error = errorWithCode(
new Error(`A request error occurred: ${original.message}`),
ErrorCode.RequestError,
) as Partial<WebAPIRequestError>
error.original = original
return (error as WebAPIRequestError)
}
/**
* A factory to create WebAPIHTTPError objects
* @param response - original error
*/
export async function httpErrorFromResponse(response: IAxiodResponse): Promise<WebAPIHTTPError> {
const error = errorWithCode(
new Error(`An HTTP protocol error occurred: statusCode = ${response.status}`),
ErrorCode.HTTPError,
) as Partial<WebAPIHTTPError>
error.statusCode = response.status
error.statusMessage = response.statusText
error.headers = response.headers
error.body = await response.data
return (error as WebAPIHTTPError)
}
/**
* A factory to create WebAPIPlatformError objects
* @param result - Web API call result
*/
export function platformErrorFromResult(result: WebAPICallResult & { error: string }): WebAPIPlatformError {
const error = errorWithCode(
new Error(`An API error occurred: ${result.error}`),
ErrorCode.PlatformError,
) as Partial<WebAPIPlatformError>
error.data = result
return (error as WebAPIPlatformError)
}
/**
* A factory to create WebAPIRateLimitedError objects
* @param retrySec - Number of seconds that the request can be retried in
*/
export function rateLimitedErrorWithDelay(retrySec: number): WebAPIRateLimitedError {
const error = errorWithCode(
new Error(`A rate-limit has been reached, you may retry this request in ${retrySec} seconds`),
ErrorCode.RateLimitedError,
) as Partial<WebAPIRateLimitedError>
error.retryAfter = retrySec
return (error as WebAPIRateLimitedError)
}