nodebestpractices | js best practices list ( March | Runtime Evironment library
kandi X-RAY | nodebestpractices Summary
kandi X-RAY | nodebestpractices Summary
:white_check_mark: The Node.js best practices list (February 2022)
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Traverse MDAST
- Include references in a file
- Traverse all scripts in an external file .
- Gets config from github environment .
- inline images in html
- Fix md - references to markdown paths
- Fix href attributes to hash
- Promise writeFile
- Read file contents
- helper function to readdir
nodebestpractices Key Features
nodebestpractices Examples and Code Snippets
Community Discussions
Trending Discussions on nodebestpractices
QUESTION
I want to return the correct HTTP status code to the client when one of my services fails.
Lets assume we have a simple express app where a service getUserById(userId) {...}
is called from a controller. The service could fail for several reasons, e.g. an invalid user id, some bug in the code or simply because no user with the given id exists.
To return the correct HTTP status code (400, 500 and 404 respectively) I would somehow need to attach it to the error when I throw it from within my service. How do I do this and are there any best practices? Or did I misunderstand something entirely?
So far I have simply done it like this:
throw { message: 'No user with this ID exists', status: 404 }
I do feel however that this is not very maintainable considering that one should only throw standard errors.
...ANSWER
Answered 2020-May-15 at 21:34You can create an error object and then just add custom properties to it.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install nodebestpractices
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page