From f89d4fb9b0980af58d1e24c9d8d2117113fe50b5 Mon Sep 17 00:00:00 2001 From: Alfredo Arronte Date: Fri, 7 Jun 2024 11:36:01 +0200 Subject: [PATCH] feat(packages/sui-domain): Add DomainError class --- packages/sui-domain/src/DomainError.js | 25 +++++++++++++++++++++++++ packages/sui-domain/src/index.js | 1 + 2 files changed, 26 insertions(+) create mode 100644 packages/sui-domain/src/DomainError.js diff --git a/packages/sui-domain/src/DomainError.js b/packages/sui-domain/src/DomainError.js new file mode 100644 index 000000000..9acc335f8 --- /dev/null +++ b/packages/sui-domain/src/DomainError.js @@ -0,0 +1,25 @@ +/** + * Base class for handling errors in the domain layer + */ +export default class DomainError extends Error { + constructor({code, message, cause}) { + super(message, {cause}) + this.name = 'DomainError' + this.code = code + } + + /** + * DomainError factory method + * + * @param {String} code Code to identify the error instance. It's required. + * @param {String} message Description for the error useful for giving more context. + * @param {Object} cause Keeps the original error useful for wrapping uncontrolled errors. + */ + static create({code, message, cause}) { + if (!code) { + throw new Error('[DomainError] The code property is required.') + } + + return new DomainError({code, message, cause}) + } +} diff --git a/packages/sui-domain/src/index.js b/packages/sui-domain/src/index.js index ec5c03a3b..37a6b83c8 100644 --- a/packages/sui-domain/src/index.js +++ b/packages/sui-domain/src/index.js @@ -1,3 +1,4 @@ +export {default as DomainError} from './DomainError.js' export {default as Entity} from './Entity.js' export {default as EntryPointFactory} from './EntryPointFactory.js' export {default as FetcherFactory} from './fetcher/factory.js'