1 | import { Deprecation } from 'deprecation';
|
2 | import once from 'once';
|
3 |
|
4 | const logOnceCode = once((deprecation) => console.warn(deprecation));
|
5 | const logOnceHeaders = once((deprecation) => console.warn(deprecation));
|
6 |
|
7 |
|
8 |
|
9 | class RequestError extends Error {
|
10 | constructor(message, statusCode, options) {
|
11 | super(message);
|
12 |
|
13 |
|
14 | if (Error.captureStackTrace) {
|
15 | Error.captureStackTrace(this, this.constructor);
|
16 | }
|
17 | this.name = "HttpError";
|
18 | this.status = statusCode;
|
19 | let headers;
|
20 | if ("headers" in options && typeof options.headers !== "undefined") {
|
21 | headers = options.headers;
|
22 | }
|
23 | if ("response" in options) {
|
24 | this.response = options.response;
|
25 | headers = options.response.headers;
|
26 | }
|
27 |
|
28 | const requestCopy = Object.assign({}, options.request);
|
29 | if (options.request.headers.authorization) {
|
30 | requestCopy.headers = Object.assign({}, options.request.headers, {
|
31 | authorization: options.request.headers.authorization.replace(/ .*$/, " [REDACTED]"),
|
32 | });
|
33 | }
|
34 | requestCopy.url = requestCopy.url
|
35 |
|
36 |
|
37 | .replace(/\bclient_secret=\w+/g, "client_secret=[REDACTED]")
|
38 |
|
39 |
|
40 | .replace(/\baccess_token=\w+/g, "access_token=[REDACTED]");
|
41 | this.request = requestCopy;
|
42 |
|
43 | Object.defineProperty(this, "code", {
|
44 | get() {
|
45 | logOnceCode(new Deprecation("[@octokit/request-error] `error.code` is deprecated, use `error.status`."));
|
46 | return statusCode;
|
47 | },
|
48 | });
|
49 | Object.defineProperty(this, "headers", {
|
50 | get() {
|
51 | logOnceHeaders(new Deprecation("[@octokit/request-error] `error.headers` is deprecated, use `error.response.headers`."));
|
52 | return headers || {};
|
53 | },
|
54 | });
|
55 | }
|
56 | }
|
57 |
|
58 | export { RequestError };
|
59 |
|