Skip to content

Commit

Permalink
fix: Handle Nullish error/response.statusText (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
motform authored Oct 7, 2024
1 parent acd7c0a commit 1467447
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 13 deletions.
10 changes: 2 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "django-bananas",
"version": "4.1.1",
"version": "4.1.2",
"license": "MIT",
"author": "Jonas Lundberg",
"repository": {
Expand All @@ -10,13 +10,7 @@
"description": "React admin frontend for Django.",
"main": "index.js",
"private": true,
"keywords": [
"admin",
"bananas",
"django",
"django-admin",
"react"
],
"keywords": ["admin", "bananas", "django", "django-admin", "react"],
"scripts": {
"start": "webpack-dev-server --config app/webpack.config.js",
"watch": "jest --watchAll --verbose",
Expand Down
2 changes: 1 addition & 1 deletion src/Admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class Admin extends React.Component {
});
} catch (error) {
logger.error("Critical Error: Failed to initialize API client!", error);
const cause = error.response ? error.response.statusText : "Unreachable";
const cause = error?.response?.statusText ?? "Unreachable";
this.admin.error(`Failed to boot: API ${cause}`);
this.setState({ booting: false });
return;
Expand Down
2 changes: 1 addition & 1 deletion src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class APIClient extends Swagger {
const message =
response.obj && response.obj.detail
? response.obj.detail
: `API ${response.statusText}`;
: `API ${response?.statusText ?? "Unknown error"}`;

this.errorHandler(message);
};
Expand Down
7 changes: 4 additions & 3 deletions src/forms/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Form extends React.Component {
this.context.api[route]({
...params,
...passedParams,
data: data || values,
data: data ?? values,
});
const promise = onSubmit
? Promise.resolve(onSubmit({ endpoint, values }))
Expand All @@ -65,12 +65,13 @@ class Form extends React.Component {
return false;
});

return promise.catch(({ response: { statusText, status, obj } }) => {
return promise.catch(({ response: { obj, ...response } }) => {
const errorMessages = {
400: "Please correct the errors on this form.",
};
this.context.admin.error(
errorMessages[status] || `${status} : ${statusText}`
errorMessages?.[response?.status] ??
`${response?.status ?? "Unknown status"} : ${response?.statusText ?? "Unknown cause"}`
);
return normalizeFormErrorData(obj);
});
Expand Down

0 comments on commit 1467447

Please sign in to comment.