@@ -19,25 +19,39 @@ export async function fetchPurlDeepScore(purl: string) {
1919 }
2020
2121 spinner . start ( 'Getting deep package score...' )
22- const response = await queryApi (
23- `purl/score/${ encodeURIComponent ( purl ) } ` ,
24- apiToken
25- )
26- spinner ?. successAndStop ( 'Received deep package score response.' )
27-
28- if ( ! response . ok ) {
29- const err = await handleApiError ( response . status )
30- logger . log ( '\nThere was an error' , err )
31- spinner . errorAndStop (
32- `${ colors . bgRed ( colors . white ( response . statusText ) ) } : ${ err } `
22+ let result
23+ try {
24+ result = await queryApi ( `purl/score/${ encodeURIComponent ( purl ) } ` , apiToken )
25+ spinner ?. successAndStop ( 'Received deep package score response.' )
26+ } catch ( e ) {
27+ spinner ?. failAndStop ( 'The request was unsuccessful.' )
28+ const msg = ( e as undefined | { message : string } ) ?. message
29+ if ( msg ) {
30+ logger . fail ( msg )
31+ logger . log (
32+ 'Please report this if the error persists or use the cli version that includes error reporting to automate that'
33+ )
34+ } else {
35+ logger . log (
36+ 'An error happened but no reason was given. If this persists please let us know about it and what you were trying to achieve. Thank you.'
37+ )
38+ }
39+ return
40+ }
41+
42+ if ( ! result . ok ) {
43+ const err = await handleApiError ( result . status )
44+ logger . fail (
45+ `${ colors . bgRed ( colors . bold ( colors . white ( ' ' + result . statusText + ' ' ) ) ) } : ${ err } `
3346 )
47+ process . exitCode = 1
3448 return
3549 }
3650
37- const result = await handleApiCall ( await response . text ( ) , 'Reading text' )
51+ const data = await handleApiCall ( await result . text ( ) , 'Reading text' )
3852
3953 try {
40- return JSON . parse ( result )
54+ return JSON . parse ( data )
4155 } catch ( e ) {
4256 throw new Error (
4357 'Was unable to JSON parse the input from the server. It may not have been a proper JSON response. Please report this problem.'
0 commit comments