~nursix.org/sahana-eden/vita

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# -*- coding: utf-8 -*-

"""
    HTTP Error handler -- implements nicer error pages

    TODO: add/replace the following to your routes.py in web2py directory
    routes_onerror = [
        ('eden/400', '!'),
        ('eden/401', '!'),
        ("eden/*", "/eden/errors/index"),
        ("*/*", "/eden/errors/index"),
    ]

    NOTE: if Eden is installed elsewhere or exists under different name in applications folder,
          just rename it in above list. Comment the last route to disable error
          catching for other apps in the same web2py environment

"""

from gluon.http import defined_status
response.s3.stylesheets.append( "S3/errorpages.css" )

error_messages = {
  "NA":(T("Oops! Something went wrong..."),[]),

  "400":(T("Sorry, I could not understand your request"),
            [T("Check for errors in the URL, maybe the address was mistyped.")]),

  "403":(T("Sorry, that page is forbidden for some reason."),
            [T("Check if the URL is pointing to a directory instead of a webpage."),
             T("Check for errors in the URL, maybe the address was mistyped.")]),

  "404":(T("Sorry, we couldn't find that page."),
            [T("Try checking the URL for errors, maybe it was mistyped."),
             T("Try refreshing the page or hitting the back button on your browser.")]),

  "500":(T("Oops! something went wrong on our side."),
            [T("Try hitting refresh/reload button or trying the URL from the address bar again."),
             T("Please come back after sometime if that doesn\'t help.")]),

  "502":(T("Sorry, something went wrong."),
            [T("The server received an incorrect response from another server that it was accessing to fill the request by the browser."),
             T("Hit the back button on your browser to try again."),
             T("Come back later.")]),

  "503":(T("Sorry, that service is temporary unavailable."),
            [T("This might be due to a temporary overloading or maintenance of the server."),
             T("Hit the back button on your browser to try again."),
             T("Come back later.")]),

  "504":(T("Sorry, things didn't get done on time."),
            [T("The server did not receive a timely response from another server that it was accessing to fill the request by the browser."),
             T("Hit the back button on your browser to try again."),
             T("Come back later. Everyone visiting this site is probably experiencing the same problem as you.")]),
}

def index():
    ''' default generic error page '''

    try:
        code = request.vars["code"]
        description = defined_status[int(code)]
    except KeyError:
        description = "unknown error"
        code = "NA"

    # Send a JSON message if non-interactive request
    request_url = request.vars["request_url"]
    path = request_url.split("/")
    ext = [a for a in path if "." in a]
    if ext:
        fmt = ext[-1].rsplit(".", 1)[1].lower()
        if fmt not in ("html", "iframe", "popup"):
            xml = current.manager.xml
            code = request.vars["code"]
            headers = {"Content-Type":"application/json"}
            raise HTTP(code,
                       body=xml.json_message(False, code, description),
                       **headers)

    details = " %s, %s " % (code, description)
    app = request.application
    try:
        message, suggestions = error_messages[code]
    except KeyError:
        message, suggestions = error_messages["NA"]

    # Retain the HTTP status code on error pages
    response.status = int(code)
    return dict(res=request.vars, message=message, details=details, suggestions=suggestions, app=app)