File tree Expand file tree Collapse file tree 3 files changed +11
-7
lines changed
Expand file tree Collapse file tree 3 files changed +11
-7
lines changed Original file line number Diff line number Diff line change @@ -56,7 +56,7 @@ class Server<A> {
5656 }
5757 catch ( e ) {
5858 if ( e instanceof Request . BadUrlError ) {
59- await this . errors . _get ( ServerErrorRegistry . ErrorCodes . BAD_URL ) . _send ( res ) ;
59+ await this . errors . _get ( ServerErrorRegistry . ErrorCodes . BAD_URL , null ) . _send ( res , this ) ;
6060 return ;
6161 }
6262 if ( e instanceof Request . SocketClosedError )
@@ -78,10 +78,10 @@ class Server<A> {
7878 }
7979 catch ( e ) {
8080 if ( e instanceof RouteRegistry . NoRouteError )
81- response = this . errors . _get ( ServerErrorRegistry . ErrorCodes . NO_ROUTE ) ;
81+ response = this . errors . _get ( ServerErrorRegistry . ErrorCodes . NO_ROUTE , apiRequest ) ;
8282 else {
8383 console . error ( "Internal Server Error:" , e ) ;
84- response = this . errors . _get ( ServerErrorRegistry . ErrorCodes . INTERNAL ) ;
84+ response = this . errors . _get ( ServerErrorRegistry . ErrorCodes . INTERNAL , apiRequest ) ;
8585 }
8686 }
8787 await response . _send ( res , apiRequest ) ;
Original file line number Diff line number Diff line change 11import { Response } from "./response/Response.js" ;
2+ import { Request } from "./Request.js" ;
23import { TextResponse } from "./response/TextResponse.js" ;
34
45/**
56 * A registry for server errors.
67 */
78class ServerErrorRegistry < A > {
8- private readonly responses : Record < ServerErrorRegistry . ErrorCodes , Response < A > > ;
9+ private readonly responses : Record < ServerErrorRegistry . ErrorCodes , Response < A > | ( ( req ?: Request < A > ) => Response < A > ) > ;
910
1011 /**
1112 * Create a new server error registry initialised with default responses.
@@ -28,13 +29,15 @@ class ServerErrorRegistry<A> {
2829 * @param code The server error code.
2930 * @param response The response to send.
3031 */
31- public register ( code : ServerErrorRegistry . ErrorCodes , response : Response < A > ) {
32+ public register ( code : ServerErrorRegistry . ErrorCodes , response : Response < A > | ( ( req ?: Request < A > ) => Response < A > ) ) {
3233 this . responses [ code ] = response ;
3334 }
3435
3536 /** @internal */
36- public _get ( code : ServerErrorRegistry . ErrorCodes ) : Response < A > {
37- return this . responses [ code ] ;
37+ public _get ( code : ServerErrorRegistry . ErrorCodes , req : Request < A > | null ) : Response < A > {
38+ const r = this . responses [ code ] ;
39+ if ( typeof r === "function" ) return r ( req ?? void 0 ) ;
40+ return r ;
3841 }
3942}
4043
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ export class JsonResponse<T, A> extends TextResponse<A> {
99 */
1010 public constructor ( json : T , statusCode = 200 , headers ?: HeadersInit ) {
1111 super ( JsonResponse . serialise ( json ) , statusCode , headers ) ;
12+ this . headers . set ( "content-type" , "application/json" ) ;
1213 }
1314
1415 protected static serialise ( value : any ) : string {
You can’t perform that action at this time.
0 commit comments