Comments (3)
See packages/core/exceptions/base-exception-filter.ts in the NestJS source code:
public handleUnknownError(
exception: T,
host: ArgumentsHost,
applicationRef: AbstractHttpAdapter | HttpServer,
) {
const body = this.isHttpError(exception)
? {
statusCode: exception.statusCode,
message: exception.message,
}
: {
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
message: MESSAGES.UNKNOWN_EXCEPTION_MESSAGE,
};
const response = host.getArgByIndex(1);
if (!applicationRef.isHeadersSent(response)) {
applicationRef.reply(response, body, body.statusCode);
} else {
applicationRef.end(response);
}
if (this.isExceptionObject(exception)) {
return BaseExceptionFilter.logger.error(
exception.message,
exception.stack,
);
}
return BaseExceptionFilter.logger.error(exception);
}
public isExceptionObject(err: any): err is Error {
return isObject(err) && !!(err as Error).message;
}
If your error is an instance of Error, then the error logs a concatenation of the error message and stack, not the Error object itself.
from nest.
@sinapirani that means we need to have our own filter if we want to get the Error object 🤔
Should we really rely on that? For me, many people want to use their custom logger (like Winston, Pino, etc) and they have their way to parse the error logs. That means if they want to catch this logs, they need to implements LoggerService
+ NestInterceptor
or an exception filter?
My current solution was to build something like this:
@Injectable()
export class ErrorInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<unknown> {
return next.handle().pipe(
catchError((error) => {
logger.error(error);
return throwError(() => {
if (error instanceof HttpException) {
return error;
}
return new HttpException(
{
statusCode: 500,
message: 'Internal server error',
},
500,
{ cause: error },
);
});
}),
);
}
}
from nest.
That's the expected behavior.
From the looks of it, this could be better discussed on our Discord. If you haven't already, please join here and send a new post in the # 🐈 nestjs-help forum. Make sure to include a link to this issue, so you don't need to write it all again. We have a large community of helpful members, who will assist you in getting this to work.
from nest.
Related Issues (20)
- @fastify/view version in @nestjs/platform-fastify needs to be updated HOT 1
- Docs Improvements: Samples Node Version HOT 2
- Pipe validation for `@UploadFiles()` not working HOT 1
- @nestjs/platform-ws: Application shutdown hangs
- feat: add rdkafka support
- Calling start() on grpc client throws DeprecationWarning after @grpc/grpc-js v1.10.0 HOT 1
- Service Injection Issue in Guard Across Modules HOT 3
- Example Deno+nest project with working tests
- Base exception filter ignores exception cause HOT 2
- TypeError: _compression is not a function when using compression middleware in NestJS 10.3.3 and SWC as TypeScript compiler HOT 1
- Autowire dependencies without annotations HOT 2
- bug : cannot use KY with latest NestJS HOT 1
- Path aliases / relative imports in a monorepo breaks `nest start` HOT 3
- ValidationPipe({transform: true}) transforms non-numeric strings to NaN HOT 2
- Throwing an exception still leads to a crash HOT 11
- Exclude version prefix for specific routes HOT 3
- If HttpAdapterHost is a provider, is it part of the current ClsModule? HOT 1
- Add support for fastify-passport package
- Express Middleware not being called for some endpoints HOT 1
- Some CLI commands do not work in project root directory HOT 2
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 nest.