File tree Expand file tree Collapse file tree 1 file changed +23
-1
lines changed
Expand file tree Collapse file tree 1 file changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -23,6 +23,19 @@ export interface RequestArgument {
2323 transport : RequestArgumentTransport ;
2424}
2525
26+ export class RestError extends Error {
27+ public readonly response : Response ;
28+ public readonly statusCode : number ;
29+
30+ constructor ( error :string , response : Response ) {
31+ super ( error ) ;
32+ this . response = response ;
33+ this . statusCode = response . status ;
34+
35+ }
36+
37+ }
38+
2639export class RestClient {
2740 private readonly _baseUrl : string ;
2841
@@ -96,6 +109,15 @@ export class RestClient {
96109
97110 const result = await fetch ( url , opts ) ;
98111
99- return result . json ( ) ;
112+ if ( result . status === 404 ) {
113+ return null ;
114+ }
115+ const jsonResult = await result . json ( ) ;
116+ if ( result . status >= 400 ) {
117+ let error = jsonResult . error ?? 'Unknown error' ;
118+ throw new RestError ( error , result ) ;
119+ }
120+
121+ return jsonResult ;
100122 }
101123}
You can’t perform that action at this time.
0 commit comments