28

Does Angular 2 framework contain Http Response Status Codes as constants?

So instead of writing 404 I can write HttpStatus.NOT_FOUND.

Thanks!

4
  • you can make use of a properties file which contains all of your response codes and then import that file in services and use Commented Jul 12, 2017 at 7:46
  • I agree with @RahulSingh. Responses only contain the error code number, but not a specific string code. Commented Jul 12, 2017 at 8:02
  • 1
    Does this exist? It does in Java. I found this (npmjs.com/package/http-status-codes) but I would love to know if it exists in the angular framework itself. Commented Jan 3, 2018 at 16:15
  • You can define your Enum or you can use http-status-codes module npmjs.com/package/http-status-codes Commented Feb 27, 2018 at 9:20

2 Answers 2

17

as of now in angular 12 this feature is now available

https://angular.io/api/common/http/HttpStatusCode

Sign up to request clarification or add additional context in comments.

1 Comment

Unluckily it's a 'const enum', meaning it cannot be used at runtime to transform a http status code (e.g 401) to its corresponding text (Unauthorized) (here is the code: github.com/angular/angular/commit/6fe3a1d)
15

Unfortunately at this point in time there doesn't seem to be an enum or const for the exact error codes.

You'd be better off doing something like @Jeff suggested, by using a library like the http-status-codes which has them all listed and documented.

Angular's Response class allows you to check if a request was successful by means of the 'ok' property (boolean), otherwise you can access the status code/text from that object.

2 Comments

http-status-code library is very small and useful even in Angular 5.
It's not a bad library at all. With a majority of the instances the basic Angular Response class variables should be ok, but in the instance that you want to do checks for specific responses and have it done in a clean way, http-status-codes is the way to go

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.