diff --git a/src/manipulation/getUniqueIdentifier.ts b/src/manipulation/getUniqueIdentifier.ts index 229cbf8..02b643c 100644 --- a/src/manipulation/getUniqueIdentifier.ts +++ b/src/manipulation/getUniqueIdentifier.ts @@ -4,6 +4,7 @@ import { assertDomainObjectIsSafeToManipulate } from '../constraints/assertDomai import { DomainEntity } from '../instantiation/DomainEntity'; import { DomainObject } from '../instantiation/DomainObject'; import { DomainValueObject } from '../instantiation/DomainValueObject'; +import { UnexpectedCodePathError } from '../utils/errors/UnexpectedCodePathError'; import { DomainEntityUniqueKeysMustBeDefinedError } from './DomainEntityUniqueKeysMustBeDefinedError'; /** @@ -46,5 +47,11 @@ export const getUniqueIdentifier = >( } // throw error we get here, this is unexpected - throw new Error('unexpected domain object type'); + throw new UnexpectedCodePathError( + 'unexpected domain object type for getUniqueIdentifier. expected DomainValueObject or DomainEntity', + { + dobjClass: (obj as any)?.constructor?.name, + dobj: obj, + }, + ); }; diff --git a/src/manipulation/getUpdatableProperties.ts b/src/manipulation/getUpdatableProperties.ts index a9fe532..b8ca76e 100644 --- a/src/manipulation/getUpdatableProperties.ts +++ b/src/manipulation/getUpdatableProperties.ts @@ -3,6 +3,7 @@ import pick from 'lodash.pick'; import { assertDomainObjectIsSafeToManipulate } from '../constraints/assertDomainObjectIsSafeToManipulate'; import { DomainEntity } from '../instantiation/DomainEntity'; import { DomainObject } from '../instantiation/DomainObject'; +import { UnexpectedCodePathError } from '../utils/errors/UnexpectedCodePathError'; import { DomainEntityUpdatablePropertiesMustBeDefinedError } from './DomainEntityUpdatablePropertiesMustBeDefinedError'; /** @@ -38,5 +39,11 @@ export const getUpdatableProperties = >( } // throw error we get here, this is unexpected - throw new Error('unexpected domain object type'); + throw new UnexpectedCodePathError( + 'unexpected domain object type for getUpdatableProperties. expected DomainEntity', + { + dobjClass: (dobj as any)?.constructor?.name, + dobj, + }, + ); }; diff --git a/src/utils/errors/UnexpectedCodePathError.ts b/src/utils/errors/UnexpectedCodePathError.ts new file mode 100644 index 0000000..ea15d60 --- /dev/null +++ b/src/utils/errors/UnexpectedCodePathError.ts @@ -0,0 +1,14 @@ +/** + * UnexpectedCodePath errors are used to indicate that we've reached a code path that should never have been reached + * + * Purpose of having a dedicated class for this type of error is to allow us to easily add metadata about what was going on when we reached this code path + * - e.g., the variables in memory at the time + */ +export class UnexpectedCodePathError extends Error { + constructor(message: string, metadata?: Record) { + const fullMessage = `UnexpectedCodePath: ${message}${ + metadata ? `\n\n${JSON.stringify(metadata)}` : '' + }`; + super(fullMessage); + } +}