@@ -43,21 +43,21 @@ function softShutdown(token: string) {
43
43
44
44
// To handle all this, we send a HTTP request to the GraphQL API instead, which triggers the same thing.
45
45
return new Promise < void > ( ( resolve , reject ) => {
46
- const req = http . request ( "http://127.0.0.1:45457" , {
46
+ const req = http . request ( "http://127.0.0.1:45457/shutdown " , {
47
47
method : 'POST' ,
48
48
headers : {
49
- 'content-type' : 'application/json' ,
50
49
'origin' : 'https://app.httptoolkit.tech' ,
51
50
'authorization' : `Bearer ${ token } `
52
51
}
53
52
} ) ;
54
- req . on ( 'error' , reject ) ;
55
-
56
- req . end ( JSON . stringify ( {
57
- operationName : 'Shutdown' ,
58
- query : 'mutation Shutdown { shutdown }' ,
59
- variables : { }
60
- } ) ) ;
53
+ req . on ( 'error' , ( e ) => {
54
+ console . warn ( `Error requesting server shutdown: ${ e . message } ` ) ;
55
+ // This often happens - not totally clear why, but seems likely that in the race to
56
+ // shut down, the server doesn't successfully send a response first. If the server
57
+ // is not reachable though, it's probably shut down already so we're all good.
58
+ resolve ( ) ;
59
+ } ) ;
60
+ req . end ( ) ;
61
61
62
62
req . on ( 'response' , ( res ) => {
63
63
if ( res . statusCode !== 200 ) {
@@ -71,20 +71,14 @@ function softShutdown(token: string) {
71
71
res . on ( 'end' , ( ) => {
72
72
const rawResponseBody = Buffer . concat ( responseChunks ) ;
73
73
try {
74
- const responseBody = JSON . parse ( rawResponseBody . toString ( 'utf8' ) ) ;
75
- const errors = responseBody . errors as Array < { message : string , path : string [ ] } > | undefined ;
76
- if ( errors ?. length ) {
77
- console . error ( errors ) ;
78
- const errorCount = errors . length > 1 ? `s (${ errors . length } )` : '' ;
74
+ const responseBodyString = rawResponseBody . toString ( 'utf8' ) ;
75
+ const responseBody = JSON . parse ( responseBodyString ) ;
79
76
80
- throw new Error (
81
- `Server error${ errorCount } during shutdown: ${ errors . map ( e =>
82
- `${ e . message } at ${ e . path . join ( '.' ) } `
83
- ) . join ( ', ' ) } `
84
- ) ;
77
+ if ( responseBody . success ) {
78
+ resolve ( ) ;
79
+ } else {
80
+ throw new Error ( `Server shotdown failed: ${ responseBodyString } ` ) ;
85
81
}
86
-
87
- resolve ( ) ;
88
82
} catch ( e ) {
89
83
reject ( e ) ;
90
84
}
0 commit comments