Skip to content

Commit 140a580

Browse files
committed
Better implem
1 parent e88cfa7 commit 140a580

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

templates/react-common/utils/dataAccess.js

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,29 @@ 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.forEach(violation =>
30-
errors[violation.propertyPath]
31-
? (errors[violation.propertyPath] +=
32-
'\n' + errors[violation.propertyPath])
33-
: (errors[violation.propertyPath] = violation.message)
34-
);
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+
);
3539

36-
throw new SubmissionError(errors);
37-
})
38-
.catch(() => {
39-
throw new Error(response.statusText);
40-
});
41-
});
40+
throw new SubmissionError(errors);
41+
},
42+
() => {
43+
throw new Error(response.statusText || 'An error occurred.');
44+
}
45+
);
46+
});
4247
}
4348

4449
export function mercureSubscribe(url, topics) {

0 commit comments

Comments
 (0)