Skip to content

Commit

Permalink
fix(devexp): make unexpected domain object errors more helpful
Browse files Browse the repository at this point in the history
  • Loading branch information
uladkasach committed Aug 7, 2023
1 parent 54e43d7 commit 83c4215
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/manipulation/getUniqueIdentifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -46,5 +47,11 @@ export const getUniqueIdentifier = <T extends Record<string, any>>(
}

// 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,
},
);
};
9 changes: 8 additions & 1 deletion src/manipulation/getUpdatableProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -38,5 +39,11 @@ export const getUpdatableProperties = <T extends Record<string, any>>(
}

// 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,
},
);
};
14 changes: 14 additions & 0 deletions src/utils/errors/UnexpectedCodePathError.ts
Original file line number Diff line number Diff line change
@@ -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<string, any>) {
const fullMessage = `UnexpectedCodePath: ${message}${
metadata ? `\n\n${JSON.stringify(metadata)}` : ''
}`;
super(fullMessage);
}
}

0 comments on commit 83c4215

Please sign in to comment.