Skip to content

fix: Default error handler doesn't work with express session/useExpressServer #653

@jddunleavy

Description

@jddunleavy

Description

When attempting to use express-session for session management (with the intention of making use of the @Session decorator) Errors thrown within routes are no longer being handled by the default error handler. In the code snippet below when I remove express-session the error gets returned correctly to the caller, but with the session configured the node process crashes with the throw HTTPError.

As an aside I would say there's very little documentation on how I'm supposed to configure sessions with Routing-controllers, so there's a reasonable chance this isn't the intended/supported way. Given the mention of express-session in the documentation I assume this is the supported express session manager!

Minimal code-snippet showcasing the problem

import express from "express";
import session from "express-session";
import {
  Get,
  HttpError,
  JsonController,
  useExpressServer,
} from "routing-controllers";

@JsonController()
class MainController {
  @Get("/")
  get() {
    throw new HttpError(500, "whoops");
  }

  // Curiously when the controller has only one route this issue
  // doesn't occur
  @Get("/another")
  getAnother() {
    return "another";
  }
}

const app = express();

// Commenting out this block makes it work again
app.use(
  session({
    secret: "23ruhiw",
    resave: true,
    saveUninitialized: true,
  })
);

useExpressServer(app, {
  controllers: [MainController],
});

app.listen(5000);

Expected behavior

  1. Call the endpoint hosted on /
  2. Receive JSON response:
{
  "name": "HttpError",
  "message": "whoops",
  "stack": "Error: \n    at new HttpError..."
}

Actual behavior

  1. Call the endpoint hosted on /
  2. Node crashes with a series of the HTTPError objects logged to the console (10 times to be specific)

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: needs triageIssues which needs to be reproduced to be verified report.type: fixIssues describing a broken feature.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions