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