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

var express = require('express'),
    app = express(),
    pug = require('pug'),
    path = require('path'),
    bodyparser = require('body-parser');

app.use(bodyparser.urlencoded({ extended: true }));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

app.get("/", function (request, response) {
    response.send("<h1>Landing Page1</h1>")

app.get("/HelloWorld", function (request, response) {
    response.send("<h1>Hello World</h1>")

app.use(function (request, response) {

console.log("Your application is running!")

Here the last route is

app.use(function (request, response) {

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

<!DOCTYPE html>
        meta(name="viewport", content="width=device-width, initial-scale=1.0")
        meta(http-equiv="X-UA-Compatible", content="ie=edge")
        title Document
        b Sorry! Page not found!
        a(href='') Mail Support Desk

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

try {
    1 / 0 
catch [DivideByZeroException] {
    $_.Exception | ConvertTo-Json -Compress
catch [System.Net.WebException], [System.Exception] {
    $_.Exception | ConvertTo-Json -Compress

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

app.get("/Exception1", function (request, response) {
    ps.invoke().then(output => {

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 *