Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Error Handling registration #66

Merged
merged 3 commits into from
Nov 23, 2024
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
2 changes: 1 addition & 1 deletion avaje-jex/src/main/java/io/avaje/jex/AppLifecycle.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Application lifecycle support.
*/
public interface AppLifecycle {
public sealed interface AppLifecycle permits DefaultLifecycle {

enum Status {
STARTING,
Expand Down
4 changes: 2 additions & 2 deletions avaje-jex/src/main/java/io/avaje/jex/BootJexState.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.avaje.config.Config;
import io.avaje.inject.BeanScope;

class BootJexState {
final class BootJexState {

private static State state;

Expand Down Expand Up @@ -32,7 +32,7 @@ State create(BeanScope beanScope) {
return new State(jex.start());
}

private static class State {
private static final class State {

private final Jex.Server server;

Expand Down
31 changes: 2 additions & 29 deletions avaje-jex/src/main/java/io/avaje/jex/DJex.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.avaje.jex;

import io.avaje.inject.BeanScope;
import io.avaje.jex.core.CoreServiceManager;
import io.avaje.jex.core.HealthPlugin;
import io.avaje.jex.core.internal.CoreServiceManager;
import io.avaje.jex.jdk.JdkServerStart;
import io.avaje.jex.routes.RoutesBuilder;
import io.avaje.jex.routes.SpiRoutes;
Expand All @@ -14,12 +14,10 @@
final class DJex implements Jex {

private final Routing routing = new DefaultRouting();
private final ErrorHandling errorHandling = new DefaultErrorHandling();
private final AppLifecycle lifecycle = new DefaultLifecycle();
private final StaticFileConfig staticFiles;
private final Map<Class<?>, Object> attributes = new HashMap<>();
private final DJexConfig config = new DJexConfig();
private ServerConfig serverConfig;

DJex() {
this.staticFiles = new DefaultStaticFileConfig(this);
Expand All @@ -43,28 +41,6 @@ public <T> T attribute(Class<T> cls) {
return (T) attributes.get(cls);
}

@Override
public Jex errorHandling(ErrorHandling.Service service) {
service.add(errorHandling);
return this;
}

@Override
public ErrorHandling errorHandling() {
return errorHandling;
}

@Override
public ServerConfig serverConfig() {
return serverConfig;
}

@Override
public Jex serverConfig(ServerConfig serverConfig) {
this.serverConfig = serverConfig;
return this;
}

@Override
public Jex routing(Routing.Service routes) {
routing.add(routes);
Expand Down Expand Up @@ -100,9 +76,6 @@ public Jex configureWith(BeanScope beanScope) {
for (JexPlugin plugin : beanScope.list(JexPlugin.class)) {
plugin.apply(this);
}
for (ErrorHandling.Service service : beanScope.list(ErrorHandling.Service.class)) {
service.add(errorHandling);
}
routing.addAll(beanScope.list(Routing.Service.class));
beanScope.getOptional(JsonService.class).ifPresent(this::jsonService);
return this;
Expand All @@ -116,7 +89,7 @@ public Jex configure(Consumer<JexConfig> configure) {

@Override
public <T extends Exception> Jex exception(Class<T> exceptionClass, ExceptionHandler<T> handler) {
errorHandling.exception(exceptionClass, handler);
routing.exception(exceptionClass, handler);
return this;
}

Expand Down
2 changes: 1 addition & 1 deletion avaje-jex/src/main/java/io/avaje/jex/DJexConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import io.avaje.jex.spi.JsonService;
import io.avaje.jex.spi.TemplateRender;

class DJexConfig implements JexConfig {
final class DJexConfig implements JexConfig {

private int port = 8080;
private String host;
Expand Down
40 changes: 0 additions & 40 deletions avaje-jex/src/main/java/io/avaje/jex/DefaultErrorHandling.java

This file was deleted.

21 changes: 19 additions & 2 deletions avaje-jex/src/main/java/io/avaje/jex/DefaultRouting.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;

import io.avaje.jex.Routing.Entry;
import io.avaje.jex.Routing.Group;
import io.avaje.jex.Routing.Service;
import io.avaje.jex.Routing.Type;
import io.avaje.jex.security.Role;

class DefaultRouting implements Routing {
final class DefaultRouting implements Routing {

private final List<Routing.Entry> handlers = new ArrayList<>();
private final List<HttpFilter> filters = new ArrayList<>();
private final Deque<String> pathDeque = new ArrayDeque<>();
private final Map<Class<?>, ExceptionHandler<?>> exceptionHandlers = new HashMap<>();

/**
* Last entry that we can add permitted roles to.
Expand All @@ -32,6 +38,11 @@ public List<HttpFilter> filters() {
return filters;
}

@Override
public Map<Class<?>, ExceptionHandler<?>> errorHandlers() {
return exceptionHandlers;
}

private String path(String path) {
return String.join("", pathDeque) + ((path.startsWith("/") || path.isEmpty()) ? path : "/" + path);
}
Expand All @@ -57,6 +68,12 @@ public Routing addAll(Collection<Routing.Service> routes) {
return this;
}

@Override
public <T extends Exception> Routing exception(Class<T> type, ExceptionHandler<T> handler) {
exceptionHandlers.put(type, handler);
return this;
}

@Override
public Routing path(String path, Group group) {
addEndpoints(path, group);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.ArrayList;
import java.util.List;

class DefaultStaticFileConfig implements StaticFileConfig {
final class DefaultStaticFileConfig implements StaticFileConfig {

private final List<StaticFileSource> sources = new ArrayList<>();
private final Jex jex;
Expand Down
42 changes: 0 additions & 42 deletions avaje-jex/src/main/java/io/avaje/jex/ErrorHandling.java

This file was deleted.

22 changes: 1 addition & 21 deletions avaje-jex/src/main/java/io/avaje/jex/Jex.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*
* }</pre>
*/
public interface Jex {
public sealed interface Jex permits DJex {

/**
* Create Jex.
Expand Down Expand Up @@ -55,26 +55,6 @@ static Jex create() {
*/
<T> T attribute(Class<T> cls);

/**
* Configure error handlers.
*/
Jex errorHandling(ErrorHandling.Service service);

/**
* Return the Error handler to add error handlers.
*/
ErrorHandling errorHandling();

/**
* Return the server specific configuration.
*/
ServerConfig serverConfig();

/**
* Set the server specific configuration.
*/
Jex serverConfig(ServerConfig serverConfig);

/**
* Add routes and handlers to the routing.
*/
Expand Down
2 changes: 1 addition & 1 deletion avaje-jex/src/main/java/io/avaje/jex/JexConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* Jex configuration.
*/
public interface JexConfig {
public sealed interface JexConfig permits DJexConfig {

/**
* Set the port to use. Defaults to 7001.
Expand Down
15 changes: 13 additions & 2 deletions avaje-jex/src/main/java/io/avaje/jex/Routing.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;

import io.avaje.jex.security.Role;

public interface Routing {
public sealed interface Routing permits DefaultRouting {

/**
* Add the routes provided by the Routing Service.
Expand Down Expand Up @@ -49,6 +50,11 @@ public interface Routing {
*/
Routing withRoles(Role... permittedRoles);

/**
* Register an exception handler for the given exception type.
*/
<T extends Exception> Routing exception(Class<T> exceptionClass, ExceptionHandler<T> handler);

/**
* Add a group of route handlers with a common path prefix.
*/
Expand Down Expand Up @@ -141,7 +147,7 @@ default Routing before(Consumer<Context> handler) {

/** Add a post-processing filter for all requests. */
default Routing after(Consumer<Context> handler) {

return filter(
(ctx, chain) -> {
chain.proceed();
Expand All @@ -159,6 +165,11 @@ default Routing after(Consumer<Context> handler) {
*/
List<HttpFilter> filters();

/**
* Return all the registered Exception Handlers.
*/
Map<Class<?>, ExceptionHandler<?>> errorHandlers();

/**
* A group of routing entries prefixed by a common path.
*/
Expand Down
7 changes: 0 additions & 7 deletions avaje-jex/src/main/java/io/avaje/jex/ServerConfig.java

This file was deleted.

2 changes: 1 addition & 1 deletion avaje-jex/src/main/java/io/avaje/jex/StaticFileConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.List;

public interface StaticFileConfig {
public sealed interface StaticFileConfig permits DefaultStaticFileConfig {

Jex addClasspath(String path);

Expand Down
Loading