return appropriate HTTP error code (api) #377

This commit is contained in:
Rushabh Mehta 2014-01-20 15:30:03 +05:30
parent f3046abd46
commit 7fa4ef9593
4 changed files with 48 additions and 41 deletions

View file

@ -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()

View file

@ -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()

View file

@ -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

View file

@ -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;