Search
 
SCRIPT & CODE EXAMPLE
 

SQL

ajax error

$.ajax({
    type: "post", url: "/SomeController/SomeAction",
    success: function (data, text) {
        //...
    },
    error: function (request, status, error) {
        alert(request.responseText);
    }
});
Comment

ajax error exception handeling

from django.conf import settings
from django.http import HttpResponse, Http404
from django.db.models.base import ObjectDoesNotExist
from django.utils.translation import ugettext as _

from snippet 800 import JSONEncoder
json_encode = JSONEncoder().encode

class AjaxErrorMiddleware(object):
    '''Return AJAX errors to the browser in a sensible way.

    Includes some code from http://www.djangosnippets.org/snippets/650/
    '''

    # Some useful errors that this middleware will catch.
    class InputError(Exception):
        def __init__(self, message):
            self.message = message

    class ParameterMissingError(InputError):
        def __init__(self, param):
            super(AjaxErrorMiddleware.ParameterMissingError, self).__init__(
                _('Required parameter missing: %s') % param)


    def process_exception(self, request, exception):
        if not request.is_ajax(): return

        if isinstance(exception, (ObjectDoesNotExist, Http404)):
            return self.not_found(request, exception)

        if isinstance(exception, AjaxErrorMiddleware.InputError):
            return self.bad_request(request, exception)

        return self.server_error(request, exception)
    

    def serialize_error(self, status, message):
        return HttpResponse(json_encode({
                    'status': status,
                    'message': message}),
                            status=status)

    
    def not_found(self, request, exception):
        return self.serialize_error(404, str(exception))

    
    def bad_request(self, request, exception):
        return self.serialize_error(400, exception.message)


    def server_error(self, request, exception):
        if settings.DEBUG:
            import sys, traceback
            (exc_type, exc_info, tb) = sys.exc_info()
            message = "%s
" % exc_type.__name__
            message += "%s

" % exc_info
            message += "TRACEBACK:
"    
            for tb in traceback.format_tb(tb):
                message += "%s
" % tb
            return self.serialize_error(500, message)
        else:
            return self.serialize_error(500, _('Internal error'))
Comment

PREVIOUS NEXT
Code Example
Sql :: how to check rollback status in oracle 
Sql :: sql server set complex constraints 
Sql :: order records by nearby cordinates sql 
Sql :: sql int vs integer 
Sql :: how to select only first 3 characters in sql 
Sql :: mysql and or 
Sql :: SQL - Row Number into Alphabetical characters 
Sql :: mysql config address 
Sql :: mysql set session timeout 
Sql :: oracle 11g default profile values 
Sql :: cursors in db2 
Sql :: create sql table from script inline primary key constraint 
Sql :: create backup 
Sql :: query archive mode 
Sql :: regex any word except sql 
Sql :: mysql create database if not exists 
Sql :: how to create a new shema using query languaage 
Sql :: sql fetch next 10 rows pdo 
Sql :: mysql top percent 
Sql :: sql delete row from table where id 
Sql :: SQL RIGHT JOIN With AS Alias 
Sql :: id INT NOT NULL AUTO_INCREMENT 
Sql :: find invalid datetime field 
Sql :: combine islands dates sql 
Sql :: oracle c# multiple update sql 
Sql :: redshift alter table alter column set not null 
Sql :: 9999 
Sql :: MySQL Quartiles in SQL query 
Sql :: get item by composite primary key mysql 
Sql :: How To Execute SQL Select Statements 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =