From 3f31fbe67d4660a8a28f485ba369a8f18f9ed764 Mon Sep 17 00:00:00 2001 From: Davide Alberani Date: Wed, 12 Dec 2018 21:14:00 +0100 Subject: [PATCH] front-end error handling --- static/js/sb.js | 16 ++++++++++++++-- toot-my-t-shirt | 6 +++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/static/js/sb.js b/static/js/sb.js index f9dadeb..5ab0da5 100644 --- a/static/js/sb.js +++ b/static/js/sb.js @@ -75,12 +75,24 @@ function sendData(data) { msg = "something went wrong sending the data: " + response.status; console.log(msg); M.toast({"html": msg}); + } + cancelPhoto(); + return response.json(); + }).then(function(json) { + json = json || {}; + console.log(json); + if (json && json.success) { + msg = "❤ ❤ ❤ photo sent successfully! ❤ ❤ ❤"; + console.log(msg); + M.toast({"html": msg}); } else { - msg = "photo was sent successfully!"; + msg = "something wrong on the backend"; + console.log(msg); + M.toast({"html": msg}); + msg = "the server says: " + json.message; console.log(msg); M.toast({"html": msg}); } - cancelPhoto(); }).catch(function(err) { msg = "something went wrong connecting to server: " + err; console.log(msg); diff --git a/toot-my-t-shirt b/toot-my-t-shirt index fa210ac..5b8f430 100755 --- a/toot-my-t-shirt +++ b/toot-my-t-shirt @@ -91,7 +91,7 @@ class BaseException(Exception): :type message: str :param status: numeric http status code :type status: int""" - def __init__(self, message, status=400): + def __init__(self, message, status=200): super(BaseException, self).__init__(message) self.message = message self.status = status @@ -136,7 +136,7 @@ class BaseHandler(tornado.web.RequestHandler): for key, value in kwargs.items(): setattr(self, key, value) - def build_error(self, message='', status=400): + def build_error(self, message='', status=200): """Build and write an error message. :param message: textual message @@ -145,7 +145,7 @@ class BaseHandler(tornado.web.RequestHandler): :type status: int """ self.set_status(status) - self.write({'error': True, 'message': message}) + self.write({'success': False, 'message': message}) class RootHandler(BaseHandler):