Node JSPowerShell

Exception Handling for Node.Js Application fused with PowerShell | Part 1

Yeah, it’s me again with Node.js and PowerShell but this time for exception handling series of blogs and this is part 1 – Handling Exceptions are must in development practice right? But, how we output to end users matters a lot. For example consider the below error

Cannot GET /hello

There is no such route (hello) exists and it’s clear for developers. Let us add few piece of code to show friendly error message with email link to report the issue when end user lands in non existing endpoints or in application.

Server.js Code is illustrated below

Here the last route is

For any non existing routes we render the NotFound.pug file for which the code is as shown below

so ultimately our first result will be like

All good! No, still we need to work bit more on code. Before the JS let us see how we can play with PowerShell exceptions. Yes, for this I used node-powershell package and don’t worry child_process works the same way! For a demo, the script file I created is Exception1.ps1 under scripts folder and code is as shown below

And I call Exception.ps1 reproduce the error – Do observe the file name (Exception1 and Exception) called in server.js is incorrect

Hurts me badly!

Ahhhhhhh! A phone rings saying “Hey Chen I don’t know Chinese! Can you read and make it clear for me?”

That’s bad right? As a first step If we correct the file name we are out of funkiness but again the exception we catch in PS throws “Attempted to divide by zero“. So, we need to render a pug file with more information.

With minimum effort we can give more information to L1 support engineers and end users will not panic looking into the issue. In my next blog post we will continue to explore more about exception handling!

Enjoy PowerShell 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *