Comments (8)
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.
@dcavanagh is anyone still maintaining inversify and other subpackages ?
from inversify-express-utils.
@PodaruDragos I have very recently been added as maintainer and I am going to start working through the backlog
from inversify-express-utils.
Will there be an actual solution? I found that throwing an error works fine but not calling next
from inversify-express-utils.
Any update on this? The error handler doesn't seems to be working for me too.
from inversify-express-utils.
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.
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.
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)
- Injection scope for controller (singleton instead request scoped)
- Per-controller error handling HOT 1
- Release of a new npm version with peer dependency Express 4.17.0+ HOT 5
- Breaking change was introduced - interfaces.ts no longer exports an interface object HOT 3
- Latest inversify-express-utils is throwing build errors HOT 3
- Inversify Express controller inheritance fail if inheriting from more than 1 controller
- [inversify-express-utils] - No support for @controller on inevrfisy-binding-decorators? HOT 1
- Error trying to instantiate controller in a test (Error: No matching bindings found for serviceIdentifier: Symbol(HttpContext)) HOT 1
- inversify-express-utils, support of express 4.17.1 HOT 1
- [Inversify Express Utils] Controller is Reconstructed on Every Request HOT 1
- Inversify-Express-Untils Cannot Build Multiple Servers HOT 1
- Is inversify-express-utils no longer under maintenance? HOT 1
- integrating inversify-express-utils with inversify-socket-utils
- inversify-express-utils: BaseHttpController's conflict should take an argument
- [inversify-express-utils] AuthProvider Unhandled Promise Rejection HOT 1
- [inversify-express-utils] allow to inject dependencies into middleware constructor HOT 2
- Adding route config while creating server doesn't assign mount path to baseurl property of express request object
- JsonContent doesn't send correctly HOT 7
- Can not inject dependency in my CustomAuthProvider HOT 1
- Breaking changes from TypeScript update HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from inversify-express-utils.