Skip to content

Commit

Permalink
Improve error handling for poorly formed relationships
Browse files Browse the repository at this point in the history
Tells the user what they did wrong when they forget to wrap their
relationship changes in a `{ data: ... }` object
  • Loading branch information
carlbennettnz committed Aug 30, 2015
1 parent 60e6931 commit 3888598
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/steps/pre-query/parse-request-primary.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,22 @@ export default function(data, parseAsLinkage) {
}

catch (error) {
const title = "The resources you provided could not be parsed.";
const details = `The precise error was: "${error.message}".`;
reject(new APIError(400, undefined, title, details));
if (error instanceof APIError) {
reject(error);
} else {
const title = "The resources you provided could not be parsed.";
const details = `The precise error was: "${error.message}".`;
reject(new APIError(400, undefined, title, details));
}
}
});
}

function relationshipObjectFromJSON(json) {
function relationshipObjectFromJSON(json, relationship) {
if (typeof json.data === 'undefined') {
throw new APIError(400, undefined, `No data key was found for the '${relationship}' relationship.`)
}

return new RelationshipObject(linkageFromJSON(json.data));
}

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

//build RelationshipObjects
for(let key in relationships) {
relationships[key] = relationshipObjectFromJSON(relationships[key]);
relationships[key] = relationshipObjectFromJSON(relationships[key], key);
}

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

0 comments on commit 3888598

Please sign in to comment.