Giter Club home page Giter Club logo

Comments (11)

sbesh91 avatar sbesh91 commented on September 25, 2024 1

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.

serhiisol avatar serhiisol commented on September 25, 2024 1

Yeah, you just need to provide the class itself, DI will instantiate it.

Container.provide([
  { provide: ERROR_MIDDLEWARE, useValue: ServerErrorMiddleware }
]);

from node-decorators.

serhiisol avatar serhiisol commented on September 25, 2024

Have you set up the provider itself via ERROR_MIDDLEWARE?

from node-decorators.

sbesh91 avatar sbesh91 commented on September 25, 2024

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.

serhiisol avatar serhiisol commented on September 25, 2024

Yup, does it generate that error with the provider too?

from node-decorators.

sbesh91 avatar sbesh91 commented on September 25, 2024

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.

serhiisol avatar serhiisol commented on September 25, 2024

Could you revert back to version 4.* and I'll take a look?

from node-decorators.

serhiisol avatar serhiisol commented on September 25, 2024

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.

sbesh91 avatar sbesh91 commented on September 25, 2024

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.

sbesh91 avatar sbesh91 commented on September 25, 2024

Well I have a bunch of code I can delete. Much appreciated.

from node-decorators.

serhiisol avatar serhiisol commented on September 25, 2024

You're always welcome :)

from node-decorators.

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.