-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathmicroservice.controller.ts
More file actions
49 lines (43 loc) · 1.47 KB
/
microservice.controller.ts
File metadata and controls
49 lines (43 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { Controller, UseGuards, UseInterceptors, UsePipes } from '@nestjs/common';
import { MessagePattern } from '@nestjs/microservices';
import * as Sentry from '@sentry/nestjs';
import { ExampleGuard } from './example.guard';
import { ExampleInterceptor } from './example.interceptor';
import { ExamplePipe } from './example.pipe';
@Controller()
export class MicroserviceController {
@MessagePattern({ cmd: 'sum' })
sum(data: { numbers: number[] }): number {
return Sentry.startSpan({ name: 'microservice-sum-operation' }, () => {
return data.numbers.reduce((a, b) => a + b, 0);
});
}
@MessagePattern({ cmd: 'exception' })
exception(data: { id: string }): never {
throw new Error(`Microservice exception with id ${data.id}`);
}
@MessagePattern({ cmd: 'manual-capture' })
manualCapture(): { success: boolean } {
try {
throw new Error('Manually captured microservice error');
} catch (e) {
Sentry.captureException(e);
}
return { success: true };
}
@UseGuards(ExampleGuard)
@MessagePattern({ cmd: 'test-guard' })
testGuard(): { result: string } {
return { result: 'guard-handled' };
}
@UseInterceptors(ExampleInterceptor)
@MessagePattern({ cmd: 'test-interceptor' })
testInterceptor(): { result: string } {
return { result: 'interceptor-handled' };
}
@UsePipes(ExamplePipe)
@MessagePattern({ cmd: 'test-pipe' })
testPipe(data: { value: number }): { result: number } {
return { result: data.value };
}
}