Error handling using JQuery Terminal Plugin and Django with jsonrpc

670 views Asked by At

Just having some fun, and trying to learn both JQuery and Django at the same time. I thought it would be nifty to write a browser based terminal application.

Here's the background:

My console page looks like this:

{% load staticfiles %}
<!DOCTYPE HTML>
<html>
  <head>
    <script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
    <script src="{% static "goat/jquery.terminal-0.7.10.min.js" %}"></script>
    <link rel="stylesheet" type="text/css" href="{% static "goat/jquery.terminal.css" %}">
  </head>
<body>
    <div id="term"></div>
<script>
jQuery(function($) {
    $('#term').terminal("http://goatgruff.com/json/", {
        greetings: "Menu: (R)egister (L)ogin"});
});

</script>
</body>
</html>

My json views.py uses the same examples as the json-rpc does...

from jsonrpc import jsonrpc_method

    @jsonrpc_method('sayHello')
    def whats_the_time(request, name='Lester'):
      return "Hello %s" % name

    @jsonrpc_method('gimmeThat', authenticated=True)
    def something_special(request, secret_data):
      return {'sauce': ['authenticated', 'sauce']}

Cool. So the question:

When I type in my terminal "sayHello Matt" the Ajax call works and responds with "Hello Matt." Just ducky. But if I type in a method that isn't in views.py I get an ugly AJAX error.

I would like to handle this error nicely, and tell the user that there's not method. Since I have to name methods in the view, I can't have an error handler there. I wonder if I should put a try / catch in the urls.py or if my JQuery should handle the error by parsing whatever comes back - I am not sure what that object would look like and how I would get at the result.

A few pointers in the right direction would help me learn.

Thanks,

Matt

1

There are 1 answers

1
Mreider On BEST ANSWER

Ok, I figured it out on my own. First I started monkeying around in the exceptions.py file in jsonrpc. But rather than override the error messages coming from jsonrpc, it seemed better to just make a simple error response from JQuery Terminal. So I just commented the more informative response and return a generic 'invalid command' to save the user from seeing verbosity they wouldn't understand.

      function make_basic_json_rpc_interpreter(url) {
        var service = function(method, params) {
            self.pause();
            $.jrpc(url, method, params, function(json) {
                if (!json.error) {
                    display_object(json.result);
                } else {
                    self.error('&#91;RPC&#93; ' + json.error.message);
                }
                self.resume();
            }, function(xhr, status, error) {
                if (status !== 'abort') {
                    //self.error('&#91;AJAX&#93; ' + status +
                    //               ' - Server reponse is: \n' +
                    //               xhr.responseText);
                    self.error('Invalid Command');
                }
                self.resume();
            });