Skip to content

Commit 3888598

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 3888598

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,22 @@ 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+
} else {
28+
const title = "The resources you provided could not be parsed.";
29+
const details = `The precise error was: "${error.message}".`;
30+
reject(new APIError(400, undefined, title, details));
31+
}
2832
}
2933
});
3034
}
3135

32-
function relationshipObjectFromJSON(json) {
36+
function relationshipObjectFromJSON(json, relationship) {
37+
if (typeof json.data === 'undefined') {
38+
throw new APIError(400, undefined, `No data key was found for the '${relationship}' relationship.`)
39+
}
40+
3341
return new RelationshipObject(linkageFromJSON(json.data));
3442
}
3543

@@ -42,7 +50,7 @@ function resourceFromJSON(json) {
4250

4351
//build RelationshipObjects
4452
for(let key in relationships) {
45-
relationships[key] = relationshipObjectFromJSON(relationships[key]);
53+
relationships[key] = relationshipObjectFromJSON(relationships[key], key);
4654
}
4755

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

0 commit comments

Comments
 (0)