|
| 1 | +import { Controller, Get, Param, ParseIntPipe, UseGuards, UseInterceptors } from '@nestjs/common'; |
| 2 | +import { AppService } from './app.service'; |
| 3 | +import { ExampleException } from './example.exception'; |
| 4 | +import { ExampleGuard } from './example.guard'; |
| 5 | +import { ExampleInterceptor } from './example.interceptor'; |
| 6 | +import { ScheduleService } from './schedule.service'; |
| 7 | + |
| 8 | +@Controller() |
| 9 | +export class AppController { |
| 10 | + public constructor( |
| 11 | + private readonly appService: AppService, |
| 12 | + private readonly scheduleService: ScheduleService, |
| 13 | + ) {} |
| 14 | + |
| 15 | + @Get('test-transaction') |
| 16 | + public testTransaction(): unknown { |
| 17 | + return this.appService.testSpan(); |
| 18 | + } |
| 19 | + |
| 20 | + @Get('test-middleware') |
| 21 | + public testMiddleware(): unknown { |
| 22 | + return this.appService.testSpan(); |
| 23 | + } |
| 24 | + |
| 25 | + @Get('test-guard') |
| 26 | + @UseGuards(ExampleGuard) |
| 27 | + public testGuard(): unknown { |
| 28 | + return {}; |
| 29 | + } |
| 30 | + |
| 31 | + @Get('test-interceptor') |
| 32 | + @UseInterceptors(ExampleInterceptor) |
| 33 | + public testInterceptor(): unknown { |
| 34 | + return this.appService.testSpan(); |
| 35 | + } |
| 36 | + |
| 37 | + @Get('test-pipe/:id') |
| 38 | + public testPipe(@Param('id', ParseIntPipe) id: number): unknown { |
| 39 | + return { value: id }; |
| 40 | + } |
| 41 | + |
| 42 | + @Get('test-exception') |
| 43 | + public testException(): never { |
| 44 | + throw new ExampleException(); |
| 45 | + } |
| 46 | + |
| 47 | + @Get('test-event') |
| 48 | + public testEvent(): unknown { |
| 49 | + this.appService.emitEvent(); |
| 50 | + return { message: 'emitted' }; |
| 51 | + } |
| 52 | + |
| 53 | + // Triggers the `@Timeout`-decorated handler directly (its real delay is long |
| 54 | + // so it never fires on its own during the test). |
| 55 | + @Get('trigger-timeout-error') |
| 56 | + public triggerTimeoutError(): unknown { |
| 57 | + try { |
| 58 | + this.scheduleService.handleTimeoutError(); |
| 59 | + } catch { |
| 60 | + // Swallow, the error is captured by the schedule instrumentation; the |
| 61 | + // route itself should still succeed. |
| 62 | + } |
| 63 | + return { message: 'triggered' }; |
| 64 | + } |
| 65 | + |
| 66 | + // Stop the auto-firing scheduled jobs so they don't keep throwing after the |
| 67 | + // assertions have run. |
| 68 | + @Get('kill-schedules') |
| 69 | + public killSchedules(): unknown { |
| 70 | + this.scheduleService.killCron('test-cron-error'); |
| 71 | + this.scheduleService.killInterval('test-interval-error'); |
| 72 | + return { message: 'killed' }; |
| 73 | + } |
| 74 | +} |
0 commit comments