2022-01-21 08:28:41 +00:00
|
|
|
import { Deprecation } from "deprecation";
|
|
|
|
import once from "once";
|
2022-06-08 10:36:39 +00:00
|
|
|
const logOnceCode = once((deprecation) => console.warn(deprecation));
|
|
|
|
const logOnceHeaders = once((deprecation) => console.warn(deprecation));
|
2022-01-21 08:28:41 +00:00
|
|
|
/**
|
|
|
|
* Error with extra properties to help with debugging
|
|
|
|
*/
|
|
|
|
export class RequestError extends Error {
|
|
|
|
constructor(message, statusCode, options) {
|
|
|
|
super(message);
|
|
|
|
// Maintains proper stack trace (only available on V8)
|
|
|
|
/* istanbul ignore next */
|
|
|
|
if (Error.captureStackTrace) {
|
|
|
|
Error.captureStackTrace(this, this.constructor);
|
|
|
|
}
|
|
|
|
this.name = "HttpError";
|
|
|
|
this.status = statusCode;
|
2022-06-08 10:36:39 +00:00
|
|
|
let headers;
|
|
|
|
if ("headers" in options && typeof options.headers !== "undefined") {
|
|
|
|
headers = options.headers;
|
|
|
|
}
|
|
|
|
if ("response" in options) {
|
|
|
|
this.response = options.response;
|
|
|
|
headers = options.response.headers;
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
// 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, {
|
2022-06-08 10:36:39 +00:00
|
|
|
authorization: options.request.headers.authorization.replace(/ .*$/, " [REDACTED]"),
|
2022-01-21 08:28:41 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
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;
|
2022-06-08 10:36:39 +00:00
|
|
|
// deprecations
|
|
|
|
Object.defineProperty(this, "code", {
|
|
|
|
get() {
|
|
|
|
logOnceCode(new Deprecation("[@octokit/request-error] `error.code` is deprecated, use `error.status`."));
|
|
|
|
return statusCode;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
Object.defineProperty(this, "headers", {
|
|
|
|
get() {
|
|
|
|
logOnceHeaders(new Deprecation("[@octokit/request-error] `error.headers` is deprecated, use `error.response.headers`."));
|
|
|
|
return headers || {};
|
|
|
|
},
|
|
|
|
});
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
}
|