Skip to content

Commit

Permalink
feat: Make getSafeFrom list existing properties as part of error for …
Browse files Browse the repository at this point in the history
…none found
  • Loading branch information
Iku-turso committed Nov 14, 2022
1 parent f9e85a4 commit 8eef13c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
8 changes: 6 additions & 2 deletions packages/fp/src/getSafeFrom/getSafeFrom.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { curry, get, has } from 'lodash/fp';
import { curry, get, has, keys } from 'lodash/fp';

export default curry((dictionary, propertyName) => {
if (!has(propertyName, dictionary)) {
throw new Error(`Tried to get unknown property "${propertyName}"`);
throw new Error(
`Tried to get unknown property "${propertyName}" from an object. Available properties are:\n\n${keys(
dictionary,
).join('\n')}`,
);
}

return get(propertyName, dictionary);
Expand Down
6 changes: 4 additions & 2 deletions packages/fp/src/getSafeFrom/getSafeFrom.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ describe('getSafeFrom', () => {

it('when getting non existing value, throws', () => {
expect(() => {
getSafeFrom({})('someNonExistingProperty');
}).toThrow('Tried to get unknown property "someNonExistingProperty"');
getSafeFrom({ someProperty: 'irrelevant' })('someNonExistingProperty');
}).toThrow(
'Tried to get unknown property "someNonExistingProperty" from an object. Available properties are:\n\nsomeProperty',
);
});

it('when getting existing nested value, returns value', () => {
Expand Down

0 comments on commit 8eef13c

Please sign in to comment.