InPart 1, error handling was created. Let's make it easier to throw errors.
Instead of
leterror=newError('This is a major issue!!')error.statusCode=500throwerror
We will make it something like this
thrownewServerError({message:'This is a major issue!!'})
- Create a new folderexceptions
CreateServerError.js in the exceptions folder
classServerErrorextendsError{constructor(resource){super()this.name=this.constructor.name// good practicethis.statusCode=500this.message=resource?resource.message:'Server Error'}}module.exports={ServerError}
Editindex.js, on how to use it
// index.js...const{ServerError}=require('./exceptions/ServerError')...app.get('/error',async(req,res,next)=>{try{thrownewServerError()// or// throw new ServerError({ message: 'A huge mistake happened!' })}catch(error){returnnext(error)}})...
Now go to
localhost:3000/error
to see the 500 server error
Top comments(0)
Subscribe
For further actions, you may consider blocking this person and/orreporting abuse