Comments (11)
I suspect that the default unhandled exception handling isn't working since NestJS actually handles all exceptions by default ?
This is my solution:
Create an Interceptor:
@Injectable()
export class SentryReportingInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, call$) {
return call$
.pipe(catchError(error => {
withScope(scope => {
scope.addEventProcessor(event => parseRequest(event, context.switchToHttp().getRequest()));
captureException(error);
});
throw error;
}));
}
}
And configure it globally in your appModule:
providers: [
{
provide: APP_INTERCEPTOR,
useClass: SentryReportingInterceptor,
}
],
I think this library could use a construction like this to properly setup the request scope and handle NestJS exceptions.
from nestjs-sentry.
The SentryService extends the nestjs/logger. The configurations on the NestJs documentation site are implemented. Please refer to https://docs.nestjs.com/techniques/logger to see the different configuration options.
from nestjs-sentry.
Did you accomplish what you were looking to do?
from nestjs-sentry.
@ntegral Hi, I referred to the link you posted but I still have issue to have it working.
here is my config:
main.ts:
import { SentryService } from '@ntegral/nestjs-sentry';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useLogger(app.get(SentryService));
await app.listen(configService.get('PORT'));
}
app.module.ts:
import { Module } from '@nestjs/common';
import { SentryModule } from '@ntegral/nestjs-sentry';
import { LogLevel } from '@sentry/types';
...
@Module({
imports: [
SentryModule.forRootAsync({
useFactory: async (configService: ConfigurationService) => ({
dsn: configService.get('SENTRY_DSN'),
debug: configService.get('SENTRY_DEBUG') == 'true',
environment: configService.get('SENTRY_ENVIRONMENT'),
//release: 'some_release', | null, // must create a release in sentry.io dashboard
logLevel: LogLevel.Debug //based on sentry.io loglevel //
}),
inject: [ConfigurationService],
}),
...
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
When I look at my Sentry dashboard, I only see some info/warning message generated during app startup but none of the exceptions or JS Errors like null.push(''test')
from my graphql resolver are captured.
JS Error:
@Mutation(returns => User)
@UseInterceptors(PictureInterceptor)
async login(@Args() loginArgs: LoginArgs): Promise<User> {
const t = null;
t.push('l'); // <- throws an error
return this.authService.login(loginArgs)
}
Throw an error:
@Mutation(returns => User)
@UseInterceptors(PictureInterceptor)
async login(@Args() loginArgs: LoginArgs): Promise<User> {
throw new Error('Sentry test');
return this.authService.login(loginArgs)
}
Do you have any idea why? is It because i'm using GraphQL?
from nestjs-sentry.
I'll look into your issue.
This definitely works.. It allows you to send message to sentry.io based on your above example.
put the code inside a try/catch statement..
constructor(@InjectSentry() private readonly client: SentryService) {
client.error('test message');
}
from nestjs-sentry.
it works as expected when I inject the sentryService into the resolver constructor and I call client.error('test message');
but if I throw an Error, it does not work.
When I call client.error('test message');
from the GraphQL resolver, The event is logged twice:
To avoid the event to be logged twice I need to instantiate the Nest App like this:
const app = await NestFactory.create(AppModule, {
logger: false,
});
It is GraphQL related because when I throw an Error from a REST Controller, it is sent to Sentry. Only one event is sent to sentry even if logger is not disabled.
When an exception is thrown from a controller, the strack trace is not sent to Sentry:
@Get('/')
public async get() {
const t = null;
t.push('t');
}
Tell me if you want me to create different issues 😄
Thank you very much for your help.
I'm going to investigate why Error thrown in GraphQL resolver are not even captured my nest internal logger (nothing is printed to the console).
from nestjs-sentry.
@ntegral any updates on this?
from nestjs-sentry.
I created an issue in nestjs/graphql repo: nestjs/graphql#449 to help understanding what's going on
from nestjs-sentry.
Updating to the last @nestjs/core
version (6.9.0) fixed the issue.
Command to do it with nestcli: nest u -f
from nestjs-sentry.
from nestjs-sentry.
My pleasure, Thanks for your work!
from nestjs-sentry.
Related Issues (20)
- Custom exception does not work in SentryInterceptor HOT 4
- Sentry Profiling Integration doesn't work HOT 8
- Exception objectError does not work
- unmet peer rimraf@^3.0.2: found 4.1.2
- Make app name in sentryService customizable HOT 1
- Argument of type 'undefined' is not assignable to parameter of type 'string | symbol'
- Is this project still maintained? HOT 7
- nestjs 10 support HOT 5
- Getting error in the latest version of nestjs (v10) HOT 6
- Remove 'rimraf' unused dependency HOT 1
- Request for Updating 'reflect-metadata' Package Version in Project HOT 2
- Can not install in NestJS 9
- Official Sentry Nest.js support in `8.x` of the Sentry SDK HOT 4
- Cannot read properties of undefined (reading 'OnUncaughtException') HOT 3
- NestJs 9 Support HOT 7
- How to inject into another module? HOT 4
- Why does my Sentry errors look like this? HOT 1
- Add Performance Tracking HOT 3
- Integrate Sentry Prisma ORM HOT 2
- Update imports of `@sentry/hub` to use `@sentry/core`
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 nestjs-sentry.