I'm trying to migrate from Play 2.6 to Play 2.7.
In my code I have a custom filter which adds specific headers to the result for CORS. Nothing special here.
Before Play 2.7, I added the headers via Action composition but since we don't have access to the result easily anymore (only the request), I figured out it would be easier with a filter (and I want it on all calls, so it makes sense).
My problem is that in Play 2.7, when there is an exception in my code, my custom filter is called, but not applied. It is applied for each call that don't go in the ErrorHandler. This makes the result unusable by the front-end because it doesn't contains the required headers.
So my question is: How do we make sure the filter is called and applied even when there is an error/exception?
Here is the filter:
@Singleton
public class AccessHeaderFilter extends EssentialFilter {
private final Executor exec;
private final Config configuration;
@Inject
public AccessHeaderFilter(Executor exec, Config configuration) {
this.exec = exec;
this.configuration = configuration;
}
@Override
public EssentialAction apply(EssentialAction next) {
return EssentialAction.of(request ->
next.apply(request)
.map(result ->
result.withHeader(Http.HeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, configuration.getString("front-end.url"))
.withHeader(Http.HeaderNames.ACCESS_CONTROL_REQUEST_METHOD, "GET, POST, PUT, DELETE, OPTIONS")
.withHeader(Http.HeaderNames.ACCESS_CONTROL_ALLOW_HEADERS, "Cache-Control, X-Requested-With, Accept, Content-Type")
.withHeader(Http.HeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true")
.withHeader(Http.HeaderNames.CACHE_CONTROL, "public"),
exec)
);
}
}
Real scenario: The user tries to create a resource with a name that is already taken. I throw a FeedbackException which will return an http 200 with meta exception details that is used by the front-end to show an interesting popup. Before Play 2.7, the response would contains the headers. Now it doesn't.
Any idea how to make sure the filter is applied even when the error handler is triggered?