annotate pylons_app/controllers/error.py @ 172:83c7ee1b5f5c

improved timerproxy with sqllogging, and new way of sqlformat queries
author Marcin Kuzminski <marcin@python-works.com>
date Fri, 21 May 2010 03:01:31 +0200
parents 2811259dc12d
children b68b2246e5a6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
1 import logging
125
2811259dc12d Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents: 101
diff changeset
2 import cgi
2811259dc12d Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents: 101
diff changeset
3 import os
14
923f0e6ab010 change error controller,
Marcin Kuzminski
parents: 0
diff changeset
4 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
5 from pylons import tmpl_context as c, app_globals as g, request, config
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
6 from pylons.controllers.util import forward
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
7 from pylons.i18n.translation import _
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
8 from pylons_app.lib.base import BaseController, render
125
2811259dc12d Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents: 101
diff changeset
9 from pylons.middleware import media_path
2811259dc12d Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents: 101
diff changeset
10 from pylons_app.lib.utils import check_repo
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
11
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
12 log = logging.getLogger(__name__)
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
13 class ErrorController(BaseController):
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
14 """
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
15 Generates error documents as and when they are required.
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
16
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
17 The ErrorDocuments middleware forwards to ErrorController when error
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
18 related status codes are returned from the application.
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
19
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
20 This behaviour can be altered by changing the parameters to the
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
21 ErrorDocuments middleware in your config/middleware.py file.
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
22 """
14
923f0e6ab010 change error controller,
Marcin Kuzminski
parents: 0
diff changeset
23 #
923f0e6ab010 change error controller,
Marcin Kuzminski
parents: 0
diff changeset
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']\
125
2811259dc12d Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents: 101
diff changeset
28 .environ.get('PATH_INFO').split('/')[1]
87
9f6300b96380 Updated error handling, from mercurial to pylons. + added tempalte for 404
Marcin Kuzminski <marcin@python-blog.com>
parents: 14
diff changeset
29
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
30 def document(self):
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
31 resp = request.environ.get('pylons.original_response')
14
923f0e6ab010 change error controller,
Marcin Kuzminski
parents: 0
diff changeset
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 }
125
2811259dc12d Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents: 101
diff changeset
39
2811259dc12d Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents: 101
diff changeset
40
87
9f6300b96380 Updated error handling, from mercurial to pylons. + added tempalte for 404
Marcin Kuzminski <marcin@python-blog.com>
parents: 14
diff changeset
41 if resp.status_int == 404:
125
2811259dc12d Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents: 101
diff changeset
42 if check_repo(c.repo_name, g.base_path):
2811259dc12d Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents: 101
diff changeset
43 return render('/errors/error_404.html')
87
9f6300b96380 Updated error handling, from mercurial to pylons. + added tempalte for 404
Marcin Kuzminski <marcin@python-blog.com>
parents: 14
diff changeset
44
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
45 c.error_message = cgi.escape(request.GET.get('code', str(resp.status)))
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
46 c.error_explanation = self.get_error_explanation(resp.status_int)
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
47
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
48 #redirect to when error with given seconds
14
923f0e6ab010 change error controller,
Marcin Kuzminski
parents: 0
diff changeset
49 c.redirect_time = 0
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
50 c.redirect_module = _('Home page')# name to what your going to be redirected
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
51 c.url_redirect = "/"
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
52
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
53 return render('/errors/error_document.html')
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
54
14
923f0e6ab010 change error controller,
Marcin Kuzminski
parents: 0
diff changeset
55
923f0e6ab010 change error controller,
Marcin Kuzminski
parents: 0
diff changeset
56 def img(self, id):
923f0e6ab010 change error controller,
Marcin Kuzminski
parents: 0
diff changeset
57 """Serve Pylons' stock images"""
923f0e6ab010 change error controller,
Marcin Kuzminski
parents: 0
diff changeset
58 return self._serve_file(os.path.join(media_path, 'img', id))
923f0e6ab010 change error controller,
Marcin Kuzminski
parents: 0
diff changeset
59
923f0e6ab010 change error controller,
Marcin Kuzminski
parents: 0
diff changeset
60 def style(self, id):
923f0e6ab010 change error controller,
Marcin Kuzminski
parents: 0
diff changeset
61 """Serve Pylons' stock stylesheets"""
923f0e6ab010 change error controller,
Marcin Kuzminski
parents: 0
diff changeset
62 return self._serve_file(os.path.join(media_path, 'style', id))
923f0e6ab010 change error controller,
Marcin Kuzminski
parents: 0
diff changeset
63
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
64 def _serve_file(self, path):
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
65 """Call Paste's FileApp (a WSGI application) to serve the file
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
66 at the specified path
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
67 """
14
923f0e6ab010 change error controller,
Marcin Kuzminski
parents: 0
diff changeset
68 fapp = paste.fileapp.FileApp(path)
923f0e6ab010 change error controller,
Marcin Kuzminski
parents: 0
diff changeset
69 return fapp(request.environ, self.start_response)
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
70
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
71 def get_error_explanation(self, code):
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
72 ''' get the error explanations of int codes
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
73 [400, 401, 403, 404, 500]'''
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
74 try:
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
75 code = int(code)
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
76 except:
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
77 code = 500
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
78
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
79 if code == 400:
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
80 return _('The request could not be understood by the server due to malformed syntax.')
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
81 if code == 401:
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
82 return _('Unathorized access to resource')
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
83 if code == 403:
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
84 return _("You don't have permission to view this page")
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
85 if code == 404:
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
86 return _('The resource could not be found')
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
87 if code == 500:
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
88 return _('The server encountered an unexpected condition which prevented it from fulfilling the request.')
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
89
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
90