Skip to content

Commit 5d223ae

Browse files
committed
fix: change error formats to one style
1 parent 7e75fa2 commit 5d223ae

File tree

5 files changed

+46
-197
lines changed

5 files changed

+46
-197
lines changed

libs/core/prisma-tools/src/lib/prisma-tools.errors.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,14 @@ export class DatabaseError<T = unknown> extends Error {
2525
metadata?: T;
2626

2727
constructor(message?: string | DatabaseErrorEnum, code?: DatabaseErrorEnum, metadata?: T) {
28+
const codeAsMetadata = Boolean(
29+
code && !Object.values(DatabaseErrorEnum).includes(String(code) as DatabaseErrorEnum),
30+
);
2831
const messageAsCode = Boolean(message && Object.values(DatabaseErrorEnum).includes(message as DatabaseErrorEnum));
2932
const preparedCode = messageAsCode ? (message as DatabaseErrorEnum) : code;
3033
const preparedMessage = messageAsCode && preparedCode ? DATABASE_ERROR_ENUM_TITLES[preparedCode] : message;
3134

35+
metadata = codeAsMetadata ? (code as T) : metadata;
3236
code = preparedCode || DatabaseErrorEnum.COMMON;
3337
message = preparedMessage || DATABASE_ERROR_ENUM_TITLES[code];
3438

libs/core/sso/src/lib/sso.errors.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,14 @@ export class SsoError<T = unknown> extends Error {
3434
metadata?: T;
3535

3636
constructor(message?: string | SsoErrorEnum, code?: SsoErrorEnum, metadata?: T) {
37+
const codeAsMetadata = Boolean(
38+
code && !Object.values(SsoErrorEnum).includes(String(code) as SsoErrorEnum),
39+
);
3740
const messageAsCode = Boolean(message && Object.values(SsoErrorEnum).includes(message as SsoErrorEnum));
3841
const preparedCode = messageAsCode ? (message as SsoErrorEnum) : code;
3942
const preparedMessage = messageAsCode && preparedCode ? SSO_ERROR_ENUM_TITLES[preparedCode] : message;
4043

44+
metadata = codeAsMetadata ? (code as T) : metadata;
4145
code = preparedCode || SsoErrorEnum.COMMON;
4246
message = preparedMessage || SSO_ERROR_ENUM_TITLES[code];
4347

libs/core/supabase/src/lib/supabase.errors.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,14 @@ export class SupabaseError<T = unknown> extends Error {
3434
metadata?: T;
3535

3636
constructor(message?: string | SupabaseErrorEnum, code?: SupabaseErrorEnum, metadata?: T) {
37+
const codeAsMetadata = Boolean(
38+
code && !Object.values(SupabaseErrorEnum).includes(String(code) as SupabaseErrorEnum),
39+
);
3740
const messageAsCode = Boolean(message && Object.values(SupabaseErrorEnum).includes(message as SupabaseErrorEnum));
3841
const preparedCode = messageAsCode ? (message as SupabaseErrorEnum) : code;
3942
const preparedMessage = messageAsCode && preparedCode ? SUPABASE_ERROR_ENUM_TITLES[preparedCode] : message;
4043

44+
metadata = codeAsMetadata ? (code as T) : metadata;
4145
code = preparedCode || SupabaseErrorEnum.COMMON;
4246
message = preparedMessage || SUPABASE_ERROR_ENUM_TITLES[code];
4347

libs/feature/notifications/src/lib/notifications.errors.ts

Lines changed: 14 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -9,82 +9,14 @@ export enum NotificationsErrorEnum {
99
FORBIDDEN = 'NOTIFICATIONS-003',
1010
}
1111

12-
export const NOTIFICATIONS_ERROR_ENUM_TITLES: Record<
13-
NotificationsErrorEnum,
14-
string
15-
> = {
12+
export const NOTIFICATIONS_ERROR_ENUM_TITLES: Record<NotificationsErrorEnum, string> = {
1613
[NotificationsErrorEnum.COMMON]: getText('Notifications error'),
17-
[NotificationsErrorEnum.EXTERNAL_TENANT_ID_NOT_SET]:
18-
getText('Tenant ID not set'),
14+
[NotificationsErrorEnum.EXTERNAL_TENANT_ID_NOT_SET]: getText('Tenant ID not set'),
1915
[NotificationsErrorEnum.EXTERNAL_USER_ID_NOT_SET]: getText('User ID not set'),
2016
[NotificationsErrorEnum.FORBIDDEN]: getText('Forbidden'),
2117
};
2218

23-
export class NotificationsErrorMetadataConstraint {
24-
@ApiProperty({
25-
type: String,
26-
})
27-
name!: string;
28-
29-
@ApiProperty({
30-
type: String,
31-
})
32-
description!: string;
33-
34-
constructor(options?: NotificationsErrorMetadataConstraint) {
35-
Object.assign(this, options);
36-
}
37-
}
38-
39-
export class NotificationsErrorMetadata {
40-
@ApiProperty({
41-
type: String,
42-
})
43-
property!: string;
44-
45-
@ApiProperty({
46-
type: () => NotificationsErrorMetadataConstraint,
47-
isArray: true,
48-
})
49-
constraints!: NotificationsErrorMetadataConstraint[];
50-
51-
@ApiPropertyOptional({
52-
type: () => NotificationsErrorMetadata,
53-
isArray: true,
54-
})
55-
children?: NotificationsErrorMetadata[];
56-
57-
constructor(options?: NotificationsErrorMetadata) {
58-
Object.assign(this, options);
59-
}
60-
61-
static fromClassValidatorNotificationsErrors(
62-
errors?: CvNotificationsError[]
63-
): NotificationsErrorMetadata[] | undefined {
64-
return errors?.map(
65-
(error) =>
66-
new NotificationsErrorMetadata({
67-
property: error.property,
68-
constraints: Object.entries(error.constraints || {}).map(
69-
([key, value]) =>
70-
new NotificationsErrorMetadataConstraint({
71-
name: key,
72-
description: value,
73-
})
74-
),
75-
...(error.children?.length
76-
? {
77-
children: this.fromClassValidatorNotificationsErrors(
78-
error.children
79-
),
80-
}
81-
: {}),
82-
})
83-
);
84-
}
85-
}
86-
87-
export class NotificationsError extends Error {
19+
export class NotificationsError<T = unknown> extends Error {
8820
@ApiProperty({
8921
type: String,
9022
description: Object.entries(NOTIFICATIONS_ERROR_ENUM_TITLES)
@@ -101,38 +33,27 @@ export class NotificationsError extends Error {
10133
})
10234
code = NotificationsErrorEnum.COMMON;
10335

104-
@ApiPropertyOptional({ type: NotificationsErrorMetadata, isArray: true })
105-
metadata?: NotificationsErrorMetadata[];
36+
@ApiPropertyOptional({ type: Object })
37+
metadata?: T;
10638

107-
constructor(
108-
message?: string | NotificationsErrorEnum,
109-
code?: NotificationsErrorEnum,
110-
metadata?: CvNotificationsError[]
111-
) {
39+
constructor(message?: string | NotificationsErrorEnum, code?: NotificationsErrorEnum, metadata?: T) {
40+
const codeAsMetadata = Boolean(
41+
code && !Object.values(NotificationsErrorEnum).includes(String(code) as NotificationsErrorEnum),
42+
);
11243
const messageAsCode = Boolean(
113-
message &&
114-
Object.values(NotificationsErrorEnum).includes(
115-
message as NotificationsErrorEnum
116-
)
44+
message && Object.values(NotificationsErrorEnum).includes(message as NotificationsErrorEnum),
11745
);
118-
const preparedCode = messageAsCode
119-
? (message as NotificationsErrorEnum)
120-
: code;
121-
const preparedMessage =
122-
messageAsCode && preparedCode
123-
? NOTIFICATIONS_ERROR_ENUM_TITLES[preparedCode]
124-
: message;
46+
const preparedCode = messageAsCode ? (message as NotificationsErrorEnum) : code;
47+
const preparedMessage = messageAsCode && preparedCode ? NOTIFICATIONS_ERROR_ENUM_TITLES[preparedCode] : message;
12548

49+
metadata = codeAsMetadata ? (code as T) : metadata;
12650
code = preparedCode || NotificationsErrorEnum.COMMON;
12751
message = preparedMessage || NOTIFICATIONS_ERROR_ENUM_TITLES[code];
12852

12953
super(message);
13054

13155
this.code = code;
13256
this.message = message;
133-
this.metadata =
134-
NotificationsErrorMetadata.fromClassValidatorNotificationsErrors(
135-
metadata
136-
);
57+
this.metadata = metadata;
13758
}
13859
}

libs/feature/two-factor/src/lib/two-factor.errors.ts

Lines changed: 20 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -7,95 +7,20 @@ export enum TwoFactorErrorEnum {
77
TwoFactorCodeNotSet = 'TWO-FACTOR-001',
88
TwoFactorCodeWrongCode = 'TWO-FACTOR-002',
99
TwoFactorCodeIsOutdated = 'TWO-FACTOR-003',
10-
TwoFactorCodePleaseWait30Seconds = 'TWO-FACTOR-004',
10+
TwoFactorCodePleaseWaitXXSeconds = 'TWO-FACTOR-004',
1111
TwoFactorCodeIsUsed = 'TWO-FACTOR-005',
1212
}
1313

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+
};
3322

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 {
9924
@ApiProperty({
10025
type: String,
10126
description: Object.entries(TWO_FACTOR_ERROR_ENUM_TITLES)
@@ -112,34 +37,25 @@ export class TwoFactorError extends Error {
11237
})
11338
code = TwoFactorErrorEnum.COMMON;
11439

115-
@ApiPropertyOptional({ type: TwoFactorErrorMetadata, isArray: true })
116-
metadata?: TwoFactorErrorMetadata[];
40+
@ApiPropertyOptional({ type: Object })
41+
metadata?: T;
11742

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));
12948
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;
13450

51+
metadata = codeAsMetadata ? (code as T) : metadata;
13552
code = preparedCode || TwoFactorErrorEnum.COMMON;
13653
message = preparedMessage || TWO_FACTOR_ERROR_ENUM_TITLES[code];
13754

13855
super(message);
13956

14057
this.code = code;
14158
this.message = message;
142-
this.metadata =
143-
TwoFactorErrorMetadata.fromClassValidatorTwoFactorErrors(metadata);
59+
this.metadata = metadata;
14460
}
14561
}

0 commit comments

Comments
 (0)