question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow non-standard http status codes to be used

See original GitHub issue

Koa throws when a status code not defined in the statuses package is used in response:

https://github.com/koajs/koa/blob/78832ff6c63fdd93ad9a49f31ba2edd8cbe8adee/lib/response.js#L84

However, the HTTP RFC does not prevent custom status codes to be used: http://greenbytes.de/tech/webdav/draft-ietf-httpbis-p2-semantics-25.html#rfc.section.6.p.2

I can understand why status code validation might be useful for people just starting with http protocols, but for people who actually need to use custom codes, this is very limiting.

Proposal

There are several options that the Koa team could choose to remediate the problem:

  • Do not throw an error and just emit a warning
  • Keep throwing but add a configuration flag to disable the check (ie. koa.validateStatus = false)
  • Allow adding custom http status codes to Koa so that it accepts them (ie. koa.AddStatus(440, 'Invalid Chicken'))

I personally like the second and third approach. Currently the third approach is possible, albeit in a hacky way:

import statuses from 'statuses'

// Add a hypothetical "invalid chicken" response code
statuses[440] = 'Invalid chicken'
statuses.codes.push(440)
statuses['Invalid chicken'] = 440

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:12
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

4reactions
fl0wcommented, Aug 16, 2017

Last option, which I would prefer, is not to expose an API for handling statuses but rather to expose the validation fn, which can be overloaded/replaced as needed.

2reactions
ruimarinhocommented, Oct 20, 2017

What about just validating that the status code is a number as per the HTTP spec?

The status-code element is a three-digit integer code giving the result of the attempt to understand and satisfy the request.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Non-standard HTTP response codes: recommended client ...
How should a http client (web-browser or other app) process non-standard HTTP response codes like 190, 290, 320, 420, 540.
Read more >
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 >
Understanding HTTPS Status Codes + a Complete List
Check out this article to learn about HTTP Status Codes: 1xx Informational; 2xx Success; 3xx Redirection; 4xx Client Error; 5xx Server ...
Read more >
HTTP Status Codes: All 63 explained - including FAQ & Video
The differences in classes are indicated through the first digit of the error code, for example: just like a 404, any other 4xx...
Read more >
HTTP Response Codes | What is HTTP Status Error Codes
This code should be used when a resource has been intentionally removed and the resource should not be purged. When a 410 status...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found