-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.ts
68 lines (56 loc) · 1.99 KB
/
index.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
import type { RequestOptions, OctokitResponse } from "@octokit/types";
import type { RequestErrorOptions } from "./types.js";
/**
* Error with extra properties to help with debugging
*/
export class RequestError extends Error {
name: "HttpError";
/**
* http status code
*/
status: number;
/**
* Request options that lead to the error.
*/
request: RequestOptions;
/**
* Response object if a response was received
*/
response?: OctokitResponse<unknown> | undefined;
constructor(
message: string,
statusCode: number,
options: RequestErrorOptions,
) {
super(message);
this.name = "HttpError";
// Coerce the statusCode to an integer especially if it is a string
this.status = Number.parseInt(statusCode as unknown as string);
// If status code is NaN, then set status to 0 to signal e.g. a network
// error or a AbortError
if (Number.isNaN(this.status)) {
this.status = 0;
}
if ("response" in options) {
this.response = options.response;
}
// redact request credentials without mutating original request options
const requestCopy = Object.assign({}, options.request);
if (options.request.headers.authorization) {
requestCopy.headers = Object.assign({}, options.request.headers, {
authorization: options.request.headers.authorization.replace(
/(?<! ) .*$/,
" [REDACTED]",
),
});
}
requestCopy.url = requestCopy.url
// client_id & client_secret can be passed as URL query parameters to increase rate limit
// see https://developer.github.com/v3/#increasing-the-unauthenticated-rate-limit-for-oauth-applications
.replace(/\bclient_secret=\w+/g, "client_secret=[REDACTED]")
// OAuth tokens can be passed as URL query parameters, although it is not recommended
// see https://developer.github.com/v3/#oauth2-token-sent-in-a-header
.replace(/\baccess_token=\w+/g, "access_token=[REDACTED]");
this.request = requestCopy;
}
}