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

Add support for custom statuses #1119

Closed
wants to merge 1 commit into from

Conversation

LukeBousfield
Copy link
Contributor

Fixes #1042

@codecov
Copy link

codecov bot commented Jan 5, 2018

Codecov Report

Merging #1119 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1119      +/-   ##
==========================================
+ Coverage   99.73%   99.73%   +<.01%     
==========================================
  Files           5        5              
  Lines         372      375       +3     
==========================================
+ Hits          371      374       +3     
  Misses          1        1
Impacted Files Coverage Δ
lib/application.js 100% <100%> (ø) ⬆️
lib/response.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6baa411...9048354. Read the comment docs.

*/

addStatus(code, description) {
this.customStatuses[code] = description;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only issue is naming. i'd prefer this to be called customStatusCodes. what does everyone else think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

other than that, i'm good with this PR

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that my opinion matters, but personally I'd prefer addCustomStatusCode, erring on the side of a more descriptive name

@@ -46,6 +46,18 @@ module.exports = class Application extends Emitter {
this.context = Object.create(context);
this.request = Object.create(request);
this.response = Object.create(response);
this.customStatuses = {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Object.create(null)

}

/**
* Add custom status code

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All other comments have a period after the description text.

@matttowerssonos
Copy link

Any updates on merging this fix? We've run in to the same problem and trying to monkey patch has introduced other issues.

@fl0w
Copy link
Contributor

fl0w commented Jul 3, 2018

Highly subjective and not a reason for this to not being merged

I dislike this solution. I would much rather see statuses provide this and Koa simply delegating. Not sure this is of interest to jshttp though.

assert(!this.res.headersSent, 'headers have already been sent');
this._explicitStatus = true;
this.res.statusCode = code;
if (this.req.httpVersionMajor < 2) this.res.statusMessage = statuses[code];
if (this.req.httpVersionMajor < 2) this.res.statusMessage = statuses[code] || this.customStatuses[code];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I get you correctly, you meant to use the custom status codes instead of the default status codes.Is that right? So, actually the logic would be:

this.customStatuses[code] || statuses[code];

Then, if you have no custom status codes the default status will be used instead. Is that the expected behavior?

@m-a-r-c-e-l-i-n-o
Copy link
Contributor

@fl0w Hey man, I don't understand why this has not been addressed yet. The way I see it, KOA has to either provide a mechanism or go with a different status module because as one of the maintainers of statuses says:
screen shot 2018-11-05 at 4 52 28 pm

They are leaning towards the former (being a list of known status codes), and it sounds like (and is apparent by the code) that KOA is leaning towards the latter. So if KOA is leaning towards the latter, it should provide a mechanism instead of imposing on the statuses library's preferences.

@darielrll
Copy link

@fl0w , @jonathanong , @MarkHerhold , @chittolina and @CodingPower472 I am facing the same issue with http status codes that are not present in Koa, any updates on this feature please

@dead-horse
Copy link
Member

closes via #1308

@dead-horse dead-horse closed this Jan 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants