to reproduce:
- degit 'https://github.com/ianzone/vite-nest-fastify#filter-error' to-check
- cd to-check
- pnpm i
- pnpm run dev
- GET http://localhost:3000/error
and it crashes...
14 messages · Page 1 of 1 (latest)
to reproduce:
Print log to here
And why did you added Scope.Request to Exception Filter?
You can inject ModuleRef to constructor
the filter is not scoped in the branch i attached, i mean filter-error branch
import { ArgumentsHost, Catch, HttpException, Injectable, Logger, Scope } from '@nestjs/common';
import { BaseExceptionFilter,ContextIdFactory,ModuleRef } from '@nestjs/core';
import { FastifyReply, FastifyRequest } from 'fastify';
import { RequestService } from './services';
// https://docs.nestjs.com/exception-filters#exception-filters
@Catch()
export class AppFilter extends BaseExceptionFilter {
private readonly logger = new Logger(AppFilter.name);
constructor(private readonly moduleRef: ModuleRef) {
super();
}
async catch(exception: Error, host: ArgumentsHost) {
const req = host.switchToHttp().getRequest<FastifyRequest>();
const res = host.switchToHttp().getResponse<FastifyReply>();
const contextId = ContextIdFactory.getByRequest(req);
const reqService = await moduleRef.resolve(RequestService,contextId);
const ctx = {
Method: req.method,
Path: req.url,
Params: req.params,
Query: req.query,
Headers: req.headers,
Body: req.body,
ReqAuxData: reqService.getAuxData(),
};
let statusCode = 500;
let message = '';
if (exception instanceof HttpException) {
this.logger.warn(ctx, exception.stack);
statusCode = exception.getStatus();
// @ts-ignore
message = exception.getResponse()?.message;
} else {
// unexpected errors that need to trigger alerts
this.logger.error(ctx, exception.stack);
message = exception.message;
}
res.status(statusCode).send({ ...reqService.getLogTrace(), message });
}
}
I Rewrite Exceptions Filter and remove @Injectable
/home/ian/templates/vite-nest/node_modules/.pnpm/@nestjs+core@9.3.9_jrq2rdgfp2sx67wmylmrqliwxe/node_modules/@nestjs/core/injector/instance-links-host.js:24
throw new unknown_element_exception_1.UnknownElementException(this.getInstanceNameByToken(token));
^
UnknownElementException [Error]: Nest could not find RequestService element (this provider does not exist in the current context)
at InstanceLinksHost.get (/home/ian/templates/vite-nest/node_modules/.pnpm/@nestjs+core@9.3.9_jrq2rdgfp2sx67wmylmrqliwxe/node_modules/@nestjs/core/injector/instance-links-host.js:24:19)
at ModuleRef.resolvePerContext (/home/ian/templates/vite-nest/node_modules/.pnpm/@nestjs+core@9.3.9_jrq2rdgfp2sx67wmylmrqliwxe/node_modules/@nestjs/core/injector/abstract-instance-resolver.js:23:38)
at ModuleRef.resolve (/home/ian/templates/vite-nest/node_modules/.pnpm/@nestjs+core@9.3.9_jrq2rdgfp2sx67wmylmrqliwxe/node_modules/@nestjs/core/injector/module.js:409:29)
at AppFilter1.catch (/src/app.filter.ts:23:49)
at ExceptionsHandler.invokeCustomFilters (/home/ian/templates/vite-nest/node_modules/.pnpm/@nestjs+core@9.3.9_jrq2rdgfp2sx67wmylmrqliwxe/node_modules/@nestjs/core/exceptions/exceptions-handler.js:33:26)
at ExceptionsHandler.next (/home/ian/templates/vite-nest/node_modules/.pnpm/@nestjs+core@9.3.9_jrq2rdgfp2sx67wmylmrqliwxe/node_modules/@nestjs/core/exceptions/exceptions-handler.js:13:18)
at /home/ian/templates/vite-nest/node_modules/.pnpm/@nestjs+core@9.3.9_jrq2rdgfp2sx67wmylmrqliwxe/node_modules/@nestjs/core/router/router-proxy.js:13:35
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Node.js v18.12.0
ELIFECYCLE Command failed with exit code 1.
that's what i updated in the filter-error branch https://github.com/ianzone/vite-nest-fastify/tree/filter-error
still crashed
is this a bug?