index.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
  4. var deprecation = require('deprecation');
  5. var once = _interopDefault(require('once'));
  6. const logOnceCode = once(deprecation => console.warn(deprecation));
  7. const logOnceHeaders = once(deprecation => console.warn(deprecation));
  8. /**
  9. * Error with extra properties to help with debugging
  10. */
  11. class RequestError extends Error {
  12. constructor(message, statusCode, options) {
  13. super(message); // Maintains proper stack trace (only available on V8)
  14. /* istanbul ignore next */
  15. if (Error.captureStackTrace) {
  16. Error.captureStackTrace(this, this.constructor);
  17. }
  18. this.name = "HttpError";
  19. this.status = statusCode;
  20. let headers;
  21. if ("headers" in options && typeof options.headers !== "undefined") {
  22. headers = options.headers;
  23. }
  24. if ("response" in options) {
  25. this.response = options.response;
  26. headers = options.response.headers;
  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 // client_id & client_secret can be passed as URL query parameters to increase rate limit
  35. // see https://developer.github.com/v3/#increasing-the-unauthenticated-rate-limit-for-oauth-applications
  36. .replace(/\bclient_secret=\w+/g, "client_secret=[REDACTED]") // OAuth tokens can be passed as URL query parameters, although it is not recommended
  37. // see https://developer.github.com/v3/#oauth2-token-sent-in-a-header
  38. .replace(/\baccess_token=\w+/g, "access_token=[REDACTED]");
  39. this.request = requestCopy; // deprecations
  40. Object.defineProperty(this, "code", {
  41. get() {
  42. logOnceCode(new deprecation.Deprecation("[@octokit/request-error] `error.code` is deprecated, use `error.status`."));
  43. return statusCode;
  44. }
  45. });
  46. Object.defineProperty(this, "headers", {
  47. get() {
  48. logOnceHeaders(new deprecation.Deprecation("[@octokit/request-error] `error.headers` is deprecated, use `error.response.headers`."));
  49. return headers || {};
  50. }
  51. });
  52. }
  53. }
  54. exports.RequestError = RequestError;
  55. //# sourceMappingURL=index.js.map