claudia-api-builder: Possible to specify response as combination of status code and body
From reading the docs, it looks like all error responses get the same status code and content type. Is it possible specify these in the handler? For example:
api.get('/', function () {
if (isBadRequest) return { "message": "Bad request" } // 400
if (isForbidden) return { "message": "Forbidden" } // 403
return { "message": "Success" } // 200
})
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 25 (13 by maintainers)
As promised, here are my thoughts on an implementation of multiple error responses.
@gojko thanks for the detailed response.
For now I’ve taken to using a post deploy step, as you mentioned. It’s a bit of a hack as I didn’t have enough time to look at creating a PR, but it works.
If anybody is interested, the code for my API is here.
One thing I had to also do was to set the error and success codes to the same value, as otherwise claudia sets the default response to return a 500. This often catches all of my errors, regardless of whether I have other regexes in place, this seems to be a quirk of how API Gateway works. I then had to ensure that all of my errors were returned with a prefix in the message, so I could set up my error responses to match the given pattern.
I have some thoughts about how errors could possibly be handled in a more generic way at the claudia level, based on some of my reading around this subject. I’ll try to jot these down over the next few days, probably in a separate issue, for consideration!
my implementation is ready 🎉
Api Errors: https://github.com/phips28/claudia-api-errors @gojko can we move this ‘claudia-api-errors’ module to your claudia/ repo? and add it to npm? error on transfering… see gitter
Claudia: PR: https://github.com/claudiajs/claudia/pull/69