index.js
1.65 KB
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
import util from 'util';
function indent(str) {
return str
.split('\n')
.map(s => (s ? ` ${s}` : ''))
.join('\n');
}
function json(data) {
return JSON.stringify(data, null, 2);
}
module.exports = class AxiosError extends Error {
constructor(messageOrErr, _err = {}) {
let err;
if (messageOrErr instanceof Error) {
super(messageOrErr.message);
err = messageOrErr;
} else {
super(messageOrErr);
err = _err;
}
const { config, request, response } = err;
this.config = config;
this.request = request;
this.response = response;
if (response && response.status) {
this.status = response.status;
}
}
// TODO: remove inspect until we drop node < 6.6
inspect(...args) {
return this[util.inspect.custom](...args);
}
[util.inspect.custom]() {
let requestMessage = '';
if (this.config) {
let { data } = this.config;
try {
data = JSON.parse(data);
} catch (_) {} // eslint-disable-line
let requestData = '';
if (this.config.data) {
requestData = `
Request Data -
${indent(json(data))}`;
}
requestMessage = `
Request -
${this.config.method.toUpperCase()} ${this.config.url}
${requestData}`;
}
let responseMessage = '';
if (this.response) {
let responseData;
if (this.response.data) {
responseData = `
Response Data -
${indent(json(this.response.data))}`;
}
responseMessage = `
Response -
${this.response.status} ${this.response.statusText}
${responseData}`;
}
return `
${this.stack}
Error Message -
${this.message}
${requestMessage}
${responseMessage}
`;
}
};