Basic
Basic

Reputation: 26766

Completely disable Http 404/500 templates when Debug=False

I'm writing some methods in Django that return json. I'm handling exceptions myself and in certain scenarios, I want to return an Http 500 response with a Json body so the client can extract (amongst other things) an Exception uuid.

This worked fine with Debug = True but when set to False, the following code results in an html error page...

Ret = {"ExceptionId:": "<Exception_uuid>",
       "Message": "Some user-friendly error message"}

return HttpResponse(json.dumps(Ret), content_type="application/json", status=INTERNAL_SERVER_ERROR)

I'm aware of the security implications which is why the Message returned here is in no way related to the exception but is generated by our own code when an exception is raised (or a generic one provided in some circumstances). The uuid is logged (ideally to database but to file if that's not possible) along with stack traces, the real exception information, etc. The user can refer to the exception by uuid when contacting us.

How can I force django not to try and interfere with a fuzzy html page that I don't want or need?

Upvotes: 0

Views: 234

Answers (2)

Akshar Raaj
Akshar Raaj

Reputation: 15211

In your ROOT_URLCONF i.e urls.py add an attribute named handler500.

urls.py

....
....
handler500 = 'myapp.views.get500handler'

myapp/views.py

def get500h(request):
    return HttpResponse("A server error ocurred")

Do not worry about what we are returning from get500h(), your user will still see the json response that you are returning.

Upvotes: 0

Aya
Aya

Reputation: 41950

I want to return an Http 500 response with a Json body so the client can extract (amongst other things) an Exception uuid.

TBH, I wouldn't bank on all web browsers treating non-200 responses as valid XHR responses, so it's probably safer to always return a 200 response when you plan to return a JSON response that you want the browser to parse.

One common way to do this is to return the same structure for all JSON responses, such that the browser can determine if the call succeeded or not. For example, you could return something like this...

{"result": "Hello, world!", "exception": null}

...if it worked, and something like this...

{"result": null, "exception": "You provided incorrect parameters"}

...if it fails. For bonus points, you could even translate the exception into a JavaScript exception, and throw it back to the calling function.

Update

To simplify processing, it's a lot easier if all your AJAX calls are routed through a single function. Something like...

function do_json_call(url)
{
    var json = null;
    // code to get JSON via XHR here
    var result = json['result'];
    var exc = json['exception'];
    if (exc !== null)
    {
        throw exc;
    }
    return result;
}

function func1()
{
    try
    {
        var result = do_json_call("http://localhost/json/func1");
        // Do something
    }
    catch (e)
    {
        alert(e);
    }
}

function func2()
{
    try
    {
        var result = do_json_call("http://localhost/json/func2");
        // Do something
    }
    catch (e)
    {
        alert(e);
    }
}

// etc..

Upvotes: 2

Related Questions