Skip to content

Commit 84efa0e

Browse files
committed
Improve error handling for poorly formed relationships
Tells the user what they did wrong when they forget to wrap their relationship changes in a `{ data: ... }` object
1 parent 60e6931 commit 84efa0e

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

src/steps/pre-query/parse-request-primary.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,24 @@ export default function(data, parseAsLinkage) {
2222
}
2323

2424
catch (error) {
25-
const title = "The resources you provided could not be parsed.";
26-
const details = `The precise error was: "${error.message}".`;
27-
reject(new APIError(400, undefined, title, details));
25+
if (error instanceof APIError) {
26+
reject(error);
27+
}
28+
29+
else {
30+
const title = "The resources you provided could not be parsed.";
31+
const details = `The precise error was: "${error.message}".`;
32+
reject(new APIError(400, undefined, title, details));
33+
}
2834
}
2935
});
3036
}
3137

3238
function relationshipObjectFromJSON(json) {
39+
if (typeof json.data === "undefined") {
40+
throw new APIError(400, undefined, `Missing relationship linkage.`);
41+
}
42+
3343
return new RelationshipObject(linkageFromJSON(json.data));
3444
}
3545

@@ -41,8 +51,15 @@ function resourceFromJSON(json) {
4151
let relationships = json.relationships || {};
4252

4353
//build RelationshipObjects
44-
for(let key in relationships) {
45-
relationships[key] = relationshipObjectFromJSON(relationships[key]);
54+
let key;
55+
try {
56+
for(key in relationships) {
57+
relationships[key] = relationshipObjectFromJSON(relationships[key], key);
58+
}
59+
}
60+
catch (e) {
61+
e.details = `No data was found for the ${key} relationship.`;
62+
throw e;
4663
}
4764

4865
return new Resource(json.type, json.id, json.attributes, relationships, json.meta);

0 commit comments

Comments
 (0)