UNPKG

2.58 kBJavaScriptView Raw
1import { Deprecation } from 'deprecation';
2import once from 'once';
3
4const logOnceCode = once((deprecation) => console.warn(deprecation));
5const logOnceHeaders = once((deprecation) => console.warn(deprecation));
6/**
7 * Error with extra properties to help with debugging
8 */
9class RequestError extends Error {
10 constructor(message, statusCode, options) {
11 super(message);
12 // Maintains proper stack trace (only available on V8)
13 /* istanbul ignore next */
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 // redact request credentials without mutating original request options
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 // client_id & client_secret can be passed as URL query parameters to increase rate limit
36 // see https://developer.github.com/v3/#increasing-the-unauthenticated-rate-limit-for-oauth-applications
37 .replace(/\bclient_secret=\w+/g, "client_secret=[REDACTED]")
38 // OAuth tokens can be passed as URL query parameters, although it is not recommended
39 // see https://developer.github.com/v3/#oauth2-token-sent-in-a-header
40 .replace(/\baccess_token=\w+/g, "access_token=[REDACTED]");
41 this.request = requestCopy;
42 // deprecations
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
58export { RequestError };
59//# sourceMappingURL=index.js.map