Skip to content
This repository has been archived by the owner on Aug 31, 2024. It is now read-only.

Task of error handler with Koa 2 cannot pass #36

Open
xie-qianyue opened this issue Nov 19, 2015 · 3 comments
Open

Task of error handler with Koa 2 cannot pass #36

xie-qianyue opened this issue Nov 19, 2015 · 3 comments

Comments

@xie-qianyue
Copy link
Contributor

In the task of error handler, the response cannot be set with ctx.body = 'internal server error'. Here is my error handler :

function errorHandler(ctx, next) {
    try {
       return next();
    }
    catch(err) {
      ctx.status = err.status || 500;     
      ctx.body = 'internal server error';   
      // emit can't help
      // ctx.app.emit('error', err, ctx);      
    }
}

When I verify my codes, the third test always fails :
3. ACTUAL: "responds "Internal Server Error" when requesting /error"
3. EXPECTED: "responds "internal server error" when requesting /error"

@dead-horse
Copy link
Member

this project is only test in koa@1 now. :(

@xie-qianyue
Copy link
Contributor Author

@dead-horse Thanks for your reply. I've created a pull request which modifies the README to figure out that.

@scorbiclife
Copy link

scorbiclife commented Dec 10, 2021

Two things of note, possibly spoilers (click to expand)

About the solution criteria

It seems a passing solution only needs a matching response body, not the response status.
I guess the author intended a response 200 with different message bodies,
while some readers (including myself) think the status code has to match the route.
(So that /500 returns HTTP 500 etc.)

About setting the response body with HTTP status >= 500

As per the http-errors docs,
it seems one needs to set err.expose = true in order to set a custom response body.
That certainly doesn't seem like the author's intention, but is one way to solve the problem.

To summarize I think elaborating the solution criteria might be a good idea.

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

No branches or pull requests

3 participants