Tip #47[HttpStatusCode enum]
Published in
Nov 14, 2021
Let’s assume that you want to fetch a resource having a unique identifier. If a server returns a response with 404 status code, you may want to return a fallback value, otherwise let the error propagate:
It works as expected, however it’s not a good idea to introduce magic number in your codebase, therefore it’s recommended to extract the 404 status code into a constant:
If you use Angular, you don’t need to create and maintain the file containing http status codes, since a corresponding enum (HttpStatusCode) is available out of the box:
Live example:
If you like the tip, please give me some applause 👏