|
31 | 31 | import java.io.*; |
32 | 32 | import java.net.InetSocketAddress; |
33 | 33 | import java.nio.charset.Charset; |
| 34 | +import java.nio.charset.StandardCharsets; |
34 | 35 | import java.util.Objects; |
35 | 36 | import java.util.concurrent.Executors; |
36 | 37 | import java.util.logging.Level; |
@@ -304,8 +305,19 @@ public void handle(HttpExchange exchange) throws IOException { |
304 | 305 |
|
305 | 306 | // Process the request using TinyStruct's DefaultHandler logic |
306 | 307 | processRequest(request, response, context); |
| 308 | + } catch (ApplicationException e) { |
| 309 | + logger.log(Level.SEVERE, e.getMessage(), e); |
| 310 | + int status = e.getStatus(); |
| 311 | + ResponseStatus responseStatus = ResponseStatus.valueOf(status); |
| 312 | + if (responseStatus == null) |
| 313 | + responseStatus = ResponseStatus.INTERNAL_SERVER_ERROR; |
| 314 | + |
| 315 | + try { |
| 316 | + sendErrorResponse(exchange, responseStatus.code(), e.getMessage()); |
| 317 | + } catch (Exception ignored) { |
| 318 | + } |
307 | 319 | } catch (Exception e) { |
308 | | - logger.log(Level.SEVERE, "Error processing request", e); |
| 320 | + logger.log(Level.SEVERE, e.getMessage(), e); |
309 | 321 | // Try to send error only if headers haven't been committed yet |
310 | 322 | try { |
311 | 323 | sendErrorResponse(exchange, 500, "Internal Server Error: " + e.getMessage()); |
@@ -561,8 +573,12 @@ private void processRequest(ServerRequest request, ServerResponse response, Cont |
561 | 573 | logger.log(Level.SEVERE, "Error in request processing", e); |
562 | 574 | response.setContentType("text/plain; charset=UTF-8"); |
563 | 575 | int status = e.getStatus(); |
564 | | - response.setStatus(org.tinystruct.http.ResponseStatus.valueOf(status)); |
565 | | - response.writeAndFlush("500 - Internal Server Error".getBytes("UTF-8")); |
| 576 | + ResponseStatus responseStatus = ResponseStatus.valueOf(status); |
| 577 | + if (responseStatus == null) |
| 578 | + responseStatus = ResponseStatus.INTERNAL_SERVER_ERROR; |
| 579 | + |
| 580 | + response.setStatus(responseStatus); |
| 581 | + response.writeAndFlush(e.getMessage().getBytes(StandardCharsets.UTF_8)); |
566 | 582 | response.close(); |
567 | 583 | } |
568 | 584 | } |
@@ -642,4 +658,3 @@ private void sendErrorResponse(HttpExchange exchange, int statusCode, String mes |
642 | 658 |
|
643 | 659 | } |
644 | 660 | } |
645 | | - |
|
0 commit comments