Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.jooby.Router;
import io.jooby.SneakyThrows;
import io.jooby.WebSocket;
import io.jooby.problem.ProblemDetailsHandler;

/**
* Utility class that allows us to execute routes using a {@link MockContext}.
Expand Down Expand Up @@ -74,6 +75,9 @@ public Object value() {
* @param application Source application.
*/
public MockRouter(@NonNull Jooby application) {
if (application.problemDetailsIsEnabled()) {
application.error(ProblemDetailsHandler.from(application.getConfig()));
}
this.supplier = () -> application;
}

Expand Down Expand Up @@ -472,6 +476,8 @@ public void tryError(Throwable cause, Consumer<MockResponse> consumer) {
*/
public void tryError(Throwable cause, Context ctx) {
var app = supplier.get();
MockContext findContext = ctx instanceof MockContext ? (MockContext) ctx : newContext();
findContext.setRouter(app);
var handler = app.getErrorHandler();
handler.apply(ctx, cause, app.errorCode(cause));
}
Expand Down
33 changes: 33 additions & 0 deletions tests/src/test/java/io/jooby/i3787/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.jooby.i3787;

import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import io.jooby.Jooby;
import io.jooby.problem.HttpProblem;

import java.util.Map;

class Application extends Jooby {
{
Config problemDetailsConfig = ConfigFactory.parseMap(
Map.of("problem.details.enabled", true)
);
getEnvironment().setConfig(problemDetailsConfig.withFallback(getConfig()));

get("/throw", ctx -> {
throw new CustomException();
});

error(CustomException.class, (ctx, throwable, statusCode) -> {
var problem = HttpProblem.badRequest("A Client Error — Obviously");
ctx.getRouter().getErrorHandler().apply(ctx, problem, statusCode);
});
}

public static void main(String[] args) {
runApp(args, Application::new);
}

static class CustomException extends RuntimeException {
}
}
20 changes: 20 additions & 0 deletions tests/src/test/java/io/jooby/i3787/Issue3787.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.jooby.i3787;

import io.jooby.StatusCode;
import io.jooby.test.MockContext;
import io.jooby.test.MockRouter;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class Issue3787 {

private final MockRouter router = new MockRouter(new Application()).setFullExecution(true);

@Test
void issue3787() {
var ctx = new MockContext();
router.tryError(new Application.CustomException(), ctx);
assertEquals(StatusCode.BAD_REQUEST, ctx.getResponseCode());
}
}