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