From bec89ebabecafb7760a54a034ad1a88cf4bf8216 Mon Sep 17 00:00:00 2001 From: Hermann Blum <hermannsblum@yahoo.de> Date: Sat, 2 Dec 2017 02:11:40 +0100 Subject: [PATCH] add error handling for patch and post responses --- src/views/editView.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/views/editView.js b/src/views/editView.js index f72aad1..9fa7716 100644 --- a/src/views/editView.js +++ b/src/views/editView.js @@ -24,8 +24,7 @@ export class EditView extends ItemView { * * Provides Methods: * - bind(attrs): binds a form-field against this.data - * - patchOnClick: onclick-function for patching - * - postOnClick: onclick-function for posting + * - submit */ constructor(vnode, resource, valid = true) { super(resource); @@ -126,6 +125,21 @@ export class EditView extends ItemView { apiSession(request).then((response) => { this.callback(response); + }).catch((error) => { + // Process the API error + const { response } = error; + if (response.status === 422) { + // there are problems with some fields, display them + Object.keys(response.data._issues).forEach((field) => { + this.errors[field] = [response.data._issues[field]]; + }); + m.redraw(); + } else if (response.status === 403) { + // Unauthorized + m.route.set('/login'); + } else { + console.log(error); + } }); }); } else { -- GitLab