index.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { Deprecation } from "deprecation";
  2. import once from "once";
  3. const logOnce = once((deprecation) => console.warn(deprecation));
  4. /**
  5. * Error with extra properties to help with debugging
  6. */
  7. export class RequestError extends Error {
  8. constructor(message, statusCode, options) {
  9. super(message);
  10. // Maintains proper stack trace (only available on V8)
  11. /* istanbul ignore next */
  12. if (Error.captureStackTrace) {
  13. Error.captureStackTrace(this, this.constructor);
  14. }
  15. this.name = "HttpError";
  16. this.status = statusCode;
  17. Object.defineProperty(this, "code", {
  18. get() {
  19. logOnce(new Deprecation("[@octokit/request-error] `error.code` is deprecated, use `error.status`."));
  20. return statusCode;
  21. }
  22. });
  23. this.headers = options.headers || {};
  24. // redact request credentials without mutating original request options
  25. const requestCopy = Object.assign({}, options.request);
  26. if (options.request.headers.authorization) {
  27. requestCopy.headers = Object.assign({}, options.request.headers, {
  28. authorization: options.request.headers.authorization.replace(/ .*$/, " [REDACTED]")
  29. });
  30. }
  31. requestCopy.url = requestCopy.url
  32. // client_id & client_secret can be passed as URL query parameters to increase rate limit
  33. // see https://developer.github.com/v3/#increasing-the-unauthenticated-rate-limit-for-oauth-applications
  34. .replace(/\bclient_secret=\w+/g, "client_secret=[REDACTED]")
  35. // OAuth tokens can be passed as URL query parameters, although it is not recommended
  36. // see https://developer.github.com/v3/#oauth2-token-sent-in-a-header
  37. .replace(/\baccess_token=\w+/g, "access_token=[REDACTED]");
  38. this.request = requestCopy;
  39. }
  40. }