##// END OF EJS Templates
another small fix for error controller
marcink -
r312:91ba8489 default
parent child Browse files
Show More
@@ -1,90 +1,92 b''
1 import logging
1 import logging
2 import cgi
2 import cgi
3 import os
3 import os
4 import paste.fileapp
4 import paste.fileapp
5 from pylons import tmpl_context as c, app_globals as g, request, config
5 from pylons import tmpl_context as c, app_globals as g, request, config
6 from pylons.controllers.util import forward
6 from pylons.controllers.util import forward
7 from pylons.i18n.translation import _
7 from pylons.i18n.translation import _
8 from pylons_app.lib.base import BaseController, render
8 from pylons_app.lib.base import BaseController, render
9 from pylons.middleware import media_path
9 from pylons.middleware import media_path
10 from pylons_app.lib.utils import check_repo
10 from pylons_app.lib.utils import check_repo
11 import pylons_app.lib.helpers as h
11 import pylons_app.lib.helpers as h
12 from pylons_app import __version__
12 log = logging.getLogger(__name__)
13 log = logging.getLogger(__name__)
13
14
14 class ErrorController(BaseController):
15 class ErrorController(BaseController):
15 """
16 """
16 Generates error documents as and when they are required.
17 Generates error documents as and when they are required.
17
18
18 The ErrorDocuments middleware forwards to ErrorController when error
19 The ErrorDocuments middleware forwards to ErrorController when error
19 related status codes are returned from the application.
20 related status codes are returned from the application.
20
21
21 This behaviour can be altered by changing the parameters to the
22 This behaviour can be altered by changing the parameters to the
22 ErrorDocuments middleware in your config/middleware.py file.
23 ErrorDocuments middleware in your config/middleware.py file.
23 """
24 """
24 def __before__(self):
25 def __before__(self):
25 pass#disable all base actions since we don't need them here
26 pass#disable all base actions since we don't need them here
26
27
27 def document(self):
28 def document(self):
28 resp = request.environ.get('pylons.original_response')
29 resp = request.environ.get('pylons.original_response')
29
30
30 log.debug('### %s ###', resp.status)
31 log.debug('### %s ###', resp.status)
31
32
32 e = request.environ
33 e = request.environ
33 c.serv_p = r'%(protocol)s://%(host)s/' % {
34 c.serv_p = r'%(protocol)s://%(host)s/' % {
34 'protocol': e.get('wsgi.url_scheme'),
35 'protocol': e.get('wsgi.url_scheme'),
35 'host':e.get('HTTP_HOST'),
36 'host':e.get('HTTP_HOST'),
36 }
37 }
37
38
38 if resp.status_int == 404:
39 if resp.status_int == 404:
39 org_e = request.environ.get('pylons.original_request').environ
40 org_e = request.environ.get('pylons.original_request').environ
40 c.repo_name = repo_name = org_e['PATH_INFO'].split('/')[1]
41 c.repo_name = repo_name = org_e['PATH_INFO'].split('/')[1]
42 c.hg_app_version = __version__
41 c.repo_name_cleaned = h.repo_name_slug(c.repo_name)
43 c.repo_name_cleaned = h.repo_name_slug(c.repo_name)
42 if check_repo(repo_name, g.base_path):
44 if check_repo(repo_name, g.base_path):
43 return render('/errors/error_404.html')
45 return render('/errors/error_404.html')
44
46
45 c.error_message = cgi.escape(request.GET.get('code', str(resp.status)))
47 c.error_message = cgi.escape(request.GET.get('code', str(resp.status)))
46 c.error_explanation = self.get_error_explanation(resp.status_int)
48 c.error_explanation = self.get_error_explanation(resp.status_int)
47
49
48 #redirect to when error with given seconds
50 #redirect to when error with given seconds
49 c.redirect_time = 0
51 c.redirect_time = 0
50 c.redirect_module = _('Home page')# name to what your going to be redirected
52 c.redirect_module = _('Home page')# name to what your going to be redirected
51 c.url_redirect = "/"
53 c.url_redirect = "/"
52
54
53 return render('/errors/error_document.html')
55 return render('/errors/error_document.html')
54
56
55
57
56 def img(self, id):
58 def img(self, id):
57 """Serve Pylons' stock images"""
59 """Serve Pylons' stock images"""
58 return self._serve_file(os.path.join(media_path, 'img', id))
60 return self._serve_file(os.path.join(media_path, 'img', id))
59
61
60 def style(self, id):
62 def style(self, id):
61 """Serve Pylons' stock stylesheets"""
63 """Serve Pylons' stock stylesheets"""
62 return self._serve_file(os.path.join(media_path, 'style', id))
64 return self._serve_file(os.path.join(media_path, 'style', id))
63
65
64 def _serve_file(self, path):
66 def _serve_file(self, path):
65 """Call Paste's FileApp (a WSGI application) to serve the file
67 """Call Paste's FileApp (a WSGI application) to serve the file
66 at the specified path
68 at the specified path
67 """
69 """
68 fapp = paste.fileapp.FileApp(path)
70 fapp = paste.fileapp.FileApp(path)
69 return fapp(request.environ, self.start_response)
71 return fapp(request.environ, self.start_response)
70
72
71 def get_error_explanation(self, code):
73 def get_error_explanation(self, code):
72 ''' get the error explanations of int codes
74 ''' get the error explanations of int codes
73 [400, 401, 403, 404, 500]'''
75 [400, 401, 403, 404, 500]'''
74 try:
76 try:
75 code = int(code)
77 code = int(code)
76 except:
78 except:
77 code = 500
79 code = 500
78
80
79 if code == 400:
81 if code == 400:
80 return _('The request could not be understood by the server due to malformed syntax.')
82 return _('The request could not be understood by the server due to malformed syntax.')
81 if code == 401:
83 if code == 401:
82 return _('Unathorized access to resource')
84 return _('Unathorized access to resource')
83 if code == 403:
85 if code == 403:
84 return _("You don't have permission to view this page")
86 return _("You don't have permission to view this page")
85 if code == 404:
87 if code == 404:
86 return _('The resource could not be found')
88 return _('The resource could not be found')
87 if code == 500:
89 if code == 500:
88 return _('The server encountered an unexpected condition which prevented it from fulfilling the request.')
90 return _('The server encountered an unexpected condition which prevented it from fulfilling the request.')
89
91
90
92
General Comments 0
You need to be logged in to leave comments. Login now