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

Error Handler does not work on 404 #87

Open
TheoSabattie opened this issue Aug 1, 2018 · 2 comments
Open

Error Handler does not work on 404 #87

TheoSabattie opened this issue Aug 1, 2018 · 2 comments

Comments

@TheoSabattie
Copy link
Contributor

TheoSabattie commented Aug 1, 2018

Hello',

I am trying to return a json message on error.
There is no problem when I use defined route and next.error method.
But when that is not a defined route, the 404 response return an html text. :(

Test :

class Main 
{    
    static function main() 
    {
        var app = new App();
        
        app.router.register(new ErrorMaker());
        app.error(ErrorHandler.handle(JsonMessage));
        
        app.http(8443);
    }
}

class ErrorMaker implements IRoute
{
    @:get("/")
    function index() {
        next.error(new Error("Something wrong happened"));
    }
}

http://myUrl/ => JSON
http://myUrl/something => HTML

I assumed that come from l:373 from AutoRegisterRoute.hx but I don't understand very well the macro part of haxe for the moment.

Does someone will be able to fix that?

There is also an other issue related to the same piece of macro:
#55

Thanks in advance,

Théo Sabattié

@fponticelli
Copy link
Member

Assuming that JSON is a good default for 404 is probably not a good strategy. I do not recall the details but content negotiation should be done based on the Accept header. If that is not the case (Might very well not be the case) I suggest to add a catch-all route at the end of your router list and use that to return the desired JSON structure.

@TheoSabattie
Copy link
Contributor Author

TheoSabattie commented Aug 2, 2018

Your patch works, thanks.

I'm ok with you and I tried to get a json by passing an Accept header with application/json but the response was a html/text.
app.error should catch and handle 404 and if there is no error handler, macro must return data type relative to Accept header.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants