|
1 | 1 | /* eslint-disable @typescript-eslint/unbound-method */ |
2 | 2 | import type { ArgumentsHost } from '@nestjs/common'; |
3 | 3 | import { HttpException, HttpStatus, Logger } from '@nestjs/common'; |
| 4 | +import { BaseExceptionFilter } from '@nestjs/core'; |
4 | 5 | import * as SentryCore from '@sentry/core'; |
5 | 6 | import { beforeEach, describe, expect, it, vi } from 'vitest'; |
6 | 7 | import * as Helpers from '../src/helpers'; |
@@ -322,4 +323,50 @@ describe('SentryGlobalFilter', () => { |
322 | 323 | expect(mockLoggerError).toHaveBeenCalledWith(error.message, error.stack); |
323 | 324 | }); |
324 | 325 | }); |
| 326 | + |
| 327 | + describe('Necord context', () => { |
| 328 | + beforeEach(() => { |
| 329 | + vi.mocked(mockArgumentsHost.getType).mockReturnValue('necord'); |
| 330 | + }); |
| 331 | + |
| 332 | + it('captures unexpected errors without delegating to the HTTP exception filter', () => { |
| 333 | + const superCatchSpy = vi.spyOn(BaseExceptionFilter.prototype, 'catch').mockImplementation(() => undefined); |
| 334 | + const error = new Error('Slash command failed'); |
| 335 | + |
| 336 | + filter.catch(error, mockArgumentsHost); |
| 337 | + |
| 338 | + expect(mockCaptureException).toHaveBeenCalledWith(error, { |
| 339 | + mechanism: { |
| 340 | + handled: false, |
| 341 | + type: 'auto.necord.nestjs.global_filter', |
| 342 | + }, |
| 343 | + }); |
| 344 | + expect(mockLoggerError).toHaveBeenCalledWith(error.message, error.stack); |
| 345 | + expect(superCatchSpy).not.toHaveBeenCalled(); |
| 346 | + }); |
| 347 | + |
| 348 | + it('does not capture expected Necord exceptions', () => { |
| 349 | + isExpectedErrorMock.mockReturnValueOnce(true); |
| 350 | + const exception = new HttpException('Unknown interaction', HttpStatus.BAD_REQUEST); |
| 351 | + |
| 352 | + filter.catch(exception, mockArgumentsHost); |
| 353 | + |
| 354 | + expect(mockCaptureException).not.toHaveBeenCalled(); |
| 355 | + expect(mockLoggerError).toHaveBeenCalledWith(exception.message, exception.stack); |
| 356 | + }); |
| 357 | + |
| 358 | + it('captures unexpected non-Error values', () => { |
| 359 | + const nonErrorObject = { message: 'interaction failed' }; |
| 360 | + |
| 361 | + filter.catch(nonErrorObject, mockArgumentsHost); |
| 362 | + |
| 363 | + expect(mockCaptureException).toHaveBeenCalledWith(nonErrorObject, { |
| 364 | + mechanism: { |
| 365 | + handled: false, |
| 366 | + type: 'auto.necord.nestjs.global_filter', |
| 367 | + }, |
| 368 | + }); |
| 369 | + expect(mockLoggerError).not.toHaveBeenCalled(); |
| 370 | + }); |
| 371 | + }); |
325 | 372 | }); |
0 commit comments