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