Пример #1
0
def error_ajax(title, description):
    dom = HTMLDoc()
    title = dom.create_element('B')
    title.append(title)
    if description:
        dom.create_element('<BR>')
        dom.append(description)
    return dom.get()
Пример #2
0
def error_page(title, description):
    dom = HTMLDoc()
    html = dom.create_element('html')
    head = html.create_element('head')
    t = head.create_element('title')
    t.append(title)
    body = html.create_element('body')
    h1 = body.create_element('h1')
    h1.append(title)
    if description:
        h2 = body.create_element('h2')
        h2.append(description)
    return dom.get()
Пример #3
0
    def handle_error(self, req, resp, exception, traceback):
        title = None
        description = None

        try:
            resp.status = exception.status
        except AttributeError:
            if isinstance(exception, AccessDenied):
                resp.status = 403
            elif isinstance(exception, NotFound):
                resp.status = 404
            elif isinstance(exception, JSONDecodeError):
                resp.status = 400
            elif isinstance(exception, FieldError):
                resp.status = 400
            elif isinstance(exception, ValidationError):
                resp.status = 400
            else:
                resp.status = 500

        if isinstance(exception, FieldError):
            description = str(exception)

        if title is None:
            try:
                title = exception.title
            except AttributeError:
                if resp.status in HTTP_STATUS_CODES:
                    title = str(
                        resp.status) + ' ' + HTTP_STATUS_CODES[resp.status]
                else:
                    title = exception.__class__.__name__

        if description is None:
            try:
                description = exception.description
            except AttributeError:
                description = str(exception)

        try:
            for header in exception.headers:
                resp.set_header(header, exception.headers[header])
        except AttributeError:
            pass

        if 'error_template' in g:
            return render_template(g, title, description)
        elif resp.content_type is None or 'json' in resp.content_type.lower():
            to_return = {}
            to_return['error'] = {}
            to_return['error']['title'] = title
            to_return['error']['description'] = description

            return to_return

        elif 'html' in resp.content_type.lower():
            dom = HTMLDoc()
            html = dom.create_element('html')
            head = html.create_element('head')
            t = head.create_element('title')
            t.append(resp.status)
            body = html.create_element('body')
            h1 = body.create_element('h1')
            h1.append(title)
            h2 = body.create_element('h2')
            h2.append(description)
            return dom.get()
        else:
            return title + ' ' + description