From fccc48aca5de1bf5ba3688463a74ef5c34d00208 Mon Sep 17 00:00:00 2001 From: motform Date: Mon, 7 Oct 2024 16:45:42 +0200 Subject: [PATCH 1/2] fix: Handle Nullish `error`/`response.statusText` --- package.json | 10 ++-------- src/Admin.js | 2 +- src/api.js | 2 +- src/forms/Form.js | 7 ++++--- 4 files changed, 8 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index d3d6fbc..1ab0d23 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "django-bananas", - "version": "4.1.1", + "version": "4.1.2", "license": "MIT", "author": "Jonas Lundberg", "repository": { @@ -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", diff --git a/src/Admin.js b/src/Admin.js index 8dde9b8..4f41702 100644 --- a/src/Admin.js +++ b/src/Admin.js @@ -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; diff --git a/src/api.js b/src/api.js index fc4bb7d..ed3b0f9 100644 --- a/src/api.js +++ b/src/api.js @@ -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); }; diff --git a/src/forms/Form.js b/src/forms/Form.js index ebd1a96..4f0c6b6 100644 --- a/src/forms/Form.js +++ b/src/forms/Form.js @@ -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 })) @@ -65,12 +65,13 @@ class Form extends React.Component { return false; }); - return promise.catch(({ response: { statusText, status, obj } }) => { + return promise.catch(({ response: { obj } }) => { 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); }); From 231a63b68732b43bb9901b522f182f08bdb23eb3 Mon Sep 17 00:00:00 2001 From: motform Date: Mon, 7 Oct 2024 16:55:34 +0200 Subject: [PATCH 2/2] javascript is not a typed programming langauge --- src/forms/Form.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/forms/Form.js b/src/forms/Form.js index 4f0c6b6..2598d88 100644 --- a/src/forms/Form.js +++ b/src/forms/Form.js @@ -65,7 +65,7 @@ class Form extends React.Component { return false; }); - return promise.catch(({ response: { obj } }) => { + return promise.catch(({ response: { obj, ...response } }) => { const errorMessages = { 400: "Please correct the errors on this form.", };