prettymuchbryce/http-status-codes

Error code instead 'text code'?

Closed this issue · 7 comments

I like it very much but I have one problem with this :) For response I'm using status code (with is exactly what I need currently in your solution, e.g.: Http.OK) and error code with is missing here. Error code is exactly the name of the code. So in case 200 this is OK, in case 404 the error code is NOT_FOUND. So this is exactly what you named using exports :)

Could you add method getErrorCode which will return the string name of it? E.g Http.getErrorCode(Http.OK) // 'OK'

:)

I'm not sure I understand your request. You want a function that takes the status code, and returns a string representation of the constant ?

Like this ?
Http.getConstant(http.NOT_FOUND); // 'NOT_FOUND'

Or are you asking for a function that returns the message ? This already exists as getStatusText().
Http.getStatusText(http.NOT_FOUND); // 'Not Found'

First one: Http.getConstant(http.NOT_FOUND); // 'NOT_FOUND' :) It is very useful ^^

I'm having trouble understanding the use case for this. Can you explain how it's useful ? The constant points to the status code, which you must already know if you're calling this function with it.

Yes, it's weird a little. But, let's say you have convetion for errors like this:

{
    errorStatus: 404,
    errorCode: NOT_FOUND,
    description: "something blablabla"
}

Then you need code and constant. So this is very useful to have it in one place :) I can of course convert constant to string but it is much more convenient to have it in one object.

Now this is more clear?

I'm still having trouble understanding. In your case the errorCode and errorStatus would be the same value, as NOT_FOUND is a variable with the value of 404. Why do you need the constant variable name (NOT_FOUND) in string form ?

Maybe it's worth noting that these constant names are not part of any spec. They are somewhat arbitrary.

Ok, I think this have no sense because for me string name of code is the same like getStatusText for you. So In that case someone else will come and say he want another method with another standardised text for him. So I will just leave My own getStatusText constants in other file and use it :)

Yes I think your conclusion is correct. Just to clarify, getStatusText() returns the "Reason-Phrase" which is documented as part of the HTTP specification here. It's not a matter of anyone's preference or opinion, but instead trying to follow the recommendation of the specification.