0
|
1 import logging
|
|
2 from paste.urlparser import PkgResourcesParser
|
14
|
3 import paste.fileapp
|
0
|
4 from pylons import request, tmpl_context as c
|
|
5 from pylons.controllers.util import forward
|
|
6 from pylons.i18n.translation import _
|
|
7 from pylons_app.lib.base import BaseController, render
|
14
|
8 from pylons.middleware import error_document_template, media_path
|
0
|
9 import cgi
|
14
|
10 import os
|
0
|
11
|
|
12 log = logging.getLogger(__name__)
|
|
13 class ErrorController(BaseController):
|
|
14 """
|
|
15 Generates error documents as and when they are required.
|
|
16
|
|
17 The ErrorDocuments middleware forwards to ErrorController when error
|
|
18 related status codes are returned from the application.
|
|
19
|
|
20 This behaviour can be altered by changing the parameters to the
|
|
21 ErrorDocuments middleware in your config/middleware.py file.
|
|
22 """
|
14
|
23 #
|
|
24 def __before__(self):
|
|
25 pass
|
0
|
26
|
|
27 def document(self):
|
|
28
|
|
29 resp = request.environ.get('pylons.original_response')
|
14
|
30 log.debug(resp.status)
|
0
|
31 c.error_message = cgi.escape(request.GET.get('code', str(resp.status)))
|
|
32 c.error_explanation = self.get_error_explanation(resp.status_int)
|
|
33
|
|
34 c.serv_p = ''.join(['http://', request.environ.get('HTTP_HOST', '')])
|
|
35
|
|
36 #redirect to when error with given seconds
|
14
|
37 c.redirect_time = 0
|
0
|
38 c.redirect_module = _('Home page')# name to what your going to be redirected
|
|
39 c.url_redirect = "/"
|
|
40
|
|
41 return render('/errors/error_document.html')
|
|
42
|
14
|
43
|
|
44 def img(self, id):
|
|
45 """Serve Pylons' stock images"""
|
|
46 return self._serve_file(os.path.join(media_path, 'img', id))
|
|
47
|
|
48 def style(self, id):
|
|
49 """Serve Pylons' stock stylesheets"""
|
|
50 return self._serve_file(os.path.join(media_path, 'style', id))
|
|
51
|
0
|
52 def _serve_file(self, path):
|
|
53 """Call Paste's FileApp (a WSGI application) to serve the file
|
|
54 at the specified path
|
|
55 """
|
14
|
56 fapp = paste.fileapp.FileApp(path)
|
|
57 return fapp(request.environ, self.start_response)
|
0
|
58
|
|
59 def get_error_explanation(self, code):
|
|
60 ''' get the error explanations of int codes
|
|
61 [400, 401, 403, 404, 500]'''
|
|
62 try:
|
|
63 code = int(code)
|
|
64 except:
|
|
65 code = 500
|
|
66
|
|
67 if code == 400:
|
|
68 return _('The request could not be understood by the server due to malformed syntax.')
|
|
69 if code == 401:
|
|
70 return _('Unathorized access to resource')
|
|
71 if code == 403:
|
|
72 return _("You don't have permission to view this page")
|
|
73 if code == 404:
|
|
74 return _('The resource could not be found')
|
|
75 if code == 500:
|
|
76 return _('The server encountered an unexpected condition which prevented it from fulfilling the request.')
|
|
77
|
|
78
|