From 84efa0e114a6c86ed056ec88daf07f2d30767f75 Mon Sep 17 00:00:00 2001 From: Carl Bennett Date: Mon, 31 Aug 2015 09:01:33 +1200 Subject: [PATCH] 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 --- src/steps/pre-query/parse-request-primary.js | 27 ++++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/steps/pre-query/parse-request-primary.js b/src/steps/pre-query/parse-request-primary.js index 9ead5de0..0f84e8de 100644 --- a/src/steps/pre-query/parse-request-primary.js +++ b/src/steps/pre-query/parse-request-primary.js @@ -22,14 +22,24 @@ 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) { + if (typeof json.data === "undefined") { + throw new APIError(400, undefined, `Missing relationship linkage.`); + } + return new RelationshipObject(linkageFromJSON(json.data)); } @@ -41,8 +51,15 @@ function resourceFromJSON(json) { let relationships = json.relationships || {}; //build RelationshipObjects - for(let key in relationships) { - relationships[key] = relationshipObjectFromJSON(relationships[key]); + let key; + try { + for(key in relationships) { + relationships[key] = relationshipObjectFromJSON(relationships[key], key); + } + } + catch (e) { + e.details = `No data was found for the ${key} relationship.`; + throw e; } return new Resource(json.type, json.id, json.attributes, relationships, json.meta);