errors.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. 'use strict';
  2. const urlLib = require('url');
  3. const http = require('http');
  4. const PCancelable = require('p-cancelable');
  5. const is = require('@sindresorhus/is');
  6. class GotError extends Error {
  7. constructor(message, error, options) {
  8. super(message);
  9. Error.captureStackTrace(this, this.constructor);
  10. this.name = 'GotError';
  11. if (!is.undefined(error.code)) {
  12. this.code = error.code;
  13. }
  14. Object.assign(this, {
  15. host: options.host,
  16. hostname: options.hostname,
  17. method: options.method,
  18. path: options.path,
  19. socketPath: options.socketPath,
  20. protocol: options.protocol,
  21. url: options.href,
  22. gotOptions: options
  23. });
  24. }
  25. }
  26. module.exports.GotError = GotError;
  27. module.exports.CacheError = class extends GotError {
  28. constructor(error, options) {
  29. super(error.message, error, options);
  30. this.name = 'CacheError';
  31. }
  32. };
  33. module.exports.RequestError = class extends GotError {
  34. constructor(error, options) {
  35. super(error.message, error, options);
  36. this.name = 'RequestError';
  37. }
  38. };
  39. module.exports.ReadError = class extends GotError {
  40. constructor(error, options) {
  41. super(error.message, error, options);
  42. this.name = 'ReadError';
  43. }
  44. };
  45. module.exports.ParseError = class extends GotError {
  46. constructor(error, statusCode, options, data) {
  47. super(`${error.message} in "${urlLib.format(options)}": \n${data.slice(0, 77)}...`, error, options);
  48. this.name = 'ParseError';
  49. this.statusCode = statusCode;
  50. this.statusMessage = http.STATUS_CODES[this.statusCode];
  51. }
  52. };
  53. module.exports.HTTPError = class extends GotError {
  54. constructor(response, options) {
  55. const {statusCode} = response;
  56. let {statusMessage} = response;
  57. if (statusMessage) {
  58. statusMessage = statusMessage.replace(/\r?\n/g, ' ').trim();
  59. } else {
  60. statusMessage = http.STATUS_CODES[statusCode];
  61. }
  62. super(`Response code ${statusCode} (${statusMessage})`, {}, options);
  63. this.name = 'HTTPError';
  64. this.statusCode = statusCode;
  65. this.statusMessage = statusMessage;
  66. this.headers = response.headers;
  67. this.body = response.body;
  68. }
  69. };
  70. module.exports.MaxRedirectsError = class extends GotError {
  71. constructor(statusCode, redirectUrls, options) {
  72. super('Redirected 10 times. Aborting.', {}, options);
  73. this.name = 'MaxRedirectsError';
  74. this.statusCode = statusCode;
  75. this.statusMessage = http.STATUS_CODES[this.statusCode];
  76. this.redirectUrls = redirectUrls;
  77. }
  78. };
  79. module.exports.UnsupportedProtocolError = class extends GotError {
  80. constructor(options) {
  81. super(`Unsupported protocol "${options.protocol}"`, {}, options);
  82. this.name = 'UnsupportedProtocolError';
  83. }
  84. };
  85. module.exports.TimeoutError = class extends GotError {
  86. constructor(error, options) {
  87. super(error.message, {code: 'ETIMEDOUT'}, options);
  88. this.name = 'TimeoutError';
  89. this.event = error.event;
  90. }
  91. };
  92. module.exports.CancelError = PCancelable.CancelError;