Skip to content

Commit d5ccc01

Browse files
authored
Merge pull request #133 from dunglas/improve-fetch
[React] Improve fetch
2 parents 0b2800d + 140a580 commit d5ccc01

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

templates/react-common/utils/dataAccess.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,28 @@ export function fetch(id, options = {}) {
2121
return global.fetch(new URL(id, ENTRYPOINT), options).then(response => {
2222
if (response.ok) return response;
2323

24-
return response.json().then(json => {
25-
const error = json['hydra:description'] || response.statusText;
26-
if (!json.violations) throw Error(error);
24+
return response.json().then(
25+
json => {
26+
const error =
27+
json['hydra:description'] ||
28+
json['hydra:title'] ||
29+
'An error occurred.';
30+
if (!json.violations) throw Error(error);
2731

28-
let errors = { _error: error };
29-
json.violations.map(
30-
violation => (errors[violation.propertyPath] = violation.message)
31-
);
32+
let errors = { _error: error };
33+
json.violations.forEach(violation =>
34+
errors[violation.propertyPath]
35+
? (errors[violation.propertyPath] +=
36+
'\n' + errors[violation.propertyPath])
37+
: (errors[violation.propertyPath] = violation.message)
38+
);
3239

33-
throw new SubmissionError(errors);
34-
});
40+
throw new SubmissionError(errors);
41+
},
42+
() => {
43+
throw new Error(response.statusText || 'An error occurred.');
44+
}
45+
);
3546
});
3647
}
3748

0 commit comments

Comments
 (0)