Search code examples
jqueryajaxangularjsdebuggingturbogears2

Access the interactive backlash/Werkzeug debugger in a failed AJAX request against a TurboGears server


TurboGears features backlash, a great interactive debugger in the browser, based on the Werkzeug Debugger. When debugging is turned on in the server configuration, if a request fails, the server responds with an interactive Web page where you can watch a Python traceback that can be inspected interactively.

However, when developing client-side applications in jQuery or AngularJS, how can I get access to the interactive debugger when an AJAX request fails?


Solution

  • When your AJAX requests fail on the server, you can replace your current document contents with the debug/error document from the servers response. For example, you can do something like the following:

    $.ajax({
        url: 'failing_controller/',
        type: 'POST'
    })
        .fail(function _handleFailure(jqXHR, textStatus, errorThrown) {
            document.open();
            document.write(jqXHR.responseText);
            document.close();
        })
        .success(function _handleSuccess(data, textStatus, jqXHR) {
            // ... handle data ...
        });
    

    You will probably want to replace the failure handler with something more proper in a production environment.