From 0882053a427b813662197f33d9824385f2660510 Mon Sep 17 00:00:00 2001 From: Brain Date: Wed, 24 Aug 2022 15:15:08 +0200 Subject: [PATCH] Handle request errors --- assets/js/contact_form.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/assets/js/contact_form.js b/assets/js/contact_form.js index 758a934c..fd2db687 100644 --- a/assets/js/contact_form.js +++ b/assets/js/contact_form.js @@ -16,10 +16,19 @@ window.addEventListener('DOMContentLoaded', function(event) { mode: 'same-origin', body: formData, }) - .then(response => response.json()) + .then(response => { + if (!response.ok) { + throw new Error('Response was not OK'); + } + + return response.json(); + }) .then(json => { console.log(json); }) + .catch(error => { + console.error('Could not start the session:', error); + }); }); contact_form.addEventListener('submit', function(event) {