I found an issue in the production environment where I had written an async function that has a try catch block. Inside the try block what I did was I made the database call to get session details from database. If the session details are not found then I am throwing a not found exception from within the try block. Now, inside the catch block for error if error has response that means if from within the above try block I have thrown a not found exception then it should throw the same error message back as response.
But when the session details are not found then the application throws that error on the console and then the application restarts completely. My IDE shows the following warning when throwing inside the try block. **"'throw' of exception caught locally
Inspection info: Reports a throw statement whose exceptions are always caught by the containing try statement. Using throw statements as a goto to change the local flow of control is confusing." ** Is it not allowed to throw an exception from within try block? If not then what is a good way of writing this where I am still able to convey to frontend the reason for not able to proceed in this function.
Kindly, help me. Also if there are any reference guides on writing good quality exception handler then it will be of a lot of help. Please find the photo I have attached which contains the function.
#use of throw inside of try catch block crashes NestJS Application.
1 messages · Page 1 of 1 (latest)
it all depends on who is calling your function
if that's isn't in nestjs request life-cycle, then it won't catch any exceptions
can you show all occurrences of endMasterClassRecordings?
sure
I will write the entire user flow
There is a service we are using that sends us a webhook in the form of HTTPS POST request with the event name. On recording-stop event I call recording stop function. This is also an async function. Recording stop function then calls endMasterClassRecordings function.
As a response to the webhook I just have to send a HTTP STATUS 200 with OK message to let their server know I have received their request. I still have to use async await because I have not implemented Queues to store all the requests.
I get the following error message and the application stops responding
recording stopped Webhook hit......
Update the existing masterclass recording row to end the masterclass recording...
/Users/vihang/Developer/BHyve/backend/bhyve-lms/src/masterclass-webhooks-actions/masterclass-webhooks-actions.service.ts:214
throw new InternalServerErrorException(
^
InternalServerErrorException: Failed to add session recordings for video sdk session public id 613731342f27f56e4fc4b6d0
at MasterclassWebhooksActionsService.recordingStop (/Users/vihang/Developer/BHyve/backend/bhyve-lms/src/masterclass-webhooks-actions/masterclass-webhooks-actions.service.ts:214:17)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at MasterclassVideoSdkWebhooksService.storeWebHooksData (/Users/vihang/Developer/BHyve/backend/bhyve-lms/src/masterclass-video-sdk-webhooks/masterclass-video-sdk-webhooks.service.ts:159:20)
[6:55:12 PM] File change detected. Starting incremental compilation...
So I can see the message that I have used to throw inside the try block. But then why does the app restart in production
since you have another throw in the catch path, we'll need to see who's calling recordingStop
I have written a controller
That is called by an external API as a part of webhook response. When an event occurs at their end. They call this API. Which calls the service this.masterclassVideoSdkWebhooksService.storeWebHooksData
This contains a switch case that calls different functions based on the events that come in the POST request from the third party service.
Now in this example recording-stopped is the event. Hence it calls /data controller and then storeWebHooksData in the service. The switch case calls recordingStop function.
you missed the await
that's why you got an unhandled promise rejection