@nest-yalc-2/errors 2.2.2 → 2.2.4
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.
- package/package.json +11 -11
- package/src/__tests__/database-exception.filter.spec.ts +71 -71
- package/src/__tests__/http-exception.filter.spec.ts +182 -182
- package/src/__tests__/system-exception.filter.spec.ts +39 -39
- package/src/__tests__/validation-exception.filter.spec.ts +31 -31
- package/src/filters/database-exception.filter.ts +45 -45
- package/src/filters/http-exception.filter.ts +144 -144
- package/src/filters/index.ts +6 -6
- package/src/filters/system-exception.filter.ts +13 -13
- package/src/filters/validation-exception.filter.ts +26 -26
- package/src/index.ts +8 -8
- package/src/__tests__/default.error.spec.ts +0 -529
- package/src/__tests__/error.helper.spec.ts +0 -23
- package/src/__tests__/errors.enum.spec.ts +0 -32
- package/src/__tests__/http-status-code-to-errors.spec.ts +0 -26
- package/src/__tests__/input-validation.error.spec.ts +0 -27
- package/src/__tests__/login.error.spec.ts +0 -23
- package/src/__tests__/result.error.spec.ts +0 -47
- package/src/__tests__/verification.error.spec.ts +0 -28
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
import {
|
|
2
|
-
expect,
|
|
3
|
-
jest,
|
|
4
|
-
describe,
|
|
5
|
-
it,
|
|
6
|
-
beforeEach,
|
|
7
|
-
beforeAll,
|
|
8
|
-
afterAll,
|
|
9
|
-
afterEach,
|
|
10
|
-
} from '@jest/globals';
|
|
11
|
-
import { createMock } from '@golevelup/ts-jest';
|
|
12
|
-
import { LoggerService } from '@nestjs/common';
|
|
13
|
-
import { ValidationExceptionFilter } from '../filters/validation-exception.filter.js';
|
|
14
|
-
import { InputValidationError } from '../index.js';
|
|
15
|
-
import { CrudGenError } from '@nest-yalc-2/crud-gen/crud-gen.error.js';
|
|
16
|
-
|
|
17
|
-
describe('ValidationExceptionFilter', () => {
|
|
18
|
-
const logger = createMock<LoggerService>();
|
|
19
|
-
it('should received error to InputValidationError', () => {
|
|
20
|
-
const filter = new ValidationExceptionFilter(logger);
|
|
21
|
-
const result = filter.catch(new Error());
|
|
22
|
-
expect(result).toBeInstanceOf(InputValidationError);
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
it('should received error to CrudGenError', () => {
|
|
26
|
-
const error: CrudGenError = new CrudGenError('message', 'systemMessage');
|
|
27
|
-
const filter = new ValidationExceptionFilter(logger);
|
|
28
|
-
filter.catch(error);
|
|
29
|
-
expect(logger.error).toHaveBeenCalledWith(error.systemMessage, error.stack);
|
|
30
|
-
});
|
|
31
|
-
});
|
|
1
|
+
import {
|
|
2
|
+
expect,
|
|
3
|
+
jest,
|
|
4
|
+
describe,
|
|
5
|
+
it,
|
|
6
|
+
beforeEach,
|
|
7
|
+
beforeAll,
|
|
8
|
+
afterAll,
|
|
9
|
+
afterEach,
|
|
10
|
+
} from '@jest/globals';
|
|
11
|
+
import { createMock } from '@golevelup/ts-jest';
|
|
12
|
+
import { LoggerService } from '@nestjs/common';
|
|
13
|
+
import { ValidationExceptionFilter } from '../filters/validation-exception.filter.js';
|
|
14
|
+
import { InputValidationError } from '../index.js';
|
|
15
|
+
import { CrudGenError } from '@nest-yalc-2/crud-gen/crud-gen.error.js';
|
|
16
|
+
|
|
17
|
+
describe('ValidationExceptionFilter', () => {
|
|
18
|
+
const logger = createMock<LoggerService>();
|
|
19
|
+
it('should received error to InputValidationError', () => {
|
|
20
|
+
const filter = new ValidationExceptionFilter(logger);
|
|
21
|
+
const result = filter.catch(new Error());
|
|
22
|
+
expect(result).toBeInstanceOf(InputValidationError);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('should received error to CrudGenError', () => {
|
|
26
|
+
const error: CrudGenError = new CrudGenError('message', 'systemMessage');
|
|
27
|
+
const filter = new ValidationExceptionFilter(logger);
|
|
28
|
+
filter.catch(error);
|
|
29
|
+
expect(logger.error).toHaveBeenCalledWith(error.systemMessage, error.stack);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
@@ -1,45 +1,45 @@
|
|
|
1
|
-
import { GqlExceptionFilter, GqlArgumentsHost } from '@nestjs/graphql';
|
|
2
|
-
import * as common from '@nestjs/common';
|
|
3
|
-
// We can import more errors from the typeorm/error folder if necessary, nothing is exported though, so use explicit paths.
|
|
4
|
-
import { EntityNotFoundError, ConnectionNotFoundError } from 'typeorm';
|
|
5
|
-
import { ExceptionContextEnum } from '@node-yalc/errors/error.enum.js';
|
|
6
|
-
|
|
7
|
-
import type { LoggerService } from '@nestjs/common';
|
|
8
|
-
|
|
9
|
-
@common.Catch(EntityNotFoundError, ConnectionNotFoundError)
|
|
10
|
-
export class DatabaseExceptionFilter implements GqlExceptionFilter {
|
|
11
|
-
constructor(private logger: LoggerService) {}
|
|
12
|
-
|
|
13
|
-
catch(error: Error, host: common.ArgumentsHost) {
|
|
14
|
-
const gqlHost = GqlArgumentsHost.create(host);
|
|
15
|
-
gqlHost.getType();
|
|
16
|
-
|
|
17
|
-
// Normally transactions are for performance monitoring,
|
|
18
|
-
// Op and Name reflect which operation and function we are running
|
|
19
|
-
// Could not get issue tracking working without, using exception filter as op.
|
|
20
|
-
// const transaction = Sentry.startTransaction({
|
|
21
|
-
// op: 'DatabaseExceptionFilter', // operation (encode)
|
|
22
|
-
// name: 'Caught Error', // name (parseAvatarImage)
|
|
23
|
-
// });
|
|
24
|
-
|
|
25
|
-
// TODO: Validate when monitoring is in place if we need an eventEmitter here instead of log
|
|
26
|
-
switch (true) {
|
|
27
|
-
// Re-throw a new 500 HTTP-friendly error
|
|
28
|
-
// Add more TypeORM errors here, because there's no base Error class
|
|
29
|
-
// @url: https://github.com/typeorm/typeorm/issues/3479
|
|
30
|
-
case error instanceof EntityNotFoundError:
|
|
31
|
-
this.logger.error(error, ExceptionContextEnum.DATABASE);
|
|
32
|
-
error = new common.InternalServerErrorException(error);
|
|
33
|
-
// Sentry.captureException(error);
|
|
34
|
-
break;
|
|
35
|
-
// Log critically any other error, because those are not expected normally
|
|
36
|
-
default:
|
|
37
|
-
this.logger.error(error, error.stack, ExceptionContextEnum.DATABASE);
|
|
38
|
-
// Sentry.captureException(error);
|
|
39
|
-
break;
|
|
40
|
-
}
|
|
41
|
-
// transaction.finish();
|
|
42
|
-
|
|
43
|
-
return error;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
1
|
+
import { GqlExceptionFilter, GqlArgumentsHost } from '@nestjs/graphql';
|
|
2
|
+
import * as common from '@nestjs/common';
|
|
3
|
+
// We can import more errors from the typeorm/error folder if necessary, nothing is exported though, so use explicit paths.
|
|
4
|
+
import { EntityNotFoundError, ConnectionNotFoundError } from 'typeorm';
|
|
5
|
+
import { ExceptionContextEnum } from '@node-yalc/errors/error.enum.js';
|
|
6
|
+
|
|
7
|
+
import type { LoggerService } from '@nestjs/common';
|
|
8
|
+
|
|
9
|
+
@common.Catch(EntityNotFoundError, ConnectionNotFoundError)
|
|
10
|
+
export class DatabaseExceptionFilter implements GqlExceptionFilter {
|
|
11
|
+
constructor(private logger: LoggerService) {}
|
|
12
|
+
|
|
13
|
+
catch(error: Error, host: common.ArgumentsHost) {
|
|
14
|
+
const gqlHost = GqlArgumentsHost.create(host);
|
|
15
|
+
gqlHost.getType();
|
|
16
|
+
|
|
17
|
+
// Normally transactions are for performance monitoring,
|
|
18
|
+
// Op and Name reflect which operation and function we are running
|
|
19
|
+
// Could not get issue tracking working without, using exception filter as op.
|
|
20
|
+
// const transaction = Sentry.startTransaction({
|
|
21
|
+
// op: 'DatabaseExceptionFilter', // operation (encode)
|
|
22
|
+
// name: 'Caught Error', // name (parseAvatarImage)
|
|
23
|
+
// });
|
|
24
|
+
|
|
25
|
+
// TODO: Validate when monitoring is in place if we need an eventEmitter here instead of log
|
|
26
|
+
switch (true) {
|
|
27
|
+
// Re-throw a new 500 HTTP-friendly error
|
|
28
|
+
// Add more TypeORM errors here, because there's no base Error class
|
|
29
|
+
// @url: https://github.com/typeorm/typeorm/issues/3479
|
|
30
|
+
case error instanceof EntityNotFoundError:
|
|
31
|
+
this.logger.error(error, ExceptionContextEnum.DATABASE);
|
|
32
|
+
error = new common.InternalServerErrorException(error);
|
|
33
|
+
// Sentry.captureException(error);
|
|
34
|
+
break;
|
|
35
|
+
// Log critically any other error, because those are not expected normally
|
|
36
|
+
default:
|
|
37
|
+
this.logger.error(error, error.stack, ExceptionContextEnum.DATABASE);
|
|
38
|
+
// Sentry.captureException(error);
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
// transaction.finish();
|
|
42
|
+
|
|
43
|
+
return error;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -1,144 +1,144 @@
|
|
|
1
|
-
import { GqlExceptionFilter } from '@nestjs/graphql';
|
|
2
|
-
import * as common from '@nestjs/common';
|
|
3
|
-
import type { HttpServer } from '@nestjs/common';
|
|
4
|
-
import {
|
|
5
|
-
DefaultErrorMixin,
|
|
6
|
-
MissingArgumentsError,
|
|
7
|
-
isDefaultErrorMixin,
|
|
8
|
-
formatCause,
|
|
9
|
-
} from '@nest-yalc-2/errors';
|
|
10
|
-
import {
|
|
11
|
-
EntityError,
|
|
12
|
-
isEntityError,
|
|
13
|
-
} from '@nest-yalc-2/crud-gen/entity.error.js';
|
|
14
|
-
import { FastifyReply as FResponse } from 'fastify';
|
|
15
|
-
import { GqlError } from '@nest-yalc-2/graphql/plugins/gql.error.js';
|
|
16
|
-
import { BaseExceptionFilter } from '@nestjs/core';
|
|
17
|
-
import { getLogLevelByStatus } from '../../../event-manager/src/event.helper.js';
|
|
18
|
-
import { LogLevelEnum } from '@node-yalc/logger/logger.enum.js';
|
|
19
|
-
import { type ImprovedLoggerService } from '@node-yalc/logger/logger-abstract.service.js';
|
|
20
|
-
|
|
21
|
-
type HttpErrorType =
|
|
22
|
-
common.HttpException | MissingArgumentsError | GqlError | DefaultErrorMixin;
|
|
23
|
-
@common.Catch(
|
|
24
|
-
common.HttpException,
|
|
25
|
-
MissingArgumentsError,
|
|
26
|
-
GqlError,
|
|
27
|
-
DefaultErrorMixin,
|
|
28
|
-
)
|
|
29
|
-
export class HttpExceptionFilter
|
|
30
|
-
extends BaseExceptionFilter
|
|
31
|
-
implements GqlExceptionFilter, common.ExceptionFilter
|
|
32
|
-
{
|
|
33
|
-
constructor(
|
|
34
|
-
protected logger: ImprovedLoggerService,
|
|
35
|
-
applicationRef?: HttpServer,
|
|
36
|
-
) {
|
|
37
|
-
super(applicationRef);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
catch(
|
|
41
|
-
error: Error | HttpErrorType,
|
|
42
|
-
host: common.ArgumentsHost,
|
|
43
|
-
{ sendResponse }: { sendResponse: boolean } = { sendResponse: true },
|
|
44
|
-
) {
|
|
45
|
-
try {
|
|
46
|
-
const isHttpError =
|
|
47
|
-
this.isHttpError(error) || error instanceof common.HttpException;
|
|
48
|
-
// TODO: Validate when monitoring is in place if we need an eventEmitter here instead of log
|
|
49
|
-
switch (true) {
|
|
50
|
-
// Base logging for normal operation execution errors
|
|
51
|
-
case error instanceof MissingArgumentsError:
|
|
52
|
-
this.logger.log(error.message, {
|
|
53
|
-
stack: error.stack,
|
|
54
|
-
});
|
|
55
|
-
break;
|
|
56
|
-
|
|
57
|
-
case isDefaultErrorMixin(error):
|
|
58
|
-
// no need to log, DefaultErrorMixin already logs
|
|
59
|
-
break;
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Log original error message (for now only if is an EntityError)
|
|
63
|
-
* @todo refactor with the default error mixin
|
|
64
|
-
*/
|
|
65
|
-
case isEntityError(error):
|
|
66
|
-
{
|
|
67
|
-
const entityError = error as EntityError;
|
|
68
|
-
this.logger.error(
|
|
69
|
-
entityError.originalError?.message
|
|
70
|
-
? entityError.originalError.message
|
|
71
|
-
: error,
|
|
72
|
-
entityError.originalError?.stack,
|
|
73
|
-
{
|
|
74
|
-
stack: entityError.originalError?.stack,
|
|
75
|
-
data: {
|
|
76
|
-
response: entityError.getResponse(),
|
|
77
|
-
name: entityError.name,
|
|
78
|
-
cause: formatCause(entityError.cause),
|
|
79
|
-
},
|
|
80
|
-
},
|
|
81
|
-
);
|
|
82
|
-
}
|
|
83
|
-
break;
|
|
84
|
-
|
|
85
|
-
case isHttpError:
|
|
86
|
-
{
|
|
87
|
-
const httpError = error as common.HttpException;
|
|
88
|
-
const logLevel = getLogLevelByStatus(httpError.getStatus());
|
|
89
|
-
|
|
90
|
-
if (logLevel === LogLevelEnum.ERROR) {
|
|
91
|
-
this.logger[logLevel](error.message, error.stack, {
|
|
92
|
-
stack: httpError.stack,
|
|
93
|
-
data: {
|
|
94
|
-
response: httpError.getResponse(),
|
|
95
|
-
name: error.name,
|
|
96
|
-
cause: formatCause(error.cause),
|
|
97
|
-
},
|
|
98
|
-
});
|
|
99
|
-
} else {
|
|
100
|
-
this.logger[logLevel](error.message, {
|
|
101
|
-
stack: error.stack,
|
|
102
|
-
data: {
|
|
103
|
-
response: httpError.getResponse(),
|
|
104
|
-
name: error.name,
|
|
105
|
-
cause: formatCause(error.cause),
|
|
106
|
-
},
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
break;
|
|
111
|
-
|
|
112
|
-
case error instanceof GqlError:
|
|
113
|
-
this.logger.error((<GqlError>error).systemMessage ?? error.message);
|
|
114
|
-
break;
|
|
115
|
-
|
|
116
|
-
// Log critically any other error, as those are not expected
|
|
117
|
-
default:
|
|
118
|
-
this.logger.error(error.message, error.stack, {
|
|
119
|
-
stack: error.stack,
|
|
120
|
-
data: {
|
|
121
|
-
cause: formatCause(error.cause),
|
|
122
|
-
name: error.name,
|
|
123
|
-
},
|
|
124
|
-
});
|
|
125
|
-
break;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
if (host.getType() === 'http') {
|
|
129
|
-
if (sendResponse) {
|
|
130
|
-
const ctx = host.switchToHttp();
|
|
131
|
-
const response = ctx.getResponse<FResponse>();
|
|
132
|
-
let status = common.HttpStatus.INTERNAL_SERVER_ERROR;
|
|
133
|
-
if (isHttpError) status = (error as common.HttpException).getStatus();
|
|
134
|
-
return response.status(status).send(error.message);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
} catch (e) {
|
|
138
|
-
// eslint-disable-next-line no-console
|
|
139
|
-
console.error(e); // if there's an error with the logger itself
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
return error;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
1
|
+
import { GqlExceptionFilter } from '@nestjs/graphql';
|
|
2
|
+
import * as common from '@nestjs/common';
|
|
3
|
+
import type { HttpServer } from '@nestjs/common';
|
|
4
|
+
import {
|
|
5
|
+
DefaultErrorMixin,
|
|
6
|
+
MissingArgumentsError,
|
|
7
|
+
isDefaultErrorMixin,
|
|
8
|
+
formatCause,
|
|
9
|
+
} from '@nest-yalc-2/errors';
|
|
10
|
+
import {
|
|
11
|
+
EntityError,
|
|
12
|
+
isEntityError,
|
|
13
|
+
} from '@nest-yalc-2/crud-gen/entity.error.js';
|
|
14
|
+
import { FastifyReply as FResponse } from 'fastify';
|
|
15
|
+
import { GqlError } from '@nest-yalc-2/graphql/plugins/gql.error.js';
|
|
16
|
+
import { BaseExceptionFilter } from '@nestjs/core';
|
|
17
|
+
import { getLogLevelByStatus } from '../../../event-manager/src/event.helper.js';
|
|
18
|
+
import { LogLevelEnum } from '@node-yalc/logger/logger.enum.js';
|
|
19
|
+
import { type ImprovedLoggerService } from '@node-yalc/logger/logger-abstract.service.js';
|
|
20
|
+
|
|
21
|
+
type HttpErrorType =
|
|
22
|
+
common.HttpException | MissingArgumentsError | GqlError | DefaultErrorMixin;
|
|
23
|
+
@common.Catch(
|
|
24
|
+
common.HttpException,
|
|
25
|
+
MissingArgumentsError,
|
|
26
|
+
GqlError,
|
|
27
|
+
DefaultErrorMixin,
|
|
28
|
+
)
|
|
29
|
+
export class HttpExceptionFilter
|
|
30
|
+
extends BaseExceptionFilter
|
|
31
|
+
implements GqlExceptionFilter, common.ExceptionFilter
|
|
32
|
+
{
|
|
33
|
+
constructor(
|
|
34
|
+
protected logger: ImprovedLoggerService,
|
|
35
|
+
applicationRef?: HttpServer,
|
|
36
|
+
) {
|
|
37
|
+
super(applicationRef);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
catch(
|
|
41
|
+
error: Error | HttpErrorType,
|
|
42
|
+
host: common.ArgumentsHost,
|
|
43
|
+
{ sendResponse }: { sendResponse: boolean } = { sendResponse: true },
|
|
44
|
+
) {
|
|
45
|
+
try {
|
|
46
|
+
const isHttpError =
|
|
47
|
+
this.isHttpError(error) || error instanceof common.HttpException;
|
|
48
|
+
// TODO: Validate when monitoring is in place if we need an eventEmitter here instead of log
|
|
49
|
+
switch (true) {
|
|
50
|
+
// Base logging for normal operation execution errors
|
|
51
|
+
case error instanceof MissingArgumentsError:
|
|
52
|
+
this.logger.log(error.message, {
|
|
53
|
+
stack: error.stack,
|
|
54
|
+
});
|
|
55
|
+
break;
|
|
56
|
+
|
|
57
|
+
case isDefaultErrorMixin(error):
|
|
58
|
+
// no need to log, DefaultErrorMixin already logs
|
|
59
|
+
break;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Log original error message (for now only if is an EntityError)
|
|
63
|
+
* @todo refactor with the default error mixin
|
|
64
|
+
*/
|
|
65
|
+
case isEntityError(error):
|
|
66
|
+
{
|
|
67
|
+
const entityError = error as EntityError;
|
|
68
|
+
this.logger.error(
|
|
69
|
+
entityError.originalError?.message
|
|
70
|
+
? entityError.originalError.message
|
|
71
|
+
: error,
|
|
72
|
+
entityError.originalError?.stack,
|
|
73
|
+
{
|
|
74
|
+
stack: entityError.originalError?.stack,
|
|
75
|
+
data: {
|
|
76
|
+
response: entityError.getResponse(),
|
|
77
|
+
name: entityError.name,
|
|
78
|
+
cause: formatCause(entityError.cause),
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
break;
|
|
84
|
+
|
|
85
|
+
case isHttpError:
|
|
86
|
+
{
|
|
87
|
+
const httpError = error as common.HttpException;
|
|
88
|
+
const logLevel = getLogLevelByStatus(httpError.getStatus());
|
|
89
|
+
|
|
90
|
+
if (logLevel === LogLevelEnum.ERROR) {
|
|
91
|
+
this.logger[logLevel](error.message, error.stack, {
|
|
92
|
+
stack: httpError.stack,
|
|
93
|
+
data: {
|
|
94
|
+
response: httpError.getResponse(),
|
|
95
|
+
name: error.name,
|
|
96
|
+
cause: formatCause(error.cause),
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
} else {
|
|
100
|
+
this.logger[logLevel](error.message, {
|
|
101
|
+
stack: error.stack,
|
|
102
|
+
data: {
|
|
103
|
+
response: httpError.getResponse(),
|
|
104
|
+
name: error.name,
|
|
105
|
+
cause: formatCause(error.cause),
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
break;
|
|
111
|
+
|
|
112
|
+
case error instanceof GqlError:
|
|
113
|
+
this.logger.error((<GqlError>error).systemMessage ?? error.message);
|
|
114
|
+
break;
|
|
115
|
+
|
|
116
|
+
// Log critically any other error, as those are not expected
|
|
117
|
+
default:
|
|
118
|
+
this.logger.error(error.message, error.stack, {
|
|
119
|
+
stack: error.stack,
|
|
120
|
+
data: {
|
|
121
|
+
cause: formatCause(error.cause),
|
|
122
|
+
name: error.name,
|
|
123
|
+
},
|
|
124
|
+
});
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (host.getType() === 'http') {
|
|
129
|
+
if (sendResponse) {
|
|
130
|
+
const ctx = host.switchToHttp();
|
|
131
|
+
const response = ctx.getResponse<FResponse>();
|
|
132
|
+
let status = common.HttpStatus.INTERNAL_SERVER_ERROR;
|
|
133
|
+
if (isHttpError) status = (error as common.HttpException).getStatus();
|
|
134
|
+
return response.status(status).send(error.message);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
} catch (e) {
|
|
138
|
+
// eslint-disable-next-line no-console
|
|
139
|
+
console.error(e); // if there's an error with the logger itself
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return error;
|
|
143
|
+
}
|
|
144
|
+
}
|
package/src/filters/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
/* istanbul ignore file */
|
|
2
|
-
|
|
3
|
-
// export { HttpExceptionFilter } from './http-exception.filter.js';
|
|
4
|
-
// export { DatabaseExceptionFilter } from './database-exception.filter.js';
|
|
5
|
-
// export { ValidationExceptionFilter } from './validation-exception.filter.js';
|
|
6
|
-
export { SystemExceptionFilter } from './system-exception.filter.js';
|
|
1
|
+
/* istanbul ignore file */
|
|
2
|
+
|
|
3
|
+
// export { HttpExceptionFilter } from './http-exception.filter.js';
|
|
4
|
+
// export { DatabaseExceptionFilter } from './database-exception.filter.js';
|
|
5
|
+
// export { ValidationExceptionFilter } from './validation-exception.filter.js';
|
|
6
|
+
export { SystemExceptionFilter } from './system-exception.filter.js';
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { Catch, ExceptionFilter } from '@nestjs/common';
|
|
2
|
-
import type { LoggerService } from '@nestjs/common';
|
|
3
|
-
import { ExceptionContextEnum } from '@node-yalc/errors/error.enum.js';
|
|
4
|
-
|
|
5
|
-
@Catch(TypeError, SyntaxError, RangeError, EvalError, ReferenceError)
|
|
6
|
-
export class SystemExceptionFilter implements ExceptionFilter {
|
|
7
|
-
constructor(private logger: LoggerService) {}
|
|
8
|
-
|
|
9
|
-
catch(error: Error) {
|
|
10
|
-
this.logger.error(error, error.stack, ExceptionContextEnum.SYSTEM);
|
|
11
|
-
return error;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
1
|
+
import { Catch, ExceptionFilter } from '@nestjs/common';
|
|
2
|
+
import type { LoggerService } from '@nestjs/common';
|
|
3
|
+
import { ExceptionContextEnum } from '@node-yalc/errors/error.enum.js';
|
|
4
|
+
|
|
5
|
+
@Catch(TypeError, SyntaxError, RangeError, EvalError, ReferenceError)
|
|
6
|
+
export class SystemExceptionFilter implements ExceptionFilter {
|
|
7
|
+
constructor(private logger: LoggerService) {}
|
|
8
|
+
|
|
9
|
+
catch(error: Error) {
|
|
10
|
+
this.logger.error(error, error.stack, ExceptionContextEnum.SYSTEM);
|
|
11
|
+
return error;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
import { CrudGenError } from '@nest-yalc-2/crud-gen/crud-gen.error.js';
|
|
2
|
-
import { UUIDValidationError } from '@nest-yalc-2/graphql/scalars/uuid-validation.error.js';
|
|
3
|
-
import * as common from '@nestjs/common';
|
|
4
|
-
import { GqlExceptionFilter } from '@nestjs/graphql';
|
|
5
|
-
import { InputValidationError } from '../index.js';
|
|
6
|
-
|
|
7
|
-
import type { LoggerService } from '@nestjs/common';
|
|
8
|
-
|
|
9
|
-
@common.Catch(UUIDValidationError, CrudGenError)
|
|
10
|
-
export class ValidationExceptionFilter implements GqlExceptionFilter {
|
|
11
|
-
constructor(private logger: LoggerService) {}
|
|
12
|
-
|
|
13
|
-
catch(error: Error) {
|
|
14
|
-
const newError = new InputValidationError(
|
|
15
|
-
(<CrudGenError>error).systemMessage,
|
|
16
|
-
{ response: { message: error.message } },
|
|
17
|
-
);
|
|
18
|
-
newError.stack = error.stack; // we need the stack trace for dev
|
|
19
|
-
this.logger.error(
|
|
20
|
-
(<CrudGenError>error).systemMessage ?? newError.message,
|
|
21
|
-
newError.stack,
|
|
22
|
-
);
|
|
23
|
-
|
|
24
|
-
return newError;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
1
|
+
import { CrudGenError } from '@nest-yalc-2/crud-gen/crud-gen.error.js';
|
|
2
|
+
import { UUIDValidationError } from '@nest-yalc-2/graphql/scalars/uuid-validation.error.js';
|
|
3
|
+
import * as common from '@nestjs/common';
|
|
4
|
+
import { GqlExceptionFilter } from '@nestjs/graphql';
|
|
5
|
+
import { InputValidationError } from '../index.js';
|
|
6
|
+
|
|
7
|
+
import type { LoggerService } from '@nestjs/common';
|
|
8
|
+
|
|
9
|
+
@common.Catch(UUIDValidationError, CrudGenError)
|
|
10
|
+
export class ValidationExceptionFilter implements GqlExceptionFilter {
|
|
11
|
+
constructor(private logger: LoggerService) {}
|
|
12
|
+
|
|
13
|
+
catch(error: Error) {
|
|
14
|
+
const newError = new InputValidationError(
|
|
15
|
+
(<CrudGenError>error).systemMessage,
|
|
16
|
+
{ response: { message: error.message } },
|
|
17
|
+
);
|
|
18
|
+
newError.stack = error.stack; // we need the stack trace for dev
|
|
19
|
+
this.logger.error(
|
|
20
|
+
(<CrudGenError>error).systemMessage ?? newError.message,
|
|
21
|
+
newError.stack,
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
return newError;
|
|
25
|
+
}
|
|
26
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
/* istanbul ignore file */
|
|
2
|
-
|
|
3
|
-
export { MissingArgumentsError } from '@nest-yalc-2/crud-gen';
|
|
4
|
-
export * from '@node-yalc/errors/error.enum.js';
|
|
5
|
-
export * from '@node-yalc/errors/error.class.js';
|
|
6
|
-
export * from '@node-yalc/errors/default.error.js';
|
|
7
|
-
export * from '@node-yalc/errors/result.error.js';
|
|
8
|
-
export * from './filters/index.js';
|
|
1
|
+
/* istanbul ignore file */
|
|
2
|
+
|
|
3
|
+
export { MissingArgumentsError } from '@nest-yalc-2/crud-gen';
|
|
4
|
+
export * from '@node-yalc/errors/error.enum.js';
|
|
5
|
+
export * from '@node-yalc/errors/error.class.js';
|
|
6
|
+
export * from '@node-yalc/errors/default.error.js';
|
|
7
|
+
export * from '@node-yalc/errors/result.error.js';
|
|
8
|
+
export * from './filters/index.js';
|