• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nestjs / nest / f7142b24-e394-4028-89c6-717e281431d1

25 Nov 2024 01:12PM UTC coverage: 90.141% (-1.8%) from 91.989%
f7142b24-e394-4028-89c6-717e281431d1

Pull #14177

circleci

web-flow
Merge pull request #14200 from nestjs/feat/allow-queue-per-handler

feat(microservices): support nats queue per handler
Pull Request #14177: release: version 11.0.0

2612 of 3236 branches covered (80.72%)

496 of 704 new or added lines in 48 files covered. (70.45%)

79 existing lines in 11 files now uncovered.

6985 of 7749 relevant lines covered (90.14%)

16.31 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.97
/packages/websockets/exceptions/base-ws-exception-filter.ts
1
import {
1✔
2
  ArgumentsHost,
3
  IntrinsicException,
4
  Logger,
5
  WsExceptionFilter,
6
} from '@nestjs/common';
7
import { isObject } from '@nestjs/common/utils/shared.utils';
1✔
8
import { MESSAGES } from '@nestjs/core/constants';
1✔
9
import { WsException } from '../errors/ws-exception';
1✔
10

11
export interface ErrorPayload<Cause = { pattern: string; data: unknown }> {
12
  /**
13
   * Error message identifier.
14
   */
15
  status: 'error';
16
  /**
17
   * Error message.
18
   */
19
  message: string;
20
  /**
21
   * Message that caused the exception.
22
   */
23
  cause?: Cause;
24
}
25

26
interface BaseWsExceptionFilterOptions {
27
  /**
28
   * When true, the data that caused the exception will be included in the response.
29
   * This is useful when you want to provide additional context to the client, or
30
   * when you need to associate the error with a specific request.
31
   * @default true
32
   */
33
  includeCause?: boolean;
34

35
  /**
36
   * A factory function that can be used to control the shape of the "cause" object.
37
   * This is useful when you need a custom structure for the cause object.
38
   * @default (pattern, data) => ({ pattern, data })
39
   */
40
  causeFactory?: (pattern: string, data: unknown) => Record<string, any>;
41
}
42

43
/**
44
 * @publicApi
45
 */
46
export class BaseWsExceptionFilter<TError = any>
1✔
47
  implements WsExceptionFilter<TError>
48
{
49
  protected static readonly logger = new Logger('WsExceptionsHandler');
1✔
50

51
  constructor(protected readonly options: BaseWsExceptionFilterOptions = {}) {
28✔
52
    this.options.includeCause = this.options.includeCause ?? true;
28✔
53
    this.options.causeFactory =
28✔
54
      this.options.causeFactory ?? ((pattern, data) => ({ pattern, data }));
2✔
55
  }
56

57
  public catch(exception: TError, host: ArgumentsHost) {
58
    const client = host.switchToWs().getClient();
6✔
59
    const pattern = host.switchToWs().getPattern();
6✔
60
    const data = host.switchToWs().getData();
6✔
61
    this.handleError(client, exception, {
6✔
62
      pattern,
63
      data,
64
    });
65
  }
66

67
  public handleError<TClient extends { emit: Function }>(
68
    client: TClient,
69
    exception: TError,
70
    cause: ErrorPayload['cause'],
71
  ) {
72
    if (!(exception instanceof WsException)) {
6✔
73
      return this.handleUnknownError(exception, client, cause);
2✔
74
    }
75

76
    const status = 'error';
4✔
77
    const result = exception.getError();
4✔
78

79
    if (isObject(result)) {
4✔
80
      return client.emit('exception', result);
2✔
81
    }
82

83
    const payload: ErrorPayload<unknown> = {
2✔
84
      status,
85
      message: result,
86
    };
87

88
    if (this.options?.includeCause) {
2✔
89
      payload.cause = this.options.causeFactory(cause.pattern, cause.data);
1✔
90
    }
91

92
    client.emit('exception', payload);
2✔
93
  }
94

95
  public handleUnknownError<TClient extends { emit: Function }>(
96
    exception: TError,
97
    client: TClient,
98
    data: ErrorPayload['cause'],
99
  ) {
100
    const status = 'error';
2✔
101
    const payload: ErrorPayload<unknown> = {
2✔
102
      status,
103
      message: MESSAGES.UNKNOWN_EXCEPTION_MESSAGE,
104
    };
105

106
    if (this.options?.includeCause) {
2✔
107
      payload.cause = this.options.causeFactory(data.pattern, data.data);
1✔
108
    }
109

110
    client.emit('exception', payload);
2✔
111

112
    if (!(exception instanceof IntrinsicException)) {
2!
113
      const logger = BaseWsExceptionFilter.logger;
2✔
114
      logger.error(exception);
2✔
115
    }
116
  }
117

118
  public isExceptionObject(err: any): err is Error {
UNCOV
119
    return isObject(err) && !!(err as Error).message;
×
120
  }
121
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc