Comments (11)
Woops, accidentally closed and opened this when replying.
I'll revert for the moment surely. Thank you for taking the time to look into this issue.
from node-decorators.
Yeah, you just need to provide the class itself, DI will instantiate it.
Container.provide([
{ provide: ERROR_MIDDLEWARE, useValue: ServerErrorMiddleware }
]);
from node-decorators.
Have you set up the provider itself via ERROR_MIDDLEWARE?
from node-decorators.
To the best of my understanding, this is about how to implement the server error middleware?
import { Container, Injectable } from '@decorators/di';
import { ErrorMiddleware, ERROR_MIDDLEWARE } from '@decorators/express';
import { NextFunction, Request, Response } from 'express';
@Injectable()
export class ServerErrorMiddleware implements ErrorMiddleware {
public use(
error: Error,
request: Request,
response: Response,
next: NextFunction
) {
if (error) {
console.log(error);
response.status(500).send();
return;
}
next();
}
}
Container.provide([
{ provide: ERROR_MIDDLEWARE, useClass: ServerErrorMiddleware },
]);
from node-decorators.
Yup, does it generate that error with the provider too?
from node-decorators.
No error is shown in the Container.provide
call if that's what you're asking. The only type error is in the injection of it.
from node-decorators.
Could you revert back to version 4.* and I'll take a look?
from node-decorators.
I'm not sure why do you need to inject ServerErrorMiddleware
anywhere, but if you need to do so you need to add @Injectable
on top of it.
Here's the working example of providing ERROR_MIDDLEWARE
:
import * as express from 'express';
import { Container } from '@decorators/di';
import { attachControllers, Controller, ERROR_MIDDLEWARE, ErrorMiddleware, Get } from '../src';
const app: express.Express = express();
class NotFoundError extends Error {}
class InternalServerError extends Error {}
@Controller('/')
class IndexController {
@Get('/')
index() {
return 'Hello World';
}
@Get('/not-found-error')
notFoundError() {
throw new NotFoundError();
}
@Get('/internal-server-error')
internalServerError() {
throw new InternalServerError();
}
}
class ServerErrorMiddleware implements ErrorMiddleware {
use(error: Error, _: express.Request, response: express.Response, next: express.NextFunction) {
if (error instanceof NotFoundError) {
return response.send('Not Found Error');
}
if (error instanceof InternalServerError) {
return response.send('Internal Server Error');
}
next(error);
}
}
export async function start() {
Container.provide([
{ provide: ERROR_MIDDLEWARE, useClass: ServerErrorMiddleware },
]);
await attachControllers(app, [IndexController]);
app.listen(3000, () => {
console.info('Server is running on port 3000');
});
}
start().catch(console.error);
Note: it works both on TS 5.x and 4.x.
from node-decorators.
Gosh, I may have overthought the problem here. All you need to do to get the error middleware mounted globally is run the code block below?
Container.provide([
{ provide: ERROR_MIDDLEWARE, useValue: serverErrorMiddleware }
]);
Sorry about the miscommunication here. Thank you for the help.
from node-decorators.
Well I have a bunch of code I can delete. Much appreciated.
from node-decorators.
You're always welcome :)
from node-decorators.
Related Issues (20)
- Error while inject property in middelwares HOT 6
- Error injection HOT 18
- Where is attachMiddleware o how it change? HOT 2
- Where did the flexibility go?! HOT 11
- @Controller & Rest methods no longer work HOT 1
- Can't attach a controller without an error middleware HOT 17
- HttpModule is not exported HOT 21
- express-session is causing ERR_HTTP_HEADERS_SENT HOT 18
- Decorator error handling
- Missing data, EJS, express-ejs-layouts
- Implement OpenAPI nullable schema property
- Param validation HOT 13
- Which container instance should I use ? HOT 2
- elysia Js HOT 3
- [BUG] Inject() and Injectable() not working as expected - upgrade 2.x -> 3.x HOT 4
- [BUG] dependency injection constructor service initialize fail HOT 1
- Middleware called from another controller [BUG]
- [BUG] Error: ENOENT: no such file or directory
- [FEAT] Enable auth token globally in swagger - @decorators/server
- [BUG] Global middleware's use method is not being called
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 node-decorators.