2022-11-04 00:55:07 +08:00
|
|
|
import { Deprecation } from "deprecation";
|
|
|
|
import once from "once";
|
2023-10-26 04:27:07 +08:00
|
|
|
const logOnceCode = once((deprecation) => console.warn(deprecation));
|
|
|
|
const logOnceHeaders = once((deprecation) => console.warn(deprecation));
|
|
|
|
class RequestError extends Error {
|
|
|
|
constructor(message, statusCode, options) {
|
|
|
|
super(message);
|
|
|
|
if (Error.captureStackTrace) {
|
|
|
|
Error.captureStackTrace(this, this.constructor);
|
2022-11-04 00:55:07 +08:00
|
|
|
}
|
2023-10-26 04:27:07 +08:00
|
|
|
this.name = "HttpError";
|
|
|
|
this.status = statusCode;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
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.replace(/\bclient_secret=\w+/g, "client_secret=[REDACTED]").replace(/\baccess_token=\w+/g, "access_token=[REDACTED]");
|
|
|
|
this.request = requestCopy;
|
|
|
|
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-11-04 00:55:07 +08:00
|
|
|
}
|
2023-10-26 04:27:07 +08:00
|
|
|
export {
|
|
|
|
RequestError
|
|
|
|
};
|