@biorate/nestjs-tools 2.5.2 → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,46 +12,44 @@ const common_1 = require("@nestjs/common");
12
12
  const errors_1 = require("../errors");
13
13
  let AllExceptionsFilter = class AllExceptionsFilter {
14
14
  catch(exception, host) {
15
- const type = host.getType();
16
- const method = `_${type}`;
15
+ this.type = host.getType();
16
+ this.exception = exception;
17
+ this.host = host;
18
+ const method = `_${this.type}`;
17
19
  if (!this[method])
18
- throw new errors_1.UnsupportedProtocolError(host.getType());
19
- this[method](exception, host);
20
+ throw new errors_1.UnsupportedProtocolError(this.type);
21
+ this[method]();
20
22
  }
21
- _http(exception, host) {
22
- const ctx = host.switchToHttp();
23
+ _http() {
24
+ const ctx = this.host.switchToHttp();
23
25
  const response = ctx.getResponse();
24
26
  const request = ctx.getRequest();
25
- const meta = exception?.meta;
26
- const status = exception instanceof common_1.HttpException
27
- ? exception.getStatus()
27
+ const meta = this.exception?.meta;
28
+ const status = this.exception instanceof common_1.HttpException
29
+ ? this.exception.getStatus()
28
30
  : meta?.status ?? common_1.HttpStatus.INTERNAL_SERVER_ERROR;
29
- this.log(exception);
30
- response.status(status).json({
31
+ this.log(this.exception);
32
+ response.status(status).json(this.format({
31
33
  status,
32
- code: this.code(exception),
33
- hint: this.hint(exception),
34
34
  path: request.url,
35
35
  meta: meta?.meta,
36
- });
36
+ }));
37
37
  }
38
- _ws(exception, host) {
39
- const ctx = host.switchToWs();
38
+ _ws() {
39
+ const ctx = this.host.switchToWs();
40
40
  const socket = ctx.getClient();
41
- const meta = exception?.meta;
41
+ const meta = this.exception?.meta;
42
42
  const status = meta?.status ?? common_1.HttpStatus.INTERNAL_SERVER_ERROR;
43
- this.log(exception);
43
+ this.log(this.exception);
44
44
  socket.send(JSON.stringify({
45
45
  event: 'error',
46
- data: {
46
+ data: this.format({
47
47
  status,
48
- code: this.code(exception),
49
- hint: this.hint(exception),
50
48
  meta: meta?.meta,
51
- },
49
+ }),
52
50
  }));
53
51
  }
54
- _rpc(exception, host) {
52
+ _rpc() {
55
53
  throw new errors_1.UnsupportedProtocolError('rpc');
56
54
  }
57
55
  code(exception) {
@@ -60,6 +58,13 @@ let AllExceptionsFilter = class AllExceptionsFilter {
60
58
  hint(exception) {
61
59
  return (exception?.getResponse?.()?.message ?? exception.message);
62
60
  }
61
+ format(data) {
62
+ return {
63
+ code: this.code(this.exception),
64
+ hint: this.hint(this.exception),
65
+ ...data,
66
+ };
67
+ }
63
68
  log(exception) {
64
69
  if (exception instanceof axios_1.AxiosError)
65
70
  console.error(new errors_1.AxiosRequestError(exception?.response?.data?.statusCode ?? exception.status, exception?.response?.data?.error ?? exception.code, exception?.response?.data?.message ?? exception.message).stack);
@@ -9,46 +9,44 @@ import { Catch, HttpException, HttpStatus, } from '@nestjs/common';
9
9
  import { AxiosRequestError, UnsupportedProtocolError } from '../errors.js';
10
10
  let AllExceptionsFilter = class AllExceptionsFilter {
11
11
  catch(exception, host) {
12
- const type = host.getType();
13
- const method = `_${type}`;
12
+ this.type = host.getType();
13
+ this.exception = exception;
14
+ this.host = host;
15
+ const method = `_${this.type}`;
14
16
  if (!this[method])
15
- throw new UnsupportedProtocolError(host.getType());
16
- this[method](exception, host);
17
+ throw new UnsupportedProtocolError(this.type);
18
+ this[method]();
17
19
  }
18
- _http(exception, host) {
19
- const ctx = host.switchToHttp();
20
+ _http() {
21
+ const ctx = this.host.switchToHttp();
20
22
  const response = ctx.getResponse();
21
23
  const request = ctx.getRequest();
22
- const meta = exception?.meta;
23
- const status = exception instanceof HttpException
24
- ? exception.getStatus()
24
+ const meta = this.exception?.meta;
25
+ const status = this.exception instanceof HttpException
26
+ ? this.exception.getStatus()
25
27
  : meta?.status ?? HttpStatus.INTERNAL_SERVER_ERROR;
26
- this.log(exception);
27
- response.status(status).json({
28
+ this.log(this.exception);
29
+ response.status(status).json(this.format({
28
30
  status,
29
- code: this.code(exception),
30
- hint: this.hint(exception),
31
31
  path: request.url,
32
32
  meta: meta?.meta,
33
- });
33
+ }));
34
34
  }
35
- _ws(exception, host) {
36
- const ctx = host.switchToWs();
35
+ _ws() {
36
+ const ctx = this.host.switchToWs();
37
37
  const socket = ctx.getClient();
38
- const meta = exception?.meta;
38
+ const meta = this.exception?.meta;
39
39
  const status = meta?.status ?? HttpStatus.INTERNAL_SERVER_ERROR;
40
- this.log(exception);
40
+ this.log(this.exception);
41
41
  socket.send(JSON.stringify({
42
42
  event: 'error',
43
- data: {
43
+ data: this.format({
44
44
  status,
45
- code: this.code(exception),
46
- hint: this.hint(exception),
47
45
  meta: meta?.meta,
48
- },
46
+ }),
49
47
  }));
50
48
  }
51
- _rpc(exception, host) {
49
+ _rpc() {
52
50
  throw new UnsupportedProtocolError('rpc');
53
51
  }
54
52
  code(exception) {
@@ -57,6 +55,13 @@ let AllExceptionsFilter = class AllExceptionsFilter {
57
55
  hint(exception) {
58
56
  return (exception?.getResponse?.()?.message ?? exception.message);
59
57
  }
58
+ format(data) {
59
+ return {
60
+ code: this.code(this.exception),
61
+ hint: this.hint(this.exception),
62
+ ...data,
63
+ };
64
+ }
60
65
  log(exception) {
61
66
  if (exception instanceof AxiosError)
62
67
  console.error(new AxiosRequestError(exception?.response?.data?.statusCode ?? exception.status, exception?.response?.data?.error ?? exception.code, exception?.response?.data?.message ?? exception.message).stack);
@@ -1,10 +1,17 @@
1
- import { ExceptionFilter, ArgumentsHost } from '@nestjs/common';
1
+ import { ExceptionFilter, ArgumentsHost, ContextType } from '@nestjs/common';
2
2
  export declare class AllExceptionsFilter implements ExceptionFilter {
3
+ protected type: ContextType;
4
+ protected exception: Error;
5
+ protected host: ArgumentsHost;
3
6
  catch(exception: Error, host: ArgumentsHost): void;
4
7
  private _http;
5
8
  private _ws;
6
9
  private _rpc;
7
10
  private code;
8
11
  private hint;
12
+ protected format(data: Record<string, unknown>): {
13
+ code: string;
14
+ hint: string | [string];
15
+ };
9
16
  protected log(exception: Error): void;
10
17
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@biorate/nestjs-tools",
3
- "version": "2.5.2",
3
+ "version": "2.6.0",
4
4
  "description": "NestJS tools",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "module": "./dist/esm/index.js",
@@ -45,7 +45,7 @@
45
45
  "keywords": [],
46
46
  "author": "llevkin",
47
47
  "license": "MIT",
48
- "gitHead": "53c9b4fbcae2fd034c3ba129a5cbe52f4b80bff3",
48
+ "gitHead": "ba34f02a95796fee72c9dc822e31eaa740e09b4a",
49
49
  "peerDependencies": {
50
50
  "@nestjs/common": ">=10.4.15",
51
51
  "@nestjs/core": ">=10.4.15",