Browse Source

return appropriate HTTP error code (api) #377

version-14
Rushabh Mehta 11 years ago
parent
commit
7fa4ef9593
4 changed files with 48 additions and 41 deletions
  1. +4
    -5
      webnotes/app.py
  2. +19
    -16
      webnotes/handler.py
  3. +6
    -5
      webnotes/public/js/wn/request.js
  4. +19
    -15
      webnotes/templates/includes/login.js

+ 4
- 5
webnotes/app.py View File

@@ -54,11 +54,7 @@ def application(request):
for k, v in (request.form or request.args).iteritems() }) for k, v in (request.form or request.args).iteritems() })
webnotes.local._response = Response() 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: if webnotes.form_dict.cmd:
webnotes.handler.handle() webnotes.handler.handle()
@@ -70,6 +66,9 @@ def application(request):
except HTTPException, e: except HTTPException, e:
return e return e
except webnotes.AuthenticationError, e:
webnotes._response.status_code=401
except webnotes.SessionStopped, e: except webnotes.SessionStopped, e:
webnotes.local._response = handle_session_stopped() webnotes.local._response = handle_session_stopped()


+ 19
- 16
webnotes/handler.py View File

@@ -68,30 +68,33 @@ def uploadfile():
def handle(): def handle():
"""handle request""" """handle request"""
cmd = webnotes.form_dict['cmd'] 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': if cmd!='login':
# login executed in webnotes.auth # login executed in webnotes.auth
if webnotes.request_method == "POST": if webnotes.request_method == "POST":
webnotes.conn.begin() webnotes.conn.begin()
status_codes = {
webnotes.PermissionError: 403,
webnotes.AuthenticationError: 401,
webnotes.DoesNotExistError: 404,
webnotes.SessionStopped: 503,
webnotes.OutgoingEmailError: 501
}
try: try:
execute_cmd(cmd) 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() print_response()




+ 6
- 5
webnotes/public/js/wn/request.js View File

@@ -58,18 +58,17 @@ wn.request.call = function(opts) {
type: 'POST', type: 'POST',
dataType: opts.dataType || 'json', dataType: opts.dataType || 'json',
statusCode: { statusCode: {
404: function(xhr) {
msgprint("Not Found");
},
403: function(xhr) { 403: function(xhr) {
wn.request.cleanup(opts, {});
msgprint("Not Permitted"); msgprint("Not Permitted");
opts.error && opts.error(xhr)
}, },
200: function(data, xhr) { 200: function(data, xhr) {
wn.request.cleanup(opts, data);
opts.success && opts.success(data, xhr.responseText); opts.success && opts.success(data, xhr.responseText);
} }
}, },
fail: function(xhr, textStatus) { fail: function(xhr, textStatus) {
wn.request.cleanup(opts, {});
opts.error && opts.error(xhr) opts.error && opts.error(xhr)
}, },
async: opts.async 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 // call execute serverside request


+ 19
- 15
webnotes/templates/includes/login.js View File

@@ -62,23 +62,27 @@ login.do_login = function(){
url: "/", url: "/",
data: args, data: args,
dataType: "json", 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; return false;


Loading…
Cancel
Save