I remember seeing in a article that try / catch blocks are not minified, haven't tested it out yet, but it's something to be careful about.
While you can use try / catch blocks, note that:
The async function returns a Promise
You usually would have the request handler declared in a controller file and have services functions handle the operations.
Promises can handle errors.
So you could also do things like this:
// user/services/user.service.getAll.tsexportdefaultasyncfunctiongetAllUsers(filter:string,page:number):Promixe<IUser[]>{//...}// user/controllers/user.get.controller.ts/*
* controller documentation...
*/asyncfunctionuserGetController(req,res,next):Promise<void>{let{filter,page}=req.query;filter=filter||'';page=parseInt(page)||1;constusers=awaitgetAllUsers(filter,page).then(users=>res.json(users)).catch(functiongetAllUsersControllerError(e){letstatus=500;leterrorObject:IApiError={//...}constreturnedApiError=!!e.status;if(returnedApiError){status=e.status;errorObject=e;}res.status(status).json(errorObject);});}// user/user.router.tsapp.get('/users',userGetController);// or if you want to be even more safeapp.get('/users',functionuserGetRoute(req,res,next):Promise<void>{returnuserGetController(req,res,next).catch(e=>{// do something...});});```
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
some things to note here:
try / catch
blocks are not minified, haven't tested it out yet, but it's something to be careful about.try / catch
blocks, note that:async
function returns aPromise
So you could also do things like this: