Possible to specify response as combination of status code and body
See original GitHub issueFrom 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
})
Issue Analytics
- State:
- Created 8 years ago
- Comments:25 (13 by maintainers)
Top Results From Across the Web
HTTP response status codes - MDN Web Docs - Mozilla
HTTP response status codes indicate whether a specific HTTP request has been successfully completed. Responses are grouped in five classes:
Read more >What HTTP status code to return if multiple actions finish with ...
A Multi-Status response conveys information about multiple resources in situations where multiple status codes might be appropriate. The default ...
Read more >HTTP/1.1: Response
The first line of a Response message is the Status-Line, consisting of the protocol version followed by a numeric status code and its...
Read more >Describing Responses - Swagger
A response is defined by its HTTP status code and the data returned in the response body and/or headers. Here is a minimal...
Read more >Can we create custom HTTP Status codes? - Stack Overflow
Yes you can add custom error codes. If possible use codes that already exist though, and if you are declaring ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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!