@nest-yalc-2/errors 1.3.4 → 2.0.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.
- package/package.json +5 -5
- package/src/__tests__/database-exception.filter.spec.ts +1 -1
- package/src/__tests__/default.error.spec.ts +1 -1
- package/src/__tests__/errors.enum.spec.ts +1 -1
- package/src/__tests__/http-exception.filter.spec.ts +54 -1
- package/src/__tests__/http-status-code-to-errors.spec.ts +1 -1
- package/src/__tests__/input-validation.error.spec.ts +1 -1
- package/src/__tests__/login.error.spec.ts +1 -1
- package/src/__tests__/system-exception.filter.spec.ts +1 -1
- package/src/__tests__/verification.error.spec.ts +1 -1
- package/src/filters/database-exception.filter.d.ts +2 -1
- package/src/filters/database-exception.filter.js +5 -46
- package/src/filters/database-exception.filter.js.map +1 -1
- package/src/filters/database-exception.filter.ts +4 -2
- package/src/filters/http-exception.filter.d.ts +4 -3
- package/src/filters/http-exception.filter.js +5 -46
- package/src/filters/http-exception.filter.js.map +1 -1
- package/src/filters/http-exception.filter.ts +4 -3
- package/src/filters/system-exception.filter.js +4 -12
- package/src/filters/system-exception.filter.js.map +1 -1
- package/src/filters/system-exception.filter.ts +1 -1
- package/src/filters/validation-exception.filter.d.ts +2 -2
- package/src/filters/validation-exception.filter.js +4 -45
- package/src/filters/validation-exception.filter.js.map +1 -1
- package/src/filters/validation-exception.filter.ts +3 -1
- package/src/index.d.ts +2 -4
- package/src/index.js +3 -18
- package/src/index.js.map +1 -1
- package/src/index.ts +2 -4
- package/src/default.error.d.ts +0 -93
- package/src/default.error.js +0 -266
- package/src/default.error.js.map +0 -1
- package/src/default.error.ts +0 -640
- package/src/error.class.d.ts +0 -103
- package/src/error.class.js +0 -170
- package/src/error.class.js.map +0 -1
- package/src/error.class.ts +0 -319
- package/src/error.enum.d.ts +0 -29
- package/src/error.enum.js +0 -41
- package/src/error.enum.js.map +0 -1
- package/src/error.enum.ts +0 -45
- package/src/error.helper.d.ts +0 -2
- package/src/error.helper.js +0 -20
- package/src/error.helper.js.map +0 -1
- package/src/error.helper.ts +0 -34
- package/src/http-status-code-to-errors.d.ts +0 -4
- package/src/http-status-code-to-errors.js +0 -24
- package/src/http-status-code-to-errors.js.map +0 -1
- package/src/http-status-code-to-errors.ts +0 -41
- package/src/result.error.d.ts +0 -4
- package/src/result.error.js +0 -24
- package/src/result.error.js.map +0 -1
- package/src/result.error.ts +0 -25
package/src/error.enum.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"error.enum.js","sourceRoot":"","sources":["error.enum.ts"],"names":[],"mappings":";;;AAAA,2CAA4C;AAE5C,IAAY,UA4BX;AA5BD,WAAY,UAAU;IAEpB,yCAA2B,CAAA;IAC3B,6CAA+B,CAAA;IAE/B,2CAA6B,CAAA;IAE7B,qCAAuB,CAAA;IAEvB,mDAAqC,CAAA;IACrC,qCAAuB,CAAA;IACvB,uDAAyC,CAAA;IACzC,qCAAuB,CAAA;IACvB,uDAAyC,CAAA;IACzC,+CAAiC,CAAA;IACjC,mCAAqB,CAAA;IACrB,2BAAa,CAAA;IACb,+DAAiD,CAAA;IACjD,2DAA6C,CAAA;IAC7C,6EAA+D,CAAA;IAC/D,qDAAuC,CAAA;IAGvC,6DAA+C,CAAA;IAC/C,iDAAmC,CAAA;IACnC,yCAA2B,CAAA;IAC3B,yDAA2C,CAAA;IAC3C,iDAAmC,CAAA;AACrC,CAAC,EA5BW,UAAU,0BAAV,UAAU,QA4BrB;AAEM,MAAM,uBAAuB,GAAG,CAAC,IAAY,EAAU,EAAE;IAC9D,MAAM,kBAAkB,GAAG,MAAM,CAAC,OAAO,CAAC,mBAAU,CAAC,CAAC,IAAI,CACxD,CAAC,CAAC,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,KAAK,KAAK,IAAI,CAC9B,EAAE,CAAC,CAAC,CAAC,CAAC;IACP,MAAM,SAAS,GAAG,UAAU,CAAC,kBAA6C,CAAC,CAAC;IAC5E,OAAO,SAAS,IAAI,SAAS,CAAC;AAChC,CAAC,CAAC;AANW,QAAA,uBAAuB,2BAMlC;AAEF,IAAY,oBAIX;AAJD,WAAY,oBAAoB;IAC9B,sDAA8B,CAAA;IAC9B,8CAAsB,CAAA;IACtB,kDAA0B,CAAA;AAC5B,CAAC,EAJW,oBAAoB,oCAApB,oBAAoB,QAI/B"}
|
package/src/error.enum.ts
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { HttpStatus } from '@nestjs/common';
|
|
2
|
-
|
|
3
|
-
export enum ErrorsEnum {
|
|
4
|
-
// 400 Client Errors
|
|
5
|
-
BAD_REQUEST = 'Bad request', // 400
|
|
6
|
-
INVALID_VALUE = 'Invalid value', // 400 (custom)
|
|
7
|
-
|
|
8
|
-
UNAUTHORIZED = 'Unauthorized', // 401
|
|
9
|
-
|
|
10
|
-
BAD_LOGIN = 'Bad login', // 401 (custom, usually used for unauthorized access with wrong credentials)
|
|
11
|
-
|
|
12
|
-
PAYMENT_REQUIRED = 'Payment required', // 402
|
|
13
|
-
FORBIDDEN = 'Forbidden', // 403
|
|
14
|
-
FORBIDDEN_RESOURCE = 'Forbidden resource', // 403 (custom)
|
|
15
|
-
NOT_FOUND = 'Not found', // 404
|
|
16
|
-
METHOD_NOT_ALLOWED = 'Method not allowed', // 405
|
|
17
|
-
NOT_ACCEPTABLE = 'Not acceptable', // 406
|
|
18
|
-
CONFLICT = 'Conflict', // 409
|
|
19
|
-
GONE = 'Gone', // 410
|
|
20
|
-
UNSUPPORTED_MEDIA_TYPE = 'Unsupported media type', // 415
|
|
21
|
-
UNPROCESSABLE_ENTITY = 'Unprocessable entity', // 422
|
|
22
|
-
UNAVAILABLE_FOR_LEGAL_REASONS = 'Unavailable for legal reasons', // 451
|
|
23
|
-
TOO_MANY_REQUESTS = 'Too many requests', // 429
|
|
24
|
-
|
|
25
|
-
// 500 Server Errors
|
|
26
|
-
INTERNAL_SERVER_ERROR = 'Internal server error', // 500
|
|
27
|
-
NOT_IMPLEMENTED = 'Not implemented', // 501
|
|
28
|
-
BAD_GATEWAY = 'Bad gateway', // 502
|
|
29
|
-
SERVICE_UNAVAILABLE = 'Service unavailable', // 503
|
|
30
|
-
GATEWAY_TIMEOUT = 'Gateway timeout', // 504
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export const getHttpStatusNameByCode = (code: number): string => {
|
|
34
|
-
const httpStatusEnumName = Object.entries(HttpStatus).find(
|
|
35
|
-
([, value]) => value === code,
|
|
36
|
-
)?.[0];
|
|
37
|
-
const enumValue = ErrorsEnum[httpStatusEnumName as keyof typeof ErrorsEnum];
|
|
38
|
-
return enumValue ?? 'Unknown';
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
export enum ExceptionContextEnum {
|
|
42
|
-
DATABASE = 'DatabaseException',
|
|
43
|
-
HTTP = 'HttpException',
|
|
44
|
-
SYSTEM = 'SystemException',
|
|
45
|
-
}
|
package/src/error.helper.d.ts
DELETED
package/src/error.helper.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getStatusCodeFromError = getStatusCodeFromError;
|
|
4
|
-
const class_helper_js_1 = require("@nest-yalc-2/utils/class.helper.js");
|
|
5
|
-
const http_helper_js_1 = require("@nest-yalc-2/utils/http.helper.js");
|
|
6
|
-
const default_error_js_1 = require("./default.error.js");
|
|
7
|
-
function getStatusCodeFromError(error) {
|
|
8
|
-
if (!(0, class_helper_js_1.isClass)(error)) {
|
|
9
|
-
if (error.getStatus) {
|
|
10
|
-
return error.getStatus();
|
|
11
|
-
}
|
|
12
|
-
return null;
|
|
13
|
-
}
|
|
14
|
-
if ((0, default_error_js_1.isDefaultErrorMixinClass)(error)) {
|
|
15
|
-
return error.defaultStatusCode;
|
|
16
|
-
}
|
|
17
|
-
const errorName = error.name;
|
|
18
|
-
return http_helper_js_1.httpExceptionStatusCodes[errorName] || null;
|
|
19
|
-
}
|
|
20
|
-
//# sourceMappingURL=error.helper.js.map
|
package/src/error.helper.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"error.helper.js","sourceRoot":"","sources":["error.helper.ts"],"names":[],"mappings":";;AASA,wDAwBC;AAhCD,wEAA6D;AAC7D,sEAA6E;AAC7E,yDAA8D;AAM9D,SAAgB,sBAAsB,CAAC,KAA2B;IAIhE,IAAI,CAAC,IAAA,yBAAO,EAAC,KAAK,CAAC,EAAE,CAAC;QACpB,IAAI,KAAK,CAAC,SAAS,EAAE,CAAC;YACpB,OAAO,KAAK,CAAC,SAAS,EAAE,CAAC;QAC3B,CAAC;QAED,OAAO,IAAI,CAAC;IACd,CAAC;IAKD,IAAI,IAAA,2CAAwB,EAAC,KAAK,CAAC,EAAE,CAAC;QACpC,OAAO,KAAK,CAAC,iBAAiB,CAAC;IACjC,CAAC;IAKD,MAAM,SAAS,GAAG,KAAK,CAAC,IAAI,CAAC;IAC7B,OAAO,yCAAwB,CAAC,SAAS,CAAC,IAAI,IAAI,CAAC;AACrD,CAAC"}
|
package/src/error.helper.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { ClassType } from '@nest-yalc-2/types/globals.d.js';
|
|
2
|
-
import { isClass } from '@nest-yalc-2/utils/class.helper.js';
|
|
3
|
-
import { httpExceptionStatusCodes } from '@nest-yalc-2/utils/http.helper.js';
|
|
4
|
-
import { isDefaultErrorMixinClass } from './default.error.js';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Function to convert all HttpException based errors classes to their corresponding status code
|
|
8
|
-
* using a predefined list of status codes
|
|
9
|
-
*/
|
|
10
|
-
export function getStatusCodeFromError(error: ClassType<any> | any) {
|
|
11
|
-
/**
|
|
12
|
-
* If it's not a class, check if it has a getStatus method
|
|
13
|
-
*/
|
|
14
|
-
if (!isClass(error)) {
|
|
15
|
-
if (error.getStatus) {
|
|
16
|
-
return error.getStatus();
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
return null;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Check Default error classes
|
|
24
|
-
*/
|
|
25
|
-
if (isDefaultErrorMixinClass(error)) {
|
|
26
|
-
return error.defaultStatusCode;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Check HttpException classes
|
|
31
|
-
*/
|
|
32
|
-
const errorName = error.name;
|
|
33
|
-
return httpExceptionStatusCodes[errorName] || null;
|
|
34
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.httpStatusCodeToErrors = void 0;
|
|
4
|
-
const error_class_js_1 = require("./error.class.js");
|
|
5
|
-
const common_1 = require("@nestjs/common");
|
|
6
|
-
exports.httpStatusCodeToErrors = {
|
|
7
|
-
[common_1.HttpStatus.BAD_REQUEST]: error_class_js_1.BadRequestError,
|
|
8
|
-
[common_1.HttpStatus.UNAUTHORIZED]: error_class_js_1.UnauthorizedError,
|
|
9
|
-
[common_1.HttpStatus.FORBIDDEN]: error_class_js_1.ForbiddenError,
|
|
10
|
-
[common_1.HttpStatus.NOT_FOUND]: error_class_js_1.NotFoundError,
|
|
11
|
-
[common_1.HttpStatus.CONFLICT]: error_class_js_1.ConflictError,
|
|
12
|
-
[common_1.HttpStatus.INTERNAL_SERVER_ERROR]: error_class_js_1.InternalServerError,
|
|
13
|
-
[common_1.HttpStatus.PAYMENT_REQUIRED]: error_class_js_1.PaymentRequiredError,
|
|
14
|
-
[common_1.HttpStatus.METHOD_NOT_ALLOWED]: error_class_js_1.MethodNotAllowedError,
|
|
15
|
-
[common_1.HttpStatus.GONE]: error_class_js_1.GoneError,
|
|
16
|
-
[common_1.HttpStatus.UNSUPPORTED_MEDIA_TYPE]: error_class_js_1.UnsupportedMediaTypeError,
|
|
17
|
-
[common_1.HttpStatus.UNPROCESSABLE_ENTITY]: error_class_js_1.UnprocessableEntityError,
|
|
18
|
-
[common_1.HttpStatus.TOO_MANY_REQUESTS]: error_class_js_1.TooManyRequestsError,
|
|
19
|
-
[common_1.HttpStatus.NOT_IMPLEMENTED]: error_class_js_1.NotImplementedError,
|
|
20
|
-
[common_1.HttpStatus.BAD_GATEWAY]: error_class_js_1.BadGatewayError,
|
|
21
|
-
[common_1.HttpStatus.SERVICE_UNAVAILABLE]: error_class_js_1.ServiceUnavailableError,
|
|
22
|
-
[common_1.HttpStatus.GATEWAY_TIMEOUT]: error_class_js_1.GatewayTimeoutError,
|
|
23
|
-
};
|
|
24
|
-
//# sourceMappingURL=http-status-code-to-errors.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"http-status-code-to-errors.js","sourceRoot":"","sources":["http-status-code-to-errors.ts"],"names":[],"mappings":";;;AAAA,qDAiB0B;AAE1B,2CAA4C;AAE/B,QAAA,sBAAsB,GAE/B;IACF,CAAC,mBAAU,CAAC,WAAW,CAAC,EAAE,gCAAe;IACzC,CAAC,mBAAU,CAAC,YAAY,CAAC,EAAE,kCAAiB;IAC5C,CAAC,mBAAU,CAAC,SAAS,CAAC,EAAE,+BAAc;IACtC,CAAC,mBAAU,CAAC,SAAS,CAAC,EAAE,8BAAa;IACrC,CAAC,mBAAU,CAAC,QAAQ,CAAC,EAAE,8BAAa;IACpC,CAAC,mBAAU,CAAC,qBAAqB,CAAC,EAAE,oCAAmB;IACvD,CAAC,mBAAU,CAAC,gBAAgB,CAAC,EAAE,qCAAoB;IACnD,CAAC,mBAAU,CAAC,kBAAkB,CAAC,EAAE,sCAAqB;IACtD,CAAC,mBAAU,CAAC,IAAI,CAAC,EAAE,0BAAS;IAC5B,CAAC,mBAAU,CAAC,sBAAsB,CAAC,EAAE,0CAAyB;IAC9D,CAAC,mBAAU,CAAC,oBAAoB,CAAC,EAAE,yCAAwB;IAC3D,CAAC,mBAAU,CAAC,iBAAiB,CAAC,EAAE,qCAAoB;IACpD,CAAC,mBAAU,CAAC,eAAe,CAAC,EAAE,oCAAmB;IACjD,CAAC,mBAAU,CAAC,WAAW,CAAC,EAAE,gCAAe;IACzC,CAAC,mBAAU,CAAC,mBAAmB,CAAC,EAAE,wCAAuB;IACzD,CAAC,mBAAU,CAAC,eAAe,CAAC,EAAE,oCAAmB;CAClD,CAAC"}
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
BadGatewayError,
|
|
3
|
-
BadRequestError,
|
|
4
|
-
ConflictError,
|
|
5
|
-
ForbiddenError,
|
|
6
|
-
GatewayTimeoutError,
|
|
7
|
-
GoneError,
|
|
8
|
-
InternalServerError,
|
|
9
|
-
MethodNotAllowedError,
|
|
10
|
-
NotFoundError,
|
|
11
|
-
NotImplementedError,
|
|
12
|
-
PaymentRequiredError,
|
|
13
|
-
ServiceUnavailableError,
|
|
14
|
-
TooManyRequestsError,
|
|
15
|
-
UnauthorizedError,
|
|
16
|
-
UnprocessableEntityError,
|
|
17
|
-
UnsupportedMediaTypeError,
|
|
18
|
-
} from './error.class.js';
|
|
19
|
-
import { HttpStatusCodes } from '@nest-yalc-2/utils/http.helper.js';
|
|
20
|
-
import { HttpStatus } from '@nestjs/common';
|
|
21
|
-
|
|
22
|
-
export const httpStatusCodeToErrors: {
|
|
23
|
-
[key in HttpStatusCodes]?: any;
|
|
24
|
-
} = {
|
|
25
|
-
[HttpStatus.BAD_REQUEST]: BadRequestError,
|
|
26
|
-
[HttpStatus.UNAUTHORIZED]: UnauthorizedError,
|
|
27
|
-
[HttpStatus.FORBIDDEN]: ForbiddenError,
|
|
28
|
-
[HttpStatus.NOT_FOUND]: NotFoundError,
|
|
29
|
-
[HttpStatus.CONFLICT]: ConflictError,
|
|
30
|
-
[HttpStatus.INTERNAL_SERVER_ERROR]: InternalServerError,
|
|
31
|
-
[HttpStatus.PAYMENT_REQUIRED]: PaymentRequiredError,
|
|
32
|
-
[HttpStatus.METHOD_NOT_ALLOWED]: MethodNotAllowedError,
|
|
33
|
-
[HttpStatus.GONE]: GoneError,
|
|
34
|
-
[HttpStatus.UNSUPPORTED_MEDIA_TYPE]: UnsupportedMediaTypeError,
|
|
35
|
-
[HttpStatus.UNPROCESSABLE_ENTITY]: UnprocessableEntityError,
|
|
36
|
-
[HttpStatus.TOO_MANY_REQUESTS]: TooManyRequestsError,
|
|
37
|
-
[HttpStatus.NOT_IMPLEMENTED]: NotImplementedError,
|
|
38
|
-
[HttpStatus.BAD_GATEWAY]: BadGatewayError,
|
|
39
|
-
[HttpStatus.SERVICE_UNAVAILABLE]: ServiceUnavailableError,
|
|
40
|
-
[HttpStatus.GATEWAY_TIMEOUT]: GatewayTimeoutError,
|
|
41
|
-
};
|
package/src/result.error.d.ts
DELETED
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import { Result } from '@nest-yalc-2/event-manager/event-result.types.js';
|
|
2
|
-
import { IDefaultErrorOptions } from './default.error.js';
|
|
3
|
-
export declare const tryCatch: <T>(fn: () => T, options?: IDefaultErrorOptions) => Result<T>;
|
|
4
|
-
export declare const tryCatchAsync: <T>(fn: () => Promise<T>, options?: IDefaultErrorOptions) => Promise<Result<T>>;
|
package/src/result.error.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.tryCatchAsync = exports.tryCatch = void 0;
|
|
4
|
-
const neverthrow_1 = require("neverthrow");
|
|
5
|
-
const default_error_js_1 = require("./default.error.js");
|
|
6
|
-
const tryCatch = (fn, options = {}) => {
|
|
7
|
-
try {
|
|
8
|
-
return (0, neverthrow_1.ok)(fn());
|
|
9
|
-
}
|
|
10
|
-
catch (error) {
|
|
11
|
-
return (0, neverthrow_1.err)((0, default_error_js_1.errorToDefaultError)(error, options));
|
|
12
|
-
}
|
|
13
|
-
};
|
|
14
|
-
exports.tryCatch = tryCatch;
|
|
15
|
-
const tryCatchAsync = async (fn, options = {}) => {
|
|
16
|
-
try {
|
|
17
|
-
return (0, neverthrow_1.ok)(await fn());
|
|
18
|
-
}
|
|
19
|
-
catch (error) {
|
|
20
|
-
return (0, neverthrow_1.err)((0, default_error_js_1.errorToDefaultError)(error, options));
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
exports.tryCatchAsync = tryCatchAsync;
|
|
24
|
-
//# sourceMappingURL=result.error.js.map
|
package/src/result.error.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"result.error.js","sourceRoot":"","sources":["result.error.ts"],"names":[],"mappings":";;;AAAA,2CAAqC;AAErC,yDAA+E;AAExE,MAAM,QAAQ,GAAG,CACtB,EAAW,EACX,UAAgC,EAAE,EACvB,EAAE;IACb,IAAI,CAAC;QACH,OAAO,IAAA,eAAE,EAAC,EAAE,EAAE,CAAC,CAAC;IAClB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,IAAA,gBAAG,EAAC,IAAA,sCAAmB,EAAC,KAAc,EAAE,OAAO,CAAC,CAAC,CAAC;IAC3D,CAAC;AACH,CAAC,CAAC;AATW,QAAA,QAAQ,YASnB;AAEK,MAAM,aAAa,GAAG,KAAK,EAChC,EAAoB,EACpB,UAAgC,EAAE,EACd,EAAE;IACtB,IAAI,CAAC;QACH,OAAO,IAAA,eAAE,EAAC,MAAM,EAAE,EAAE,CAAC,CAAC;IACxB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,IAAA,gBAAG,EAAC,IAAA,sCAAmB,EAAC,KAAc,EAAE,OAAO,CAAC,CAAC,CAAC;IAC3D,CAAC;AACH,CAAC,CAAC;AATW,QAAA,aAAa,iBASxB"}
|
package/src/result.error.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { err, ok } from 'neverthrow';
|
|
2
|
-
import { Result } from '@nest-yalc-2/event-manager/event-result.types.js';
|
|
3
|
-
import { errorToDefaultError, IDefaultErrorOptions } from './default.error.js';
|
|
4
|
-
|
|
5
|
-
export const tryCatch = <T>(
|
|
6
|
-
fn: () => T,
|
|
7
|
-
options: IDefaultErrorOptions = {},
|
|
8
|
-
): Result<T> => {
|
|
9
|
-
try {
|
|
10
|
-
return ok(fn());
|
|
11
|
-
} catch (error) {
|
|
12
|
-
return err(errorToDefaultError(error as Error, options));
|
|
13
|
-
}
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
export const tryCatchAsync = async <T>(
|
|
17
|
-
fn: () => Promise<T>,
|
|
18
|
-
options: IDefaultErrorOptions = {},
|
|
19
|
-
): Promise<Result<T>> => {
|
|
20
|
-
try {
|
|
21
|
-
return ok(await fn());
|
|
22
|
-
} catch (error) {
|
|
23
|
-
return err(errorToDefaultError(error as Error, options));
|
|
24
|
-
}
|
|
25
|
-
};
|