From d2de48579e4ad7ce8efbcf18ddd0dae427ac2fa7 Mon Sep 17 00:00:00 2001 From: Razzib Date: Mon, 21 Feb 2022 11:34:39 +0545 Subject: [PATCH 1/2] fix error message while submitting the form --- src/form/FormController.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/form/FormController.tsx b/src/form/FormController.tsx index 584ee22..cc11c80 100644 --- a/src/form/FormController.tsx +++ b/src/form/FormController.tsx @@ -120,8 +120,8 @@ export class FormController { const response = await res.json(); this.responseHandler(response); } else { - // const response = await res.text(); - throw new Error(res.statusText); + const errorObj= await res.json(); + throw new Error(errorObj.message); } } catch (err) { if (this.errorHandler) { From 07130708b6a383bc70018963c603a743becdfb86 Mon Sep 17 00:00:00 2001 From: Razzib Date: Mon, 21 Feb 2022 14:46:59 +0545 Subject: [PATCH 2/2] fix error handler --- src/form/FormController.tsx | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/form/FormController.tsx b/src/form/FormController.tsx index cc11c80..1ac2864 100644 --- a/src/form/FormController.tsx +++ b/src/form/FormController.tsx @@ -26,7 +26,7 @@ export class FormController { * Keep track of raw input data as provided by the * user */ - private inputs: {[K in keyof T]?: string}; + private inputs: { [K in keyof T]?: string }; /** * The parsers defined for this form @@ -113,15 +113,14 @@ export class FormController { }, body: JSON.stringify(this.state), }); - if (res.status === 500) { + if (res.status >= 500 && res.status <= 600) { const response = await res.json(); throw new Error(response.message); } else if (res.status === 200) { const response = await res.json(); this.responseHandler(response); } else { - const errorObj= await res.json(); - throw new Error(errorObj.message); + throw new Error(res.statusText); } } catch (err) { if (this.errorHandler) { @@ -154,7 +153,7 @@ export class FormController { set(name: K, newValue: T[K]) { // Object only if value changes if (newValue === this.state[name]) return; - this.state = Object.assign({}, this.state, {[name]: newValue}); + this.state = Object.assign({}, this.state, { [name]: newValue }); const listeners = this.listeners[name]; if (listeners) listeners.forEach(l => l(newValue)); }