DEV Community

Anjan Talatam
Anjan Talatam

Posted on

2

How to handle unmatched routes with serverless

Context:

I mistakenly misspelled my login route as /dev/loginn instead of /dev/login on Postman, here is the response

{
    "currentRoute": "post - /dev/loginn",
    "error": "Serverless-offline: route not found.",
    "existingRoutes": [...],
    "statusCode": 404
}
Enter fullscreen mode Exit fullscreen mode

Problem

The above response has 2 major issues

  1. Existing routes are getting exposed which shouldn't be (ideally).
  2. The response is too detailed. We need to keep it simple.

Fix

Add a new Lambda function to handle unmatched/ unconfigured Routes in serverless.yml

#serverless.yml

functions:
  unmatchedRoute:
    handler: handler.unmatchedRoute
    events:
      - http:
          path: /{proxy+}
          method: ANY
Enter fullscreen mode Exit fullscreen mode
// handler.ts

import { Handler } from 'aws-lambda';

export const unmatchedRoute: Handler = async () => {
  return {
    statusCode: 404,
    body: '404 page not found',
  };
};
Enter fullscreen mode Exit fullscreen mode

Thanks for reading! Drop a like if you found it helpful ❤️

Cheers! Anjan Talatam


Ignore from here

SEO

  1. How to handle unconfigured routes with serverless
  2. How to handle unmatched routes for AWS lambda functions
  3. How to handle unconfigured routes for AWS lambda functions
  4. How to handle unconfigured routes for AWS lambda functions with serverless

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay