Giter Club home page Giter Club logo

Comments (8)

nref-dan avatar nref-dan commented on June 3, 2024 2

We are also having this issue. It appears to only affect the last listed method in the controller class.

I have found 2 workarounds but these go against the documentation of calling return next(err);.

Workaround 1 (throw the error, seems a bit pointless try/catch to throw, but you may want to manipulate the error before you send it up):

try {
  const val = await serviceCallAsync();
  res.status(200).send(val);
} catch(err) {
  throw err;
}

Workaround 2 (utilise the Promises):

try {
  const val = await serviceCallAsync();
  Promise.resolve(val);
} catch(err) {
  return Promise.reject(err);
}

We haven't been able to figure out where the problem lies but it could be something to do with the decorators.

from inversify-express-utils.

PodaruDragos avatar PodaruDragos commented on June 3, 2024

@dcavanagh is anyone still maintaining inversify and other subpackages ?

from inversify-express-utils.

Jameskmonger avatar Jameskmonger commented on June 3, 2024

@PodaruDragos I have very recently been added as maintainer and I am going to start working through the backlog

from inversify-express-utils.

sittingbool avatar sittingbool commented on June 3, 2024

Will there be an actual solution? I found that throwing an error works fine but not calling next

from inversify-express-utils.

SuspiciousLookingOwl avatar SuspiciousLookingOwl commented on June 3, 2024

Any update on this? The error handler doesn't seems to be working for me too.

from inversify-express-utils.

ctalvarez avatar ctalvarez commented on June 3, 2024

I'm having the same issue. I have this configuration to handle some method error with setErrorConfig and sometimes the server responds with status 204 No content, instead of the actual json and status. It always happens with the same endpoint, and with most of the others endpoints works fine, returning the corresponding error status

inversifyServer.setErrorConfig((app) => {
  app.use((err, req, res, _next) => {
    }
    // const jsonResponse ={ ... }
    return res.status(err.status || 500).json(jsonResponse);
  });


@httpPost(
    '/foo',
    'SomeMiddleware',
  )
  async Foo(req: Request, res: Response, next: NextFunction) {
    try {
      // do something
      return res.status(201).json();
    } catch (err) {
      return next(err);
    }
  }

The error in the console is Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
It looks like the response is sent before accessing the setErrorConfig callback.

from inversify-express-utils.

Mannydheer avatar Mannydheer commented on June 3, 2024

I'm having a super weird issue as well with this...

To give some context,

This is my implementation of overriding the default error handler:

export const errorHandler = (err: Error, req: Request, res: Response, next: NextFunction) => {
  //We check if the error is coming from our custom error objects.

  if (err instanceof HttpException) {
    return res.status(err?.statusCode).json({
      message: err?.message,
      errorName: err?.name,
      stack: err?.stack,
    });
  }
  //Errors that a unexpected and not handled by our custom errors.
  return res.status(500).json({
    message: err?.message,
    errorName: err?.name,
    stack: err?.stack,
  });

};

I have the following endpoint, let's call it endpoint1

 @httpPost('/refreshAccessToken')
  public async refreshAccessToken(req: Request, res: Response, next: NextFunction): Promise<any> {
    try {
      if (!req.body.refreshToken || !req.body.accessToken || !req.body.id || !req.body.email) {
        throw new BadRequestError('Missing fields')
      }
      return res.status(200).json({status: 200,message: 'Success getting new access token with refresh token.',});
    } catch (error) {
      next(error);
    }
  }

and then just below I have this empty endpoint, let's call this endpoint2.

  @httpPost('')
  public async() {
  }

If I have both endpoint1 and endpoint2, everything works fine, endpoint1 will throw a BadRequestError which will get caught in the catch block and then will be delegated to the errorHandler middleware.

As soon as I remove endpoint2, I get a 204 no-content and the following message:

ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

I have no idea what is happening...

from inversify-express-utils.

arudenkoofficial avatar arudenkoofficial commented on June 3, 2024

I confirm the reports from @nref-dan and @Mannydheer
The error occurs for the last method listed in the controller.

Would be great to see some updates on this problem

from inversify-express-utils.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.