@@ -7,95 +7,20 @@ export enum TwoFactorErrorEnum {
7
7
TwoFactorCodeNotSet = 'TWO-FACTOR-001' ,
8
8
TwoFactorCodeWrongCode = 'TWO-FACTOR-002' ,
9
9
TwoFactorCodeIsOutdated = 'TWO-FACTOR-003' ,
10
- TwoFactorCodePleaseWait30Seconds = 'TWO-FACTOR-004' ,
10
+ TwoFactorCodePleaseWaitXXSeconds = 'TWO-FACTOR-004' ,
11
11
TwoFactorCodeIsUsed = 'TWO-FACTOR-005' ,
12
12
}
13
13
14
- export const TWO_FACTOR_ERROR_ENUM_TITLES : Record < TwoFactorErrorEnum , string > =
15
- {
16
- [ TwoFactorErrorEnum . COMMON ] : getText ( 'Two factor error' ) ,
17
- [ TwoFactorErrorEnum . TwoFactorCodeNotSet ] : getText (
18
- 'Two factor code not set'
19
- ) ,
20
- [ TwoFactorErrorEnum . TwoFactorCodeWrongCode ] : getText (
21
- 'Wrong two factor code'
22
- ) ,
23
- [ TwoFactorErrorEnum . TwoFactorCodeIsOutdated ] : getText (
24
- 'Two factor code is outdated'
25
- ) ,
26
- [ TwoFactorErrorEnum . TwoFactorCodePleaseWait30Seconds ] : getText (
27
- 'Please wait 30 seconds'
28
- ) ,
29
- [ TwoFactorErrorEnum . TwoFactorCodeIsUsed ] : getText (
30
- 'Two-factor code has already been used'
31
- ) ,
32
- } ;
14
+ export const TWO_FACTOR_ERROR_ENUM_TITLES : Record < TwoFactorErrorEnum , string > = {
15
+ [ TwoFactorErrorEnum . COMMON ] : getText ( 'Two factor error' ) ,
16
+ [ TwoFactorErrorEnum . TwoFactorCodeNotSet ] : getText ( 'Two factor code not set' ) ,
17
+ [ TwoFactorErrorEnum . TwoFactorCodeWrongCode ] : getText ( 'Wrong two factor code' ) ,
18
+ [ TwoFactorErrorEnum . TwoFactorCodeIsOutdated ] : getText ( 'Two factor code is outdated' ) ,
19
+ [ TwoFactorErrorEnum . TwoFactorCodePleaseWaitXXSeconds ] : getText ( 'Please wait {{{timeout}}} seconds' ) ,
20
+ [ TwoFactorErrorEnum . TwoFactorCodeIsUsed ] : getText ( 'Two-factor code has already been used' ) ,
21
+ } ;
33
22
34
- export class TwoFactorErrorMetadataConstraint {
35
- @ApiProperty ( {
36
- type : String ,
37
- } )
38
- name ! : string ;
39
-
40
- @ApiProperty ( {
41
- type : String ,
42
- } )
43
- description ! : string ;
44
-
45
- constructor ( options ?: TwoFactorErrorMetadataConstraint ) {
46
- Object . assign ( this , options ) ;
47
- }
48
- }
49
-
50
- export class TwoFactorErrorMetadata {
51
- @ApiProperty ( {
52
- type : String ,
53
- } )
54
- property ! : string ;
55
-
56
- @ApiProperty ( {
57
- type : ( ) => TwoFactorErrorMetadataConstraint ,
58
- isArray : true ,
59
- } )
60
- constraints ! : TwoFactorErrorMetadataConstraint [ ] ;
61
-
62
- @ApiPropertyOptional ( {
63
- type : ( ) => TwoFactorErrorMetadata ,
64
- isArray : true ,
65
- } )
66
- children ?: TwoFactorErrorMetadata [ ] ;
67
-
68
- constructor ( options ?: TwoFactorErrorMetadata ) {
69
- Object . assign ( this , options ) ;
70
- }
71
-
72
- static fromClassValidatorTwoFactorErrors (
73
- errors ?: CvTwoFactorError [ ]
74
- ) : TwoFactorErrorMetadata [ ] | undefined {
75
- return errors ?. map (
76
- ( error ) =>
77
- new TwoFactorErrorMetadata ( {
78
- property : error . property ,
79
- constraints : Object . entries ( error . constraints || { } ) . map (
80
- ( [ key , value ] ) =>
81
- new TwoFactorErrorMetadataConstraint ( {
82
- name : key ,
83
- description : value ,
84
- } )
85
- ) ,
86
- ...( error . children ?. length
87
- ? {
88
- children : this . fromClassValidatorTwoFactorErrors (
89
- error . children
90
- ) ,
91
- }
92
- : { } ) ,
93
- } )
94
- ) ;
95
- }
96
- }
97
-
98
- export class TwoFactorError extends Error {
23
+ export class TwoFactorError < T = unknown > extends Error {
99
24
@ApiProperty ( {
100
25
type : String ,
101
26
description : Object . entries ( TWO_FACTOR_ERROR_ENUM_TITLES )
@@ -112,34 +37,25 @@ export class TwoFactorError extends Error {
112
37
} )
113
38
code = TwoFactorErrorEnum . COMMON ;
114
39
115
- @ApiPropertyOptional ( { type : TwoFactorErrorMetadata , isArray : true } )
116
- metadata ?: TwoFactorErrorMetadata [ ] ;
40
+ @ApiPropertyOptional ( { type : Object } )
41
+ metadata ?: T ;
117
42
118
- constructor (
119
- message ?: string | TwoFactorErrorEnum ,
120
- code ?: TwoFactorErrorEnum ,
121
- metadata ?: CvTwoFactorError [ ]
122
- ) {
123
- const messageAsCode = Boolean (
124
- message &&
125
- Object . values ( TwoFactorErrorEnum ) . includes (
126
- message as TwoFactorErrorEnum
127
- )
128
- ) ;
43
+ constructor ( message ?: string | TwoFactorErrorEnum , code ?: TwoFactorErrorEnum , metadata ?: T ) {
44
+ const codeAsMetadata = Boolean (
45
+ code && ! Object . values ( TwoFactorErrorEnum ) . includes ( String ( code ) as TwoFactorErrorEnum ) ,
46
+ ) ;
47
+ const messageAsCode = Boolean ( message && Object . values ( TwoFactorErrorEnum ) . includes ( message as TwoFactorErrorEnum ) ) ;
129
48
const preparedCode = messageAsCode ? ( message as TwoFactorErrorEnum ) : code ;
130
- const preparedMessage =
131
- messageAsCode && preparedCode
132
- ? TWO_FACTOR_ERROR_ENUM_TITLES [ preparedCode ]
133
- : message ;
49
+ const preparedMessage = messageAsCode && preparedCode ? TWO_FACTOR_ERROR_ENUM_TITLES [ preparedCode ] : message ;
134
50
51
+ metadata = codeAsMetadata ? ( code as T ) : metadata ;
135
52
code = preparedCode || TwoFactorErrorEnum . COMMON ;
136
53
message = preparedMessage || TWO_FACTOR_ERROR_ENUM_TITLES [ code ] ;
137
54
138
55
super ( message ) ;
139
56
140
57
this . code = code ;
141
58
this . message = message ;
142
- this . metadata =
143
- TwoFactorErrorMetadata . fromClassValidatorTwoFactorErrors ( metadata ) ;
59
+ this . metadata = metadata ;
144
60
}
145
61
}
0 commit comments