We’ve covered debugging errors in our code and unexpected errors in Lambda functions. Now let’s look at how to debug errors that happen outside our Lambda functions.

Initialization Errors

Lambda functions could fail not because of an error inside your handler code, but because of an error outside it. In this case, your Lambda function won’t be invoked. Let’s add some faulty code outside our handler function.

Change indicator Replace the main function in packages/functions/src/get.ts with the following.

// Some faulty code
dynamoDb.notExist();

export const main = handler(async (event: APIGatewayProxyEvent) => {
  let path_id

  if (!event.pathParameters || !event.pathParameters.id || event.pathParameters.id.length == 0) {
    throw new Error("Please provide the 'id' parameter.");
  } else {
    path_id = event.pathParameters.id
  }

  const params = {
    TableName: Table.Notes.tableName,
    // 'Key' defines the partition key and sort key of
    // the item to be retrieved
    Key: {
      userId: event.requestContext.authorizer?.iam.cognitoIdentity.identityId,
      noteId: path_id, // The id of the note from the path
    },
  };

  const result = await dynamoDb.get(params);
  if (!result.Item) {
    throw new Error("Item not found.");
  }

  // Return the retrieved item
  return result.Item;
});

Change indicator Commit this code.

$ git add .
$ git commit -m "Adding an init error"
$ git push

Head over to your Seed dashboard, and deploy it.

Now if you select a note in your notes app, you’ll notice that it fails with an error.

Init error in notes app note page

You should see an error in Sentry. And if you head over to the Issues in Seed and click on the new error.

Init error details in Seed

You’ll notice the error message dynamodb_lib.notExist is not a function.

Note that, you might see there are 3 events for this error. This is because the Lambda runtime prints out the error message multiple times.

Handler Function Errors

Another error that can happen outside a Lambda function is when the handler has been misnamed.

Change indicator Replace the main function in packages/functions/src/get.ts with the following.

// Wrong handler function name
export const main2 = handler(async (event: APIGatewayProxyEvent) => {
  let path_id

  if (!event.pathParameters || !event.pathParameters.id || event.pathParameters.id.length == 0) {
    throw new Error("Please provide the 'id' parameter.");
  } else {
    path_id = event.pathParameters.id
  }

  const params = {
    TableName: Table.Notes.tableName,
    // 'Key' defines the partition key and sort key of
    // the item to be retrieved
    Key: {
      userId: event.requestContext.authorizer?.iam.cognitoIdentity.identityId,
      noteId: path_id, // The id of the note from the path
    },
  };

  const result = await dynamoDb.get(params);
  if (!result.Item) {
    throw new Error("Item not found.");
  }

  // Return the retrieved item
  return result.Item;
});

Change indicator Let’s commit this.

$ git add .
$ git commit -m "Adding a handler error"
$ git push

Head over to your Seed dashboard and deploy it. Then, in your notes app, try and load a note. It should fail with an error alert.

Just as before, you’ll see the error in Sentry. Head over to the new error in Seed.

Handler error details in Seed

You should see the error Runtime.HandlerNotFound, along with message get.main is undefined or not exported.

And that about covers the main Lambda function errors. So the next time you see one of the above error messages, you’ll know what’s going on.

Remove the Faulty Code

Let’s cleanup all the faulty code.

Change indicator Replace packages/functions/src/get.ts with the following.

import handler from "@notes/core/handler";
import { APIGatewayProxyEvent } from 'aws-lambda';
import { Table } from "sst/node/table";
import dynamoDb from "@notes/core/dynamodb";

export const main = handler(async (event: APIGatewayProxyEvent) => {
  let path_id

  if (!event.pathParameters || !event.pathParameters.id || event.pathParameters.id.length == 0) {
    throw new Error("Please provide the 'id' parameter.");
  } else {
    path_id = event.pathParameters.id
  }

  const params = {
    TableName: Table.Notes.tableName,
    // 'Key' defines the partition key and sort key of
    // the item to be retrieved
    Key: {
      userId: event.requestContext.authorizer?.iam.cognitoIdentity.identityId,
      noteId: path_id, // The id of the note from the path
    },
  };

  const result = await dynamoDb.get(params);
  if (!result.Item) {
    throw new Error("Item not found.");
  }

  // Return the retrieved item
  return result.Item;
});

Commit and push the code.

$ git add .
$ git commit -m "Reverting faulty code"
$ git push

Head over to your Seed dashboard and deploy it.

Now let’s move on to debugging API Gateway errors.