@@ -11,6 +11,20 @@ const pipeline = promisify(stream.pipeline);
1111
1212const FINISHED = new Set ( [ 'FINISHED' ] ) ;
1313const RESULTS = new Set ( [ 'REVIEW' , 'PASSED' , 'WARNING' , 'SKIPPED' ] ) ;
14+ const MAX_ERROR_BODY_LENGTH = 4000 ;
15+
16+ async function assertResponseStatus ( response , expected ) {
17+ if ( response . status === expected ) {
18+ return ;
19+ }
20+
21+ let body = await response . text ( ) ;
22+ if ( body . length > MAX_ERROR_BODY_LENGTH ) {
23+ body = `${ body . slice ( 0 , MAX_ERROR_BODY_LENGTH ) } \n... truncated ...` ;
24+ }
25+
26+ throw new Error ( `unexpected response code: expected ${ expected } , got ${ response . status } \n${ body || '<empty response body>' } ` ) ;
27+ }
1428
1529class API {
1630 #headers = new Headers ( { accept : 'application/json' } ) ;
@@ -28,11 +42,7 @@ class API {
2842 async getAllTestModules ( ) {
2943 const response = await fetch ( new URL ( 'api/runner/available' , this . #baseUrl) , { headers : this . #headers } ) ;
3044
31- try {
32- assert . equal ( response . status , 200 ) ;
33- } catch {
34- throw new Error ( 'unexpected response code' , { cause : [ response . status , await response . text ( ) ] } ) ;
35- }
45+ await assertResponseStatus ( response , 200 ) ;
3646
3747 return response . json ( ) ;
3848 }
@@ -56,11 +66,7 @@ class API {
5666 body : JSON . stringify ( configuration ) ,
5767 } ) ;
5868
59- try {
60- assert . equal ( response . status , 201 ) ;
61- } catch {
62- throw new Error ( 'unexpected response code' , { cause : [ response . status , await response . text ( ) ] } ) ;
63- }
69+ await assertResponseStatus ( response , 201 ) ;
6470
6571 return response . json ( ) ;
6672 }
@@ -79,11 +85,7 @@ class API {
7985 headers : this . #headers,
8086 } ) ;
8187
82- try {
83- assert . equal ( response . status , 201 ) ;
84- } catch {
85- throw new Error ( 'unexpected response code' , { cause : [ response . status , await response . text ( ) ] } ) ;
86- }
88+ await assertResponseStatus ( response , 201 ) ;
8789
8890 return response . json ( ) ;
8991 }
@@ -93,11 +95,7 @@ class API {
9395
9496 const response = await fetch ( new URL ( `api/info/${ moduleId } ` , this . #baseUrl) , { headers : this . #headers } ) ;
9597
96- try {
97- assert . equal ( response . status , 200 ) ;
98- } catch {
99- throw new Error ( 'unexpected response code' , { cause : [ response . status , await response . text ( ) ] } ) ;
100- }
98+ await assertResponseStatus ( response , 200 ) ;
10199
102100 return response . json ( ) ;
103101 }
@@ -107,11 +105,7 @@ class API {
107105
108106 const response = await fetch ( new URL ( `api/log/${ moduleId } ` , this . #baseUrl) , { headers : this . #headers } ) ;
109107
110- try {
111- assert . equal ( response . status , 200 ) ;
112- } catch {
113- throw new Error ( 'unexpected response code' , { cause : [ response . status , await response . text ( ) ] } ) ;
114- }
108+ await assertResponseStatus ( response , 200 ) ;
115109
116110 return response . json ( ) ;
117111 }
@@ -129,11 +123,7 @@ class API {
129123 headers . set ( 'accept' , 'application/zip' ) ;
130124 const response = await fetch ( new URL ( `api/plan/exporthtml/${ planId } ` , this . #baseUrl) , { headers } ) ;
131125
132- try {
133- assert . equal ( response . status , 200 ) ;
134- } catch {
135- throw new Error ( 'unexpected response code' , { cause : [ response . status , await response . text ( ) ] } ) ;
136- }
126+ await assertResponseStatus ( response , 200 ) ;
137127
138128 return pipeline (
139129 response . body ,
0 commit comments