Skip to content

Conversation

mw-advait
Copy link
Member

  • added support for errorHandler function.
  • covers express, nestjs and fastify web frameworks.
  • will be able to set errorHandler as a wrapper inside global errorHandler

Fastify:

const middleware = require('@middleware.io/node-apm');
const fastify = require('fastify')({ logger: true });

// Register Middleware.io error handler for Fastify
fastify.setErrorHandler((error, request, reply) => {
    middleware.errorHandler(error, request, reply);
    // Optionally, send a response
    reply.status(500).send({ error: error.message });
});

const start = async () => {
    try {
        await fastify.listen({ port: 8080, host: '0.0.0.0' });
        fastify.log.info(`E-commerce app listening at http://0.0.0.0:8080`);
    } catch (err) {
        fastify.log.error(err);
        process.exit(1);
    }
};
start();

Nest.Js

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { errorHandler } from '@middleware.io/node-apm';
import { Catch, ArgumentsHost, ExceptionFilter } from '@nestjs/common';

@Catch()
export class ExpressErrorAdapterFilter implements ExceptionFilter {
  catch(exception: any, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const req = ctx.getRequest();
    const res = ctx.getResponse();
    const next = ctx.getNext();
    // Call your existing Express-style error handler
    errorHandler(exception, req, res, next);
  }
}

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useGlobalFilters(new ExpressErrorAdapterFilter());
  await app.listen(3000);
}
bootstrap();

@mw-advait mw-advait force-pushed the mw-advait/AGE-334/master/profiling+ops-ai-k8s branch from 5d108fd to 8adc945 Compare September 23, 2025 09:36
@mw-advait mw-advait force-pushed the mw-advait/AGE-334/master/profiling+ops-ai-k8s branch from 8adc945 to 8f13b23 Compare October 8, 2025 04:53
@bhogayatakb bhogayatakb merged commit c70f0fd into master Oct 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants