Simple email application for Android. Original source code: https://framagit.org/dystopia-project/simple-email
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
2.7 KiB

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