@@ -4,6 +4,7 @@ import packageJson from "../package.json" with {type: "json"};
44import { Request } from "./Request.js" ;
55import { EmptyResponse } from "./response/index.js" ;
66import { Response } from "./response/Response.js" ;
7+ import { ThrowableResponse } from "./response/ThrowableResponse.js" ;
78import { RouteRegistry } from "./routing/RouteRegistry.js" ;
89import { ServerErrorRegistry } from "./ServerErrorRegistry.js" ;
910
@@ -82,9 +83,13 @@ class Server extends EventEmitter<Server.Events> {
8283 this . errors . _get ( ServerErrorRegistry . ErrorCodes . BAD_URL , null ) . _send ( res , this ) ;
8384 return ;
8485 }
86+
8587 if ( e instanceof Request . SocketClosedError )
8688 return ;
87- throw e ;
89+
90+ this . emit ( "error" , e as any ) ;
91+ this . errors . _get ( ServerErrorRegistry . ErrorCodes . INTERNAL , null ) . _send ( res , this ) ;
92+ return ;
8893 }
8994
9095 for ( const [ key , value ] of this . globalHeaders )
@@ -100,7 +105,13 @@ class Server extends EventEmitter<Server.Events> {
100105 response = await this . routes . handle ( apiRequest ) ;
101106 }
102107 catch ( e ) {
103- if ( e instanceof RouteRegistry . NoRouteError )
108+ if ( e instanceof ThrowableResponse ) {
109+ response = e . getResponse ( ) ;
110+ const cause = e . getError ( ) ;
111+ if ( cause !== null )
112+ this . emit ( "error" , cause ) ;
113+ }
114+ else if ( e instanceof RouteRegistry . NoRouteError )
104115 response = this . errors . _get ( ServerErrorRegistry . ErrorCodes . NO_ROUTE , apiRequest ) ;
105116 else {
106117 this . emit ( "error" , e as any ) ;
0 commit comments