@@ -5,6 +5,7 @@ import {Authenticator} from "./auth/Authenticator.js";
55import { Request } from "./Request.js" ;
66import { EmptyResponse } from "./response/index.js" ;
77import { Response } from "./response/Response.js" ;
8+ import { ThrowableResponse } from "./response/ThrowableResponse.js" ;
89import { RouteRegistry } from "./routing/RouteRegistry.js" ;
910import { ServerErrorRegistry } from "./ServerErrorRegistry.js" ;
1011
@@ -89,9 +90,13 @@ class Server<A> extends EventEmitter<Server.Events> {
8990 await this . errors . _get ( ServerErrorRegistry . ErrorCodes . BAD_URL , null ) . _send ( res ) ;
9091 return ;
9192 }
93+
9294 if ( e instanceof Request . SocketClosedError )
9395 return ;
94- throw e ;
96+
97+ this . emit ( "error" , e as any ) ;
98+ this . errors . _get ( ServerErrorRegistry . ErrorCodes . INTERNAL , null ) . _send ( res , this ) ;
99+ return ;
95100 }
96101
97102 for ( const [ key , value ] of this . globalHeaders )
@@ -107,7 +112,13 @@ class Server<A> extends EventEmitter<Server.Events> {
107112 response = await this . routes . handle ( apiRequest ) ;
108113 }
109114 catch ( e ) {
110- if ( e instanceof RouteRegistry . NoRouteError )
115+ if ( e instanceof ThrowableResponse ) {
116+ response = e . getResponse ( ) ;
117+ const cause = e . getError ( ) ;
118+ if ( cause !== null )
119+ this . emit ( "error" , cause ) ;
120+ }
121+ else if ( e instanceof RouteRegistry . NoRouteError )
111122 response = this . errors . _get ( ServerErrorRegistry . ErrorCodes . NO_ROUTE , apiRequest ) ;
112123 else {
113124 this . emit ( "error" , e as any ) ;
0 commit comments