@@ -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
@@ -109,9 +110,13 @@ class Server extends EventEmitter<Server.Events> {
109110 this . errors . _get ( ServerErrorRegistry . ErrorCodes . BAD_URL , null ) . _send ( res , this ) ;
110111 return ;
111112 }
113+
112114 if ( e instanceof Request . SocketClosedError )
113115 return ;
114- throw e ;
116+
117+ this . emit ( "error" , e as any ) ;
118+ this . errors . _get ( ServerErrorRegistry . ErrorCodes . INTERNAL , null ) . _send ( res , this ) ;
119+ return ;
115120 }
116121
117122 for ( const [ key , value ] of this . globalHeaders )
@@ -127,7 +132,13 @@ class Server extends EventEmitter<Server.Events> {
127132 response = await this . routes . handle ( apiRequest ) ;
128133 }
129134 catch ( e ) {
130- if ( e instanceof RouteRegistry . NoRouteError )
135+ if ( e instanceof ThrowableResponse ) {
136+ response = e . getResponse ( ) ;
137+ const cause = e . getError ( ) ;
138+ if ( cause !== null )
139+ this . emit ( "error" , cause ) ;
140+ }
141+ else if ( e instanceof RouteRegistry . NoRouteError )
131142 response = this . errors . _get ( ServerErrorRegistry . ErrorCodes . NO_ROUTE , apiRequest ) ;
132143 else {
133144 this . emit ( "error" , e as any ) ;
0 commit comments