1
1
import type {
2
- InternalAxiosRequestConfig ,
3
- AxiosResponse ,
4
2
AxiosError ,
3
+ AxiosResponse ,
4
+ InternalAxiosRequestConfig ,
5
5
} from "axios" ;
6
6
import { isAxiosError } from "axios" ;
7
7
import { getErrorMessage } from "coder/site/src/api/errors" ;
8
8
import { getErrorDetail } from "../error" ;
9
9
import {
10
- formatHeaders ,
11
10
formatBody ,
12
- formatUri ,
13
11
formatContentLength ,
12
+ formatHeaders ,
14
13
formatMethod ,
14
+ formatTime ,
15
+ formatUri ,
15
16
} from "./formatters" ;
16
17
import type { Logger } from "./logger" ;
17
18
import {
18
19
HttpClientLogLevel ,
19
- RequestMeta ,
20
20
RequestConfigWithMeta ,
21
+ RequestMeta ,
21
22
} from "./types" ;
22
- import { shortId , formatTime , createRequestId } from "./utils" ;
23
+ import { createRequestId , shortId } from "./utils" ;
23
24
24
25
export function createRequestMeta ( ) :RequestMeta {
25
26
return {
@@ -40,7 +41,7 @@ export function logRequest(
40
41
41
42
const method = formatMethod ( config . method ) ;
42
43
const url = formatUri ( config ) ;
43
- const len = formatContentLength ( config . headers ) ;
44
+ const len = formatContentLength ( config . headers , config . data ) ;
44
45
45
46
let msg = `→${ shortId ( requestId ) } ${ method } ${ url } ${ len } ` ;
46
47
if ( logLevel >= HttpClientLogLevel . HEADERS ) {
@@ -67,7 +68,7 @@ export function logResponse(
67
68
const method = formatMethod ( response . config . method ) ;
68
69
const url = formatUri ( response . config ) ;
69
70
const time = formatTime ( Date . now ( ) - meta . startedAt ) ;
70
- const len = formatContentLength ( response . headers ) ;
71
+ const len = formatContentLength ( response . headers , response . data ) ;
71
72
72
73
let msg = `←${ shortId ( meta . requestId ) } ${ response . status } ${ method } ${ url } ${ len } ${ time } ` ;
73
74
@@ -94,22 +95,32 @@ export function logRequestError(
94
95
const requestId = meta ?. requestId || "unknown" ;
95
96
const time = meta ?formatTime ( Date . now ( ) - meta . startedAt ) :"?ms" ;
96
97
97
- const msg = getErrorMessage ( error , "No error message " ) ;
98
+ const msg = getErrorMessage ( error , "" ) ;
98
99
const detail = getErrorDetail ( error ) ?? "" ;
99
100
100
101
if ( error . response ) {
101
- const responseData =
102
- error . response . statusText || String ( error . response . data ) . slice ( 0 , 100 ) ;
103
- const errorInfo = [ msg , detail , responseData ] . filter ( Boolean ) . join ( " - " ) ;
104
- logger . error (
105
- `←${ shortId ( requestId ) } ${ error . response . status } ${ method } ${ url } ${ time } -${ errorInfo } ` ,
106
- error ,
107
- ) ;
102
+ const msgParts = [
103
+ `←${ shortId ( requestId ) } ${ error . response . status } ${ method } ${ url } ${ time } ` ,
104
+ msg ,
105
+ detail ,
106
+ ] ;
107
+ if ( msg . trim ( ) . length === 0 && detail . trim ( ) . length === 0 ) {
108
+ const responseData =
109
+ error . response . statusText ||
110
+ String ( error . response . data ) . slice ( 0 , 100 ) ||
111
+ "No error info" ;
112
+ msgParts . push ( responseData ) ;
113
+ }
114
+
115
+ const fullMsg = msgParts . map ( ( str ) => str . trim ( ) ) . join ( " - " ) ;
116
+ const headers = formatHeaders ( error . response . headers ) ;
117
+ logger . error ( `${ fullMsg } \n${ headers } ` , error ) ;
108
118
} else {
109
119
const reason = error . code || error . message || "Network error" ;
110
120
const errorInfo = [ msg , detail , reason ] . filter ( Boolean ) . join ( " - " ) ;
121
+ const headers = formatHeaders ( config ?. headers ?? { } ) ;
111
122
logger . error (
112
- `✗${ shortId ( requestId ) } ${ method } ${ url } ${ time } -${ errorInfo } ` ,
123
+ `✗${ shortId ( requestId ) } ${ method } ${ url } ${ time } -${ errorInfo } \n ${ headers } ` ,
113
124
error ,
114
125
) ;
115
126
}