Skip to content

Commit

Permalink
feat(core): better error message for mapping operation error
Browse files Browse the repository at this point in the history
error message now includes `destinationMemberPath` to know which property on the destination is
failing. The error message also includes the original error which might hint the actual issue at the
`destinationMemberPath` being reported on the error.
  • Loading branch information
nartc committed Jan 30, 2021
1 parent 736f58d commit 550d8e8
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions packages/core/src/lib/map/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,19 @@ function map<
] = propsToMap[i];

// Setup a shortcut function to set destinationMemberPath on destination with value as argument
const setMember = setMemberFn(destinationMemberPath, destination);
const setMember = (valFn: () => unknown) => {
try {
const val = valFn();
return setMemberFn(destinationMemberPath, destination)(val);
} catch (originalError) {
const errorMessage = `
Error at "${destinationMemberPath}" on ${JSON.stringify(destination)}
---------------------------------------------------------------------
Original error: ${originalError}`;
errorHandler.handle(errorMessage);
throw new Error(errorMessage);
}
};

// This destination key is being configured. Push to configuredKeys array
configuredKeys.push(destinationMemberPath);
Expand All @@ -243,7 +255,7 @@ function map<
transformationPreCondPredicate &&
!transformationPreCondPredicate(sourceObj)
) {
setMember(preCondDefaultValue);
setMember(() => preCondDefaultValue);
continue;
}

Expand All @@ -258,13 +270,13 @@ function map<

// if null/undefined
if (mapInitializedValue == null) {
setMember(mapInitializedValue);
setMember(() => mapInitializedValue);
continue;
}

// if isDate
if (mapInitializedValue instanceof Date) {
setMember(new Date(mapInitializedValue));
setMember(() => new Date(mapInitializedValue));
continue;
}

Expand All @@ -273,17 +285,17 @@ function map<
const [first] = mapInitializedValue;
// if first item is a primitive
if (typeof first !== 'object') {
setMember(mapInitializedValue.slice());
setMember(() => mapInitializedValue.slice());
continue;
}

// if first is empty
if (isEmpty(first)) {
setMember([]);
setMember(() => []);
continue;
}

setMember(
setMember(() =>
mapArray(
mapInitializedValue,
nestedDestinationMemberKey,
Expand All @@ -303,7 +315,7 @@ function map<
nestedDestinationMemberKey
);
// for nested model, we do not care about mutate or return. we will always need to return
setMember(
setMember(() =>
map(
mapInitializedValue,
nestedMapping,
Expand All @@ -319,11 +331,11 @@ function map<
}

// if is primitive
setMember(mapInitializedValue);
setMember(() => mapInitializedValue);
continue;
}

setMember(
setMember(() =>
mapMember(
transformationMapFn,
sourceObj,
Expand Down

0 comments on commit 550d8e8

Please sign in to comment.