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