@@ -5,6 +5,17 @@ const fs = require('fs');
55const path = require ( 'path' ) ;
66const { CI_DOMAIN } = require ( './ci/ci_type_parser' ) ;
77const proxy = require ( './proxy' ) ;
8+ const {
9+ isDebugVerbosity,
10+ debuglog
11+ } = require ( './verbosity' ) ;
12+
13+ function wrappedFetch ( url , options , ...args ) {
14+ if ( isDebugVerbosity ( ) ) {
15+ debuglog ( '[fetch]' , url ) ;
16+ }
17+ return fetch ( url , options , ...args ) ;
18+ }
819
920class Request {
1021 constructor ( credentials ) {
@@ -23,7 +34,7 @@ class Request {
2334 options . headers = options . headers || { } ;
2435 Object . assign ( options . headers , this . getJenkinsHeaders ( ) ) ;
2536 }
26- return fetch ( url , options ) ;
37+ return wrappedFetch ( url , options ) ;
2738 }
2839
2940 async text ( url , options = { } ) {
@@ -32,7 +43,16 @@ class Request {
3243
3344 async json ( url , options = { } ) {
3445 options . headers = options . headers || { } ;
35- return this . fetch ( url , options ) . then ( res => res . json ( ) ) ;
46+ const text = await this . text ( url , options ) ;
47+ try {
48+ return JSON . parse ( text ) ;
49+ } catch ( e ) {
50+ if ( isDebugVerbosity ( ) ) {
51+ debuglog ( '[Request] Cannot parse JSON response from' ,
52+ url , ':\n' , text ) ;
53+ }
54+ throw e ;
55+ }
3656 }
3757
3858 async gql ( name , variables , path ) {
@@ -81,7 +101,7 @@ class Request {
81101 } )
82102 } ;
83103
84- const result = await fetch ( url , options ) . then ( res => res . json ( ) ) ;
104+ const result = await this . json ( url , options ) ;
85105 if ( result . errors ) {
86106 const { type, message } = result . errors [ 0 ] ;
87107 const err = new Error ( `[${ type } ] GraphQL request Error: ${ message } ` ) ;
0 commit comments