Skip to content

Commit 2652efe

Browse files
author
Bruno Castro
committed
Remove message bundle and improve error handling
1 parent 5adf45b commit 2652efe

File tree

16 files changed

+76
-270
lines changed

16 files changed

+76
-270
lines changed

src/_lib/errors/BadRequestError.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import { makePredicate } from '@/_lib/Predicate';
33

44
namespace BadRequestError {
55
const type = Symbol();
6-
const code = 'BadRequestError';
6+
const name = 'BadRequestError';
7+
const _message = 'Bad Request';
78

8-
export const create = (message: string): Exception => new BaseError({ type, code, message });
9+
export const create = (message: string = _message, code: string = name): Exception =>
10+
new BaseError({ type, name, code, message });
911

1012
export const is = makePredicate<Exception>(type);
1113
}

src/_lib/errors/BaseError.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
type Exception<M = any> = Readonly<{
2+
name: string;
23
type: symbol;
34
message: string;
45
code: string;
56
meta?: M;
67
}>;
78

9+
type Props<M = any> = Omit<Exception<M>, 'name'> & { name?: string };
10+
811
class BaseError<M = any> extends Error implements Exception<M> {
912
public readonly type: symbol;
1013
public readonly code: string;
1114
public readonly meta?: M;
1215

13-
constructor(props: Exception<M>) {
16+
constructor(props: Props<M>) {
1417
super();
15-
this.name = props.code;
18+
this.name = props.name || props.code;
1619
this.type = props.type;
1720
this.code = props.code;
1821
this.meta = props.meta;

src/_lib/errors/ForbiddenError.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import { makePredicate } from '@/_lib/Predicate';
33

44
namespace ForbiddenError {
55
const type = Symbol();
6-
const code = 'ForbiddenError';
7-
const message = 'Forbidden';
6+
const name = 'ForbiddenError';
7+
const _message = 'Forbidden';
88

9-
export const create = (customMsg?: string): Exception => new BaseError({ type, code, message: customMsg || message });
9+
export const create = (message: string = _message, code: string = name): Exception =>
10+
new BaseError({ type, name, code, message });
1011

1112
export const is = makePredicate<Exception>(type);
1213
}

src/_lib/errors/NotFoundError.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import { makePredicate } from '@/_lib/Predicate';
33

44
namespace NotFoundError {
55
const type = Symbol();
6-
const code = 'NotFoundError';
6+
const name = 'NotFoundError';
7+
const _message = 'Not Found';
78

8-
export const create = (message: string): Exception => new BaseError({ type, code, message });
9+
export const create = (message: string = _message, code: string = name): Exception =>
10+
new BaseError({ type, name, code, message });
911

1012
export const is = makePredicate<Exception>(type);
1113
}

src/_lib/errors/UnauthorizedError.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import { makePredicate } from '@/_lib/Predicate';
33

44
namespace UnauthorizedError {
55
const type = Symbol();
6-
const code = 'UnauthorizedError';
7-
const message = 'Unauthorized';
6+
const name = 'UnauthorizedError';
7+
const _message = 'Unauthorized';
88

9-
export const create = (customMsg?: string): Exception => new BaseError({ type, code, message: customMsg || message });
9+
export const create = (message: string = _message, code: string = name): Exception =>
10+
new BaseError({ type, name, code, message });
1011

1112
export const is = makePredicate<Exception>(type);
1213
}

src/_lib/errors/ValidationError.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import { makePredicate } from '@/_lib/Predicate';
44

55
namespace ValidationError {
66
const type = Symbol();
7-
const code = 'ValidationError';
7+
const name = 'ValidationError';
88

99
type Props = {
1010
readonly target: string;
1111
readonly error: Joi.ValidationError;
1212
};
1313

1414
export const create = ({ error, target }: Props): Exception<Props> =>
15-
new BaseError<Props>({ type, code, message: error.message, meta: { target, error } });
15+
new BaseError<Props>({ type, name, code: name, message: error.message, meta: { target, error } });
1616

1717
export const is = makePredicate<Exception<Props>>(type);
1818
}

src/_lib/message/MessageBundle.ts

Lines changed: 0 additions & 161 deletions
This file was deleted.

src/_sharedKernel/domain/error/BusinessError.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,12 @@ import { makePredicate } from '@/_lib/Predicate';
33

44
namespace BusinessError {
55
const type = Symbol();
6-
const code = 'BusinessError';
6+
const name = 'BusinessError';
77

8-
type Props = {
9-
key: string;
10-
template: string | null;
11-
parameters?: any;
12-
};
8+
export const create = (message: string, code: string = name): Exception =>
9+
new BaseError({ type, name, code, message });
1310

14-
export const create = ({ key, message, template, parameters }: Props & { message: string }): Exception<Props> =>
15-
new BaseError<Props>({ type, code, message, meta: { key, template, parameters } });
16-
17-
export const is = makePredicate<Exception<Props>>(type);
11+
export const is = makePredicate<Exception>(type);
1812
}
1913

2014
export { BusinessError };

src/_sharedKernel/interface/http/ErrorConverters.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ const errorConverters = [
1515
return {
1616
status,
1717
body: {
18-
error: err.code,
18+
error: err.name,
19+
code: err.code,
1920
status,
2021
message: err.message,
2122
details: err.meta?.error.details.map((detail) => ({
@@ -28,39 +29,44 @@ const errorConverters = [
2829
errorConverter(BadRequestError.is, (err) => ({
2930
status: HttpStatus.BAD_REQUEST,
3031
body: {
31-
error: err.code,
32+
error: err.name,
33+
code: err.code,
3234
status: HttpStatus.BAD_REQUEST,
3335
message: err.message,
3436
},
3537
})),
3638
errorConverter(NotFoundError.is, (err) => ({
3739
status: HttpStatus.NOT_FOUND,
3840
body: {
39-
error: err.code,
41+
error: err.name,
42+
code: err.code,
4043
status: HttpStatus.NOT_FOUND,
4144
message: err.message,
4245
},
4346
})),
4447
errorConverter(UnauthorizedError.is, (err) => ({
4548
status: HttpStatus.UNAUTHORIZED,
4649
body: {
47-
error: err.code,
50+
error: err.name,
51+
code: err.code,
4852
status: HttpStatus.UNAUTHORIZED,
4953
message: err.message,
5054
},
5155
})),
5256
errorConverter(ForbiddenError.is, (err) => ({
5357
status: HttpStatus.FORBIDDEN,
5458
body: {
55-
error: err.code,
59+
error: err.name,
60+
code: err.code,
5661
status: HttpStatus.FORBIDDEN,
5762
message: err.message,
5863
},
5964
})),
6065
errorConverter(BusinessError.is, (err) => ({
6166
status: HttpStatus.CONFLICT,
6267
body: {
63-
error: err.code,
68+
error: err.name,
69+
code: err.code,
6470
status: HttpStatus.CONFLICT,
6571
kind: err.meta?.key,
6672
message: err.message,
@@ -69,8 +75,14 @@ const errorConverters = [
6975
errorConverter(
7076
(err: any | BaseError): err is BaseError => err instanceof BaseError,
7177
(err) => ({
72-
status: HttpStatus.BAD_REQUEST,
73-
body: err.message,
78+
status: HttpStatus.INTERNAL_SERVER_ERROR,
79+
body: {
80+
error: err.name,
81+
code: err.code,
82+
status: HttpStatus.INTERNAL_SERVER_ERROR,
83+
meta: err.meta,
84+
message: err.message,
85+
},
7486
})
7587
),
7688
];

0 commit comments

Comments
 (0)