|
12 | 12 | import io.jooby.internal.SingleValue; |
13 | 13 | import io.jooby.internal.UrlParser; |
14 | 14 | import io.jooby.internal.ValueConverters; |
| 15 | +import org.slf4j.Logger; |
15 | 16 |
|
16 | 17 | import javax.annotation.Nonnull; |
17 | 18 | import javax.annotation.Nullable; |
@@ -559,30 +560,38 @@ default Context setResponseHeader(@Nonnull String name, @Nonnull Object value) { |
559 | 560 | } |
560 | 561 |
|
561 | 562 | /** |
562 | | - * Send an error response. |
| 563 | + * Send an error response. This method set the error code. |
563 | 564 | * |
564 | 565 | * @param cause Error. If this is a fatal error it is going to be rethrow it. |
565 | | - * @param statusCode Status code. |
| 566 | + * @param code Default error code. |
566 | 567 | * @return This context. |
567 | 568 | */ |
568 | 569 | @Override @Nonnull default Context sendError(@Nonnull Throwable cause, |
569 | | - @Nonnull StatusCode statusCode) { |
| 570 | + @Nonnull StatusCode code) { |
570 | 571 | Router router = getRouter(); |
| 572 | + Logger log = router.getLog(); |
571 | 573 | if (isResponseStarted()) { |
572 | | - router.getLog().error(ErrorHandler.errorMessage(this, statusCode), cause); |
| 574 | + log.error(ErrorHandler.errorMessage(this, code), cause); |
573 | 575 | } else { |
574 | 576 | try { |
575 | 577 | if (getResetHeadersOnError()) { |
576 | 578 | removeResponseHeaders(); |
577 | 579 | } |
578 | | - router.getErrorHandler().apply(this, cause, statusCode); |
| 580 | + // set default error code |
| 581 | + setResponseCode(code); |
| 582 | + router.getErrorHandler().apply(this, cause, code); |
579 | 583 | } catch (Exception x) { |
| 584 | + if (!isResponseStarted()) { |
| 585 | + // edge case when there is a bug in a the error handler (probably custom error) what we |
| 586 | + // do is to use the default error handler |
| 587 | + ErrorHandler.create().apply(this, cause, code); |
| 588 | + } |
580 | 589 | if (Server.connectionLost(x)) { |
581 | | - router.getLog() |
582 | | - .debug("error handler resulted in exception {} {}", getMethod(), getRequestPath(), x); |
| 590 | + log.debug("error handler resulted in a exception while processing `{}`", cause.toString(), |
| 591 | + x); |
583 | 592 | } else { |
584 | | - router.getLog() |
585 | | - .error("error handler resulted in exception {} {}", getMethod(), getRequestPath(), x); |
| 593 | + log.error("error handler resulted in a exception while processing `{}`", cause.toString(), |
| 594 | + x); |
586 | 595 | } |
587 | 596 | } |
588 | 597 | } |
|
0 commit comments