Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handle Nullish error/response.statusText #101

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading