@@ -4,10 +4,12 @@ import {
4
4
type AxiosError ,
5
5
isAxiosError ,
6
6
} from "axios" ;
7
+ import { getErrorMessage } from "coder/site/src/api/errors" ;
7
8
import { Buffer } from "node:buffer" ;
8
9
import crypto from "node:crypto" ;
9
10
import type * as vscode from "vscode" ;
10
11
import { errToStr } from "../api-helper" ;
12
+ import { getErrorDetail } from "../error" ;
11
13
12
14
export interface RequestMeta {
13
15
requestId :string ;
@@ -58,10 +60,9 @@ export function logRequestStart(
58
60
config :InternalAxiosRequestConfig ,
59
61
) :void {
60
62
const method = ( config . method ?? "GET" ) . toUpperCase ( ) ;
61
- const url = config . url || "" ;
62
- const len = config . headers ?. [ "content-length" ] as string | undefined ;
63
- const lenStr = len ?` (${ len } b)` :"" ;
64
- logger . trace ( `→${ shortId ( requestId ) } ${ method } ${ url } ${ lenStr } ` ) ;
63
+ const url = extractUri ( config ) ;
64
+ const len = extractContentLength ( config . headers ) ;
65
+ logger . trace ( `→${ shortId ( requestId ) } ${ method } ${ url } ${ len } ` ) ;
65
66
}
66
67
67
68
export function logRequestSuccess (
@@ -70,15 +71,23 @@ export function logRequestSuccess(
70
71
response :AxiosResponse ,
71
72
) :void {
72
73
const method = ( response . config . method ?? "GET" ) . toUpperCase ( ) ;
73
- const url = response . config . url || "" ;
74
- const len = response . headers ?. [ "content-length" ] as string | undefined ;
74
+ const url = extractUri ( response . config ) ;
75
75
const ms = Date . now ( ) - meta . startedAt ;
76
- const lenStr = len ? ` ( ${ len } b)` : "" ;
76
+ const len = extractContentLength ( response . headers ) ;
77
77
logger . trace (
78
- `←${ shortId ( meta . requestId ) } ${ response . status } ${ method } ${ url } ${ ms } ms${ lenStr } ` ,
78
+ `←${ shortId ( meta . requestId ) } ${ response . status } ${ method } ${ url } ${ ms } ms${ len } ` ,
79
79
) ;
80
80
}
81
81
82
+ function extractUri ( config :InternalAxiosRequestConfig | undefined ) :string {
83
+ return config ?. url || "<no url>" ;
84
+ }
85
+
86
+ function extractContentLength ( headers :Record < string , unknown > ) :string {
87
+ const len = headers [ "content-length" ] ;
88
+ return len && typeof len === "string" ?`(${ len } b)` :"<unknown size>" ;
89
+ }
90
+
82
91
export function logRequestError (
83
92
logger :vscode . LogOutputChannel ,
84
93
error :AxiosError | unknown ,
@@ -87,23 +96,27 @@ export function logRequestError(
87
96
const config = error . config as RequestConfigWithMeta | undefined ;
88
97
const meta = config ?. metadata ;
89
98
const method = ( config ?. method ?? "GET" ) . toUpperCase ( ) ;
90
- const url = config ?. url || "" ;
91
- const requestId = meta ?. requestId ?? "unknown" ;
99
+ const url = extractUri ( config ) ;
100
+ const requestId = meta ?. requestId || "unknown" ;
92
101
const ms = meta ?Date . now ( ) - meta . startedAt :"?" ;
93
102
103
+ const msg = getErrorMessage ( error , "No error message" ) ;
104
+ const detail = getErrorDetail ( error ) ?? "" ;
94
105
if ( error . response ) {
95
106
// Response error (4xx, 5xx status codes)
96
- const msg =
107
+ const responseData =
97
108
error . response . statusText || String ( error . response . data ) . slice ( 0 , 100 ) ;
109
+ const errorInfo = [ msg , detail , responseData ] . filter ( Boolean ) . join ( " - " ) ;
98
110
logger . error (
99
- `←${ shortId ( requestId ) } ${ error . response . status } ${ method } ${ url } ${ ms } ms -${ msg } ` ,
111
+ `←${ shortId ( requestId ) } ${ error . response . status } ${ method } ${ url } ${ ms } ms -${ errorInfo } ` ,
100
112
error ,
101
113
) ;
102
114
} else {
103
115
// Request error (network, timeout, etc)
104
116
const reason = error . code || error . message || "Network error" ;
117
+ const errorInfo = [ msg , detail , reason ] . filter ( Boolean ) . join ( " - " ) ;
105
118
logger . error (
106
- `✗${ shortId ( requestId ) } ${ method } ${ url } ${ ms } ms -${ reason } ` ,
119
+ `✗${ shortId ( requestId ) } ${ method } ${ url } ${ ms } ms -${ errorInfo } ` ,
107
120
error ,
108
121
) ;
109
122
}
@@ -161,13 +174,13 @@ export class WsLogger {
161
174
const statsStr = stats . length > 0 ?` [${ stats . join ( ", " ) } ]` :"" ;
162
175
163
176
this . logger . trace (
164
- `✗ WS${ shortId ( this . id ) } closed${ codeStr } ${ reasonStr } ${ statsStr } ` ,
177
+ `▣ WS${ shortId ( this . id ) } closed${ codeStr } ${ reasonStr } ${ statsStr } ` ,
165
178
) ;
166
179
}
167
180
168
- logError ( error :unknown ) :void {
181
+ logError ( error :unknown , message : string ) :void {
169
182
const ms = Date . now ( ) - this . startedAt ;
170
- const errorMsg = errToStr ( error , "connection error" ) ;
183
+ const errorMsg = message || errToStr ( error , "connection error" ) ;
171
184
this . logger . error (
172
185
`✗ WS${ shortId ( this . id ) } error${ ms } ms -${ errorMsg } ` ,
173
186
error ,