You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is very frequent that some RestX projects implements a RestxFilter for exception handling, because there is no extension point on StdRestxMainRouter exception handling
I would introduce a new RestxExceptionHandler interface for this purpose :
public interface RestxExceptionHandler<E extends Throwable> extends Predicate<Throwable> {
void handle(E exception, RestxRequest restxRequest, RestxResponse restxResponse);
}
public abstract class RestxExceptionHandlerBase<E extends Throwable> implements RestxExceptionHandler<E> {
private Class<E> exceptionClass;
public RestxExceptionHandlerBase(Class<E> exceptionClass) {
this.exceptionClass = exceptionClass;
}
public boolean apply(Throwable t) {
return exceptionClass.isAssignableFrom(t);
}
// Maybe some more utility methods such as the one for filtering stacktraces (see #281)
}
with a bunch of default exception handlers allowing to replicate existing exception handlings in StdRestxMainRouter, for instance :
@Module(priority = 1000)
public class ErrorsModule {
@Provide @Named("WebException")
public RestxExceptionHandler<? extends Throwable> webException() {
return new RestxExceptionHandlerBase(WebException.class) {
void handle(WebException ex, RestxRequest restxRequest, RestxResponse restxResponse) {
ex.writeTo(restxRequest, restxResponse);
}
});
}
@Provide @Named("RestxException")
public RestxExceptionHandler<? extends Throwable> webException() {
return new RestxExceptionHandlerBase(RestxError.RestxException.class) {
void handle(RestxError.RestxException ex, RestxRequest restxRequest, RestxResponse restxResponse) {
logger.debug("request raised RestxException", ex);
restxResponse.setStatus(ex.getErrorStatus());
restxResponse.setContentType("application/json");
PrintWriter out = restxResponse.getWriter();
out.println(ex.toJSON());
}
});
}
// etc..
}
WDYT ?
The text was updated successfully, but these errors were encountered:
This is very frequent that some RestX projects implements a
RestxFilter
for exception handling, because there is no extension point onStdRestxMainRouter
exception handlingI would introduce a new
RestxExceptionHandler
interface for this purpose :with a bunch of default exception handlers allowing to replicate existing exception handlings in
StdRestxMainRouter
, for instance :WDYT ?
The text was updated successfully, but these errors were encountered: