1- import { ErrorHandler , HttpError , BadRequestError , AuthenticationError , AuthorizationError , NotFoundError , TimeoutError , ConflictError , ConcurrencyError , RangeError } from "." ;
1+ import { ErrorHandler , HttpError , BadRequestError , AuthenticationError , AuthorizationError , NotFoundError , TimeoutError , ConflictError , ConcurrencyError } from "." ;
22import { HttpStatusCode , HttpHeader } from "../http" ;
33
44/**
@@ -15,46 +15,42 @@ export class DefaultErrorHandler implements ErrorHandler {
1515 * @throws {@link TimeoutError }: {@link HttpStatusCode.RequestTimeout}
1616 * @throws {@link ConflictError }: {@link HttpStatusCode.Conflict}
1717 * @throws {@link ConcurrencyError }: {@link HttpStatusCode.PreconditionFailed}
18- * @throws {@link RangeError }: {@link HttpStatusCode.RequestedRangeNotSatisfiable}
1918 * @throws {@link HttpError }: Other non-success status code
2019 */
2120 async handle ( response : Response ) {
2221 if ( response . ok ) return ;
2322
24- const message = await this . extractJsonMessage ( response ) ;
25- throw DefaultErrorHandler . mapToError (
26- response . status ,
27- message ?? `HTTP ${ response . status } ${ response . statusText } ` ) ;
28- }
29-
30- private async extractJsonMessage ( response : Response ) {
3123 const contentType = response . headers . get ( HttpHeader . ContentType ) ;
32- return ( contentType ?. startsWith ( "application/json" ) || contentType ?. includes ( "+json" ) )
33- ? ( await response . json ( ) ) ?. message
24+ const jsonBody = ( contentType ?. startsWith ( "application/json" ) || contentType ?. includes ( "+json" ) )
25+ ? await response . json ( )
3426 : undefined ;
27+
28+ const errorType = DefaultErrorHandler . errorType ( response . status ) ;
29+ throw new errorType (
30+ jsonBody ?. message ?? jsonBody ?. details ?? `HTTP ${ response . status } ${ response . statusText } ` ,
31+ response . status ,
32+ jsonBody ) ;
3533 }
3634
37- private static mapToError ( status : HttpStatusCode , message : string ) {
35+ private static errorType ( status : HttpStatusCode ) : new ( message : string , status : HttpStatusCode , data ?: any ) => Error {
3836 switch ( status ) {
3937 case HttpStatusCode . BadRequest :
40- return new BadRequestError ( message , status )
38+ return BadRequestError ;
4139 case HttpStatusCode . Unauthorized :
42- return new AuthenticationError ( message , status )
40+ return AuthenticationError ;
4341 case HttpStatusCode . Forbidden :
44- return new AuthorizationError ( message , status )
42+ return AuthorizationError ;
4543 case HttpStatusCode . NotFound :
4644 case HttpStatusCode . Gone :
47- return new NotFoundError ( message , status )
45+ return NotFoundError ;
4846 case HttpStatusCode . RequestTimeout :
49- return new TimeoutError ( message , status )
47+ return TimeoutError ;
5048 case HttpStatusCode . Conflict :
51- return new ConflictError ( message , status )
49+ return ConflictError ;
5250 case HttpStatusCode . PreconditionFailed :
53- return new ConcurrencyError ( message , status )
54- case HttpStatusCode . RequestedRangeNotSatisfiable :
55- return new RangeError ( message , status )
51+ return ConcurrencyError ;
5652 default :
57- return new HttpError ( message , status ) ;
53+ return HttpError ;
5854 }
5955 }
6056}
0 commit comments