From 7fa4ef9593cfab0e4047d33e369c757d2b383e19 Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Mon, 20 Jan 2014 15:30:03 +0530 Subject: [PATCH] return appropriate HTTP error code (api) #377 --- webnotes/app.py | 9 ++++--- webnotes/handler.py | 35 +++++++++++++++------------- webnotes/public/js/wn/request.js | 11 +++++---- webnotes/templates/includes/login.js | 34 +++++++++++++++------------ 4 files changed, 48 insertions(+), 41 deletions(-) diff --git a/webnotes/app.py b/webnotes/app.py index 317111e3c4..e2b819337c 100644 --- a/webnotes/app.py +++ b/webnotes/app.py @@ -54,11 +54,7 @@ def application(request): for k, v in (request.form or request.args).iteritems() }) webnotes.local._response = Response() - - try: - webnotes.http_request = webnotes.auth.HTTPRequest() - except webnotes.AuthenticationError, e: - pass + webnotes.http_request = webnotes.auth.HTTPRequest() if webnotes.form_dict.cmd: webnotes.handler.handle() @@ -70,6 +66,9 @@ def application(request): except HTTPException, e: return e + except webnotes.AuthenticationError, e: + webnotes._response.status_code=401 + except webnotes.SessionStopped, e: webnotes.local._response = handle_session_stopped() diff --git a/webnotes/handler.py b/webnotes/handler.py index 080a518ac5..95d28e6631 100755 --- a/webnotes/handler.py +++ b/webnotes/handler.py @@ -68,30 +68,33 @@ def uploadfile(): def handle(): """handle request""" cmd = webnotes.form_dict['cmd'] + + def _error(status_code): + webnotes.errprint(webnotes.utils.get_traceback()) + webnotes._response.status_code = status_code + if webnotes.request_method == "POST": + webnotes.conn.rollback() if cmd!='login': # login executed in webnotes.auth if webnotes.request_method == "POST": webnotes.conn.begin() + status_codes = { + webnotes.PermissionError: 403, + webnotes.AuthenticationError: 401, + webnotes.DoesNotExistError: 404, + webnotes.SessionStopped: 503, + webnotes.OutgoingEmailError: 501 + } + try: execute_cmd(cmd) - except webnotes.ValidationError, e: - webnotes.errprint(webnotes.utils.get_traceback()) - if webnotes.request_method == "POST": - webnotes.conn.rollback() - except webnotes.PermissionError, e: - webnotes.errprint(webnotes.utils.get_traceback()) - webnotes._response.status_code = 403 - if webnotes.request_method == "POST": - webnotes.conn.rollback() - except: - webnotes.errprint(webnotes.utils.get_traceback()) - if webnotes.request_method == "POST": - webnotes.conn and webnotes.conn.rollback() - - if webnotes.request_method == "POST" and webnotes.conn: - webnotes.conn.commit() + except Exception, e: + _error(status_codes.get(e.__class__, 500)) + else: + if webnotes.request_method == "POST" and webnotes.conn: + webnotes.conn.commit() print_response() diff --git a/webnotes/public/js/wn/request.js b/webnotes/public/js/wn/request.js index 5ca9dd5320..8d87eb4890 100644 --- a/webnotes/public/js/wn/request.js +++ b/webnotes/public/js/wn/request.js @@ -58,18 +58,17 @@ wn.request.call = function(opts) { type: 'POST', dataType: opts.dataType || 'json', statusCode: { + 404: function(xhr) { + msgprint("Not Found"); + }, 403: function(xhr) { - wn.request.cleanup(opts, {}); msgprint("Not Permitted"); - opts.error && opts.error(xhr) }, 200: function(data, xhr) { - wn.request.cleanup(opts, data); opts.success && opts.success(data, xhr.responseText); } }, fail: function(xhr, textStatus) { - wn.request.cleanup(opts, {}); opts.error && opts.error(xhr) }, async: opts.async @@ -101,7 +100,9 @@ wn.request.call = function(opts) { }) } - return $.ajax(ajax_args); + return $.ajax(ajax_args).always(function(data) { + wn.request.cleanup(opts, data); + }); } // call execute serverside request diff --git a/webnotes/templates/includes/login.js b/webnotes/templates/includes/login.js index 5cc23a3573..0a691ab01c 100644 --- a/webnotes/templates/includes/login.js +++ b/webnotes/templates/includes/login.js @@ -62,23 +62,27 @@ login.do_login = function(){ url: "/", data: args, dataType: "json", - success: function(data) { - $("#login-spinner").toggle(false); - $('#login_btn').prop("disabled", false); - if(data.message=="Logged In") { - window.location.href = "app.html"; - } else if(data.message=="No App") { - if(localStorage) { - var last_visited = localStorage.getItem("last_visited") || "index"; - localStorage.removeItem("last_visited"); - window.location.href = last_visited; - } else { - window.location.href = "index"; - } - } else { - login.set_message(data.message || data._server_messages); + statusCode: { + 200: function(data) { + if(data.message=="Logged In") { + window.location.href = "app.html"; + } else if(data.message=="No App") { + if(localStorage) { + var last_visited = localStorage.getItem("last_visited") || "index"; + localStorage.removeItem("last_visited"); + window.location.href = last_visited; + } else { + window.location.href = "index"; + } + } + }, + 401: function(xhr, data) { + login.set_message("Invalid Login"); } } + }).always(function(){ + $("#login-spinner").toggle(false); + $('#login_btn').prop("disabled", false); }) return false;