npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

lambda-exceptions

v1.0.0

Published

An npm package for catching all the exceptions thrown by the code base, log and send error response to the client

Downloads

2

Readme

An easy way to handle and log Lambda exceptions

An npm package for catching all the exceptions thrown by the code base, log and send error response to the client

For a Lambda function that uses extenral services like DynamoDB, ElasticCache, MySQL it is common practice that we try/catch all the places where we anticipate the service may fail at one time. Instead of this we have created a wrapper for the hander so that, if any error triggers it will catch that and send proper response to the client.

Also we can specify the logger in the configuration of this exception handler function so that we can decide where we can save the error logs.

Here we are using aws-lambda as a dependency package.

This package has one wrapper function httpErrorHandler for exception handling and class GenericError which is extending Error class added for throwing custom errors in case.

An example code implementation is below.

import { httpErrorHandler } from 'lambda-exceptions';
import { httpGatewayEvent } from './data/http-gateway-event';
import { APIGatewayProxyResult, APIGatewayEvent, Context } from 'aws-lambda';
import { Employee } from './employee.entity';

const wrongTrackHandler = async (
  event: APIGatewayEvent
): Promise<APIGatewayProxyResult> => {
  const employee = new Employee('John', 30, 1000);
  const employeeInfo = await employee.getNonExistentEmployee();

  return {
    statusCode: 200,
    body: JSON.stringify(employeeInfo),
  };
};

const rightTrackHandler = async (
  event: APIGatewayEvent
): Promise<APIGatewayProxyResult> => {
  const employee = new Employee('Emily', 3, 1000);
  const employeeInfo = await employee.getEmployee();

  return {
    statusCode: 200,
    body: JSON.stringify(employeeInfo),
  };
};

const customLogger = (message: string) => {
  console.log('---------Error starts-------------');
  console.log(message);
  console.log('---------Error end-------------');
};

describe('Lambda error handler', () => {
  it('Error handler testing', async () => {
    const trackHandler = httpErrorHandler(customLogger)(wrongTrackHandler);

    const response = await trackHandler(httpGatewayEvent, {} as Context);
    expect(response.statusCode).to.equal(404);
  });

  it('Correct handler testing', async () => {
    const trackHandler = httpErrorHandler()(rightTrackHandler);

    const response = await trackHandler(httpGatewayEvent, {} as Context);
    expect(response.statusCode).to.equal(200);
    expect(JSON.parse(response.body)).to.have.property('name');
  });
});

In the above code you can see, I define customLogger using console.log and passed to the exception wrapper as logger. Else it will take default console.log

const trackHandler = httpErrorHandler(customLogger)(wrongTrack);

GenericError implementation example is

import { GenericError } from 'lambda-exceptions';

class Employee {
  private id!: string;
  private name: string;
  private age: number;
  private salary: number;

  constructor(name: string, age: number, salary: number) {
    this.name = name;
    this.age = age;
    this.salary = salary;
  }

  async getEmployee(): Promise<Employee> {
    const found = false;
    if (!found) {
      throw new GenericError('Employee not found', 404);
    }
  }
}

errorStack will get logged into the logger based on the configuration you passed through.