#Handling GraphQLError that gets thrown in a global middleware

1 messages · Page 1 of 1 (latest)

spare inlet
#

I have a AuthMiddleware that is registered in the AppModule like this consumer.apply(AuthMiddleware).forRoutes('*');
The auth is done in the AuthMiddleware, if the auth fails, an error of type GraphQLError is thrown. This can be caught in a try catch which solves the issue. But we are currently trying to get the default Error handler of the GraphQL Engine in the GraphQLModule to automatically format and handle this. However, if the error thrown(line 25) in the AuthMiddleware is not caught, it will indefinitely hang the application for some reason. Any ideas? I have attached some images below.

glacial flumeBOT
#

:warning: Please do not screenshot code as it causes a number of issues:

  • Ease of assistance: if someone wants to copy your code and correct it, they cannot. Making it easy for people to help you is in your best interests.
  • Editorializing: it's common to try to make images small, which means you're likely to crop out code relevant to your issue
  • Accessibility: wide images can be hard to read on mobile devices, and are impossible for screen readers.
  • Legibility: you cannot read screenshots of code directly, instead you have to open them in an enlarged context.
  • Bandwidth usage/clutter: some of our members use metered connections, and it is wasteful for them to download images of a text.

For a small amount of code, please use a code-block.

spare inlet
#

hello sorry heres the text version

export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(PlexusAuthMiddleware).forRoutes('*');
  }
}
@Injectable()
export class PlexusAuthMiddleware implements NestMiddleware {
  private plexusConnectionManager: PlexusConnectionManager;

  constructor(plexusConnectionManager: PlexusConnectionManager) {
    this.plexusConnectionManager = plexusConnectionManager;
  }

  async use(req: Request, res: Response, next: NextFunction) {
    const authInfo = AuthUtils.decodeAuthInfo(req.header(Constants.authorizationHeaderKey));
    Logger.log(LogLevel.error, `XXXXX ${authInfo}`);
    if (authInfo) {
      Logger.log(LogLevel.error, `XXXXX ${authInfo} ${authInfo.accessToken} ${authInfo.clientId} ${authInfo.clientSecret} ${authInfo.idToken}`);
      const plexusConnection = await this.plexusConnectionManager.getConnection(authInfo);
      Logger.log(LogLevel.error, `XXXXX - 1 ${plexusConnection.getX()}`);
  
      await plexusConnection.connect(authInfo);
      await plexusConnection.authenticate(authInfo);
  
      Logger.log(LogLevel.error, `XXXXX - 2 ${plexusConnection.getX()}`);
  
      req.plexusConnection = plexusConnection;
  
      next();
    } else {
      throw new ForbiddenException();
    }
  }
}