File tree Expand file tree Collapse file tree 1 file changed +23
-13
lines changed Expand file tree Collapse file tree 1 file changed +23
-13
lines changed Original file line number Diff line number Diff line change @@ -62,21 +62,31 @@ export default class SpotifyProvider extends MetadataApiProvider {
62
62
}
63
63
64
64
async query < Data > ( apiUrl : URL , maxTimestamp ?: number ) : Promise < CacheEntry < Data > > {
65
- const accessToken = await this . cachedAccessToken ( this . requestAccessToken ) ;
66
- const cacheEntry = await this . fetchJSON < Data > ( apiUrl , {
67
- policy : { maxTimestamp } ,
68
- requestInit : {
69
- headers : {
70
- 'Authorization' : `Bearer ${ accessToken } ` ,
65
+ try {
66
+ const accessToken = await this . cachedAccessToken ( this . requestAccessToken ) ;
67
+ const cacheEntry = await this . fetchJSON < Data > ( apiUrl , {
68
+ policy : { maxTimestamp } ,
69
+ requestInit : {
70
+ headers : {
71
+ 'Authorization' : `Bearer ${ accessToken } ` ,
72
+ } ,
71
73
} ,
72
- } ,
73
- } ) ;
74
- const { error } = cacheEntry . content as { error ?: ApiError } ;
75
-
76
- if ( error ) {
77
- throw new SpotifyResponseError ( error , apiUrl ) ;
74
+ } ) ;
75
+ const apiError = cacheEntry . content as ApiError ;
76
+ if ( apiError . error ) {
77
+ throw new SpotifyResponseError ( apiError , apiUrl ) ;
78
+ }
79
+ return cacheEntry ;
80
+ } catch ( error ) {
81
+ // Clone the response so the body of the original response can be
82
+ // consumed later if the error gets re-thrown.
83
+ const apiError = await error ?. response ?. clone ( ) . json ( ) as ApiError ;
84
+ if ( apiError ?. error ) {
85
+ throw new SpotifyResponseError ( apiError , apiUrl ) ;
86
+ } else {
87
+ throw error ;
88
+ }
78
89
}
79
- return cacheEntry ;
80
90
}
81
91
82
92
private async requestAccessToken ( ) : Promise < ApiAccessToken > {
You can’t perform that action at this time.
0 commit comments