|
36 | 36 | import io.fusionauth.http.server.HTTPRequest; |
37 | 37 | import io.fusionauth.http.server.HTTPResponse; |
38 | 38 | import io.fusionauth.http.server.HTTPServerConfiguration; |
| 39 | +import io.fusionauth.http.server.HTTPUnexpectedExceptionHandler; |
39 | 40 | import io.fusionauth.http.server.Instrumenter; |
40 | 41 | import io.fusionauth.http.server.io.ConnectionClosedException; |
41 | 42 | import io.fusionauth.http.server.io.HTTPInputStream; |
@@ -267,9 +268,27 @@ public void run() { |
267 | 268 | logger.debug(String.format("[%s] Closing socket with status [%d]. An IO exception was thrown during processing. These are pretty common.", Thread.currentThread().threadId(), Status.InternalServerError), e); |
268 | 269 | closeSocketOnError(response, Status.InternalServerError); |
269 | 270 | } catch (Throwable e) { |
270 | | - // Log the error and signal a failure |
| 271 | + HTTPUnexpectedExceptionHandler unexpectedExceptionHandler = configuration.getDefaultExceptionHandler(); |
271 | 272 | var status = Status.InternalServerError; |
272 | | - logger.error(String.format("[%s] Closing socket with status [%d]. An HTTP worker threw an exception while processing a request.", Thread.currentThread().threadId(), status), e); |
| 273 | + if (unexpectedExceptionHandler != null) { |
| 274 | + if (response != null) { |
| 275 | + // Set the initial status, allowing the handler to attempt to modify the status code. |
| 276 | + response.setHeader(Headers.Connection, Connections.Close); |
| 277 | + response.setStatus(status); |
| 278 | + } |
| 279 | + |
| 280 | + try { |
| 281 | + unexpectedExceptionHandler.handle(response, e); |
| 282 | + } catch (Throwable ignore) { |
| 283 | + } |
| 284 | + |
| 285 | + status = response != null ? response.getStatus() : status; |
| 286 | + } else { |
| 287 | + // Log the error |
| 288 | + logger.error(String.format("[%s] Closing socket with status [%d]. An HTTP worker threw an exception while processing a request.", Thread.currentThread().threadId(), status), e); |
| 289 | + } |
| 290 | + |
| 291 | + // Signal an error |
273 | 292 | closeSocketOnError(response, status); |
274 | 293 | } finally { |
275 | 294 | if (instrumenter != null) { |
|
0 commit comments