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 handling with seneca-web 1.0.0 #103

Closed
allinwind3344 opened this issue Sep 30, 2016 · 8 comments
Closed

error handling with seneca-web 1.0.0 #103

allinwind3344 opened this issue Sep 30, 2016 · 8 comments
Labels

Comments

@allinwind3344
Copy link

when seneca action encounter fatal error, it may terminate seneca-web.
check the issue #56, can handle error as below:

seneca.act('role: web', {use: {
  prefix: '/',
  pin: 'role: user, cmd: *',
  map: {
    read: {GET: { responder : handle_response}, alias: '/user/:userId'},
    create: {POST: { responder : handle_response}, alias: '/user'}
  }
}})

whether "responder" still exist in seneca-web 1.0.0? if not, how to customize error handling?

@mcdonnelldean
Copy link
Contributor

@allinwind3344 All action handlers have access to the request and response objects. So to do the above you can do.

seneca.act('role:web', {routes: {
  prefix: '/',
  pin: 'role: user, cmd: *',
  map: {
    read: {GET: true, alias: '/user/:userId', autoreply:false},
    create: {POST: true, alias: '/user', autoreply:false}
  }
}})

Then in your handler,

seneca.add('role:user,cmd:read', (msg, done) => {
  // The req, res provided by the adapter.
  var requestObj = msg.request$
  var replyObj = msg.response$

  // Tell seneca you are done
  done()

  // It is now up to you to reply
 replyObj.xyz // Differs depending on adapter used
})

Do not auto handlers are the only type of web route that can participate in transport. To put it another way request$ and response$ are not transported over the network so cannot be handled in another microservice.

@tswaters
Copy link
Member

Maybe it would be good to provide a next function to routes and if an error occurs route it through there. Not having custom error handling over transport is a pretty substantive limitation

I'm not sure how this could work in HAPI but with express/connect it would allow the host application to handle errors in a custom error handler. Currently errors are always returned as 500, sending the entire error.

@mcdonnelldean
Copy link
Contributor

If it can be made to work generally with all adapters I'm all for it. I want to avoid specific functionality differences where possible.

@tswaters
Copy link
Member

Fair enough.... I'll look into this later today. Heading into work now but in ~8-9 hours I'll look into how this can be done in hapi/koa

@tswaters
Copy link
Member

tswaters commented Oct 1, 2016

After playing with this a bit, it seems that each of the adapters has a method for the host application to handle errors except for connect/express... I've opened tickets on each of the adapters for this and have attached PRs to fix each (see above for tickets)

For prosperity, here's how to handle errors in each of the adapters

  • hapi - attaching onPreResponse extension and checking for isBoom and replying with something custom
  • koa1/2 - attaching middleware at the top that awaits/yields next; one can catch the error and reply
  • connect/express adding a error handler via app.use((err, req, res, next) => {}) (todo - see above tickets)

I've added/updated the test cases in each of the adapters to show some usage of this. (also, @mcdonnelldean, this test case brings HAPI is back up to >80% coverage, so I've bumped it up again)

@mcdonnelldean
Copy link
Contributor

@tswaters epic stuff. We'll probably need a doc on this.

@allinwind3344
Copy link
Author

@mcdonnelldean thanks for your explanation!
@tswaters thanks for your great work!

@sandeepmalviya581
Copy link

how to use boom for exception handling in seneca js ?

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

No branches or pull requests

4 participants