forked from hg-pyun/axios-logger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.ts
44 lines (35 loc) · 1.25 KB
/
error.ts
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
import { AxiosError } from 'axios';
import { ErrorLogConfig } from '../common/types';
import { assembleBuildConfig } from '../common/config';
import StringBuilder from '../common/string-builder';
function errorLoggerWithoutPromise(error: AxiosError, config: ErrorLogConfig = {}) {
if (!error.config) {
return error
}
const {config: { method, baseURL, params, url }, response} = error;
let status, statusText, data, headers;
if (response) {
status = response.status;
statusText = response.statusText;
data = response.data;
headers = response.headers;
}
const buildConfig = assembleBuildConfig(config);
const stringBuilder = new StringBuilder(buildConfig);
const log = stringBuilder
.makeLogTypeWithPrefix('Error')
.makeDateFormat(new Date())
.makeMethod(method)
.makeUrl(url, baseURL)
.makeParams(params)
.makeStatus(status, statusText)
.makeHeader(headers)
.makeData(data)
.build();
buildConfig.logger(log);
return error;
}
function errorLogger(error: AxiosError, config?: ErrorLogConfig) {
return Promise.reject(errorLoggerWithoutPromise(error, config));
}
export { errorLogger, errorLoggerWithoutPromise };