Mercurial > kallithea
annotate pylons_app/controllers/error.py @ 101:8b06c420491d
statics moved to pylons.
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Sun, 25 Apr 2010 01:25:54 +0200 |
parents | 9f6300b96380 |
children | 2811259dc12d |
rev | line source |
---|---|
0 | 1 import logging |
2 from paste.urlparser import PkgResourcesParser | |
14 | 3 import paste.fileapp |
87
9f6300b96380
Updated error handling, from mercurial to pylons. + added tempalte for 404
Marcin Kuzminski <marcin@python-blog.com>
parents:
14
diff
changeset
|
4 from pylons import tmpl_context as c, app_globals as g, request, config |
0 | 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): | |
87
9f6300b96380
Updated error handling, from mercurial to pylons. + added tempalte for 404
Marcin Kuzminski <marcin@python-blog.com>
parents:
14
diff
changeset
|
25 c.repos_prefix = config['repos_name'] |
101
8b06c420491d
statics moved to pylons.
Marcin Kuzminski <marcin@python-works.com>
parents:
87
diff
changeset
|
26 |
87
9f6300b96380
Updated error handling, from mercurial to pylons. + added tempalte for 404
Marcin Kuzminski <marcin@python-blog.com>
parents:
14
diff
changeset
|
27 c.repo_name = request.environ['pylons.original_request']\ |
9f6300b96380
Updated error handling, from mercurial to pylons. + added tempalte for 404
Marcin Kuzminski <marcin@python-blog.com>
parents:
14
diff
changeset
|
28 .environ.get('PATH_INFO').split('/')[-1] |
9f6300b96380
Updated error handling, from mercurial to pylons. + added tempalte for 404
Marcin Kuzminski <marcin@python-blog.com>
parents:
14
diff
changeset
|
29 |
0 | 30 def document(self): |
31 resp = request.environ.get('pylons.original_response') | |
14 | 32 log.debug(resp.status) |
87
9f6300b96380
Updated error handling, from mercurial to pylons. + added tempalte for 404
Marcin Kuzminski <marcin@python-blog.com>
parents:
14
diff
changeset
|
33 |
9f6300b96380
Updated error handling, from mercurial to pylons. + added tempalte for 404
Marcin Kuzminski <marcin@python-blog.com>
parents:
14
diff
changeset
|
34 e = request.environ |
9f6300b96380
Updated error handling, from mercurial to pylons. + added tempalte for 404
Marcin Kuzminski <marcin@python-blog.com>
parents:
14
diff
changeset
|
35 c.serv_p = r'%(protocol)s://%(host)s/' % { |
9f6300b96380
Updated error handling, from mercurial to pylons. + added tempalte for 404
Marcin Kuzminski <marcin@python-blog.com>
parents:
14
diff
changeset
|
36 'protocol': e.get('wsgi.url_scheme'), |
9f6300b96380
Updated error handling, from mercurial to pylons. + added tempalte for 404
Marcin Kuzminski <marcin@python-blog.com>
parents:
14
diff
changeset
|
37 'host':e.get('HTTP_HOST'), |
9f6300b96380
Updated error handling, from mercurial to pylons. + added tempalte for 404
Marcin Kuzminski <marcin@python-blog.com>
parents:
14
diff
changeset
|
38 } |
9f6300b96380
Updated error handling, from mercurial to pylons. + added tempalte for 404
Marcin Kuzminski <marcin@python-blog.com>
parents:
14
diff
changeset
|
39 |
9f6300b96380
Updated error handling, from mercurial to pylons. + added tempalte for 404
Marcin Kuzminski <marcin@python-blog.com>
parents:
14
diff
changeset
|
40 if resp.status_int == 404: |
9f6300b96380
Updated error handling, from mercurial to pylons. + added tempalte for 404
Marcin Kuzminski <marcin@python-blog.com>
parents:
14
diff
changeset
|
41 return render('/errors/error_404.html') |
9f6300b96380
Updated error handling, from mercurial to pylons. + added tempalte for 404
Marcin Kuzminski <marcin@python-blog.com>
parents:
14
diff
changeset
|
42 |
0 | 43 c.error_message = cgi.escape(request.GET.get('code', str(resp.status))) |
44 c.error_explanation = self.get_error_explanation(resp.status_int) | |
45 | |
46 #redirect to when error with given seconds | |
14 | 47 c.redirect_time = 0 |
0 | 48 c.redirect_module = _('Home page')# name to what your going to be redirected |
49 c.url_redirect = "/" | |
50 | |
51 return render('/errors/error_document.html') | |
52 | |
14 | 53 |
54 def img(self, id): | |
55 """Serve Pylons' stock images""" | |
56 return self._serve_file(os.path.join(media_path, 'img', id)) | |
57 | |
58 def style(self, id): | |
59 """Serve Pylons' stock stylesheets""" | |
60 return self._serve_file(os.path.join(media_path, 'style', id)) | |
61 | |
0 | 62 def _serve_file(self, path): |
63 """Call Paste's FileApp (a WSGI application) to serve the file | |
64 at the specified path | |
65 """ | |
14 | 66 fapp = paste.fileapp.FileApp(path) |
67 return fapp(request.environ, self.start_response) | |
0 | 68 |
69 def get_error_explanation(self, code): | |
70 ''' get the error explanations of int codes | |
71 [400, 401, 403, 404, 500]''' | |
72 try: | |
73 code = int(code) | |
74 except: | |
75 code = 500 | |
76 | |
77 if code == 400: | |
78 return _('The request could not be understood by the server due to malformed syntax.') | |
79 if code == 401: | |
80 return _('Unathorized access to resource') | |
81 if code == 403: | |
82 return _("You don't have permission to view this page") | |
83 if code == 404: | |
84 return _('The resource could not be found') | |
85 if code == 500: | |
86 return _('The server encountered an unexpected condition which prevented it from fulfilling the request.') | |
87 | |
88 |