Show More
@@ -1,85 +1,110 b'' | |||
|
1 | import logging | |
|
1 | # -*- coding: utf-8 -*- | |
|
2 | """ | |
|
3 | package.rhodecode.controllers.error | |
|
4 | ~~~~~~~~~~~~~~ | |
|
5 | ||
|
6 | RhodeCode error controller | |
|
7 | ||
|
8 | :created_on: Dec 8, 2010 | |
|
9 | :author: marcink | |
|
10 | :copyright: (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com> | |
|
11 | :license: GPLv3, see COPYING for more details. | |
|
12 | """ | |
|
13 | # This program is free software; you can redistribute it and/or | |
|
14 | # modify it under the terms of the GNU General Public License | |
|
15 | # as published by the Free Software Foundation; version 2 | |
|
16 | # of the License or (at your opinion) any later version of the license. | |
|
17 | # | |
|
18 | # This program is distributed in the hope that it will be useful, | |
|
19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
|
20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
|
21 | # GNU General Public License for more details. | |
|
22 | # | |
|
23 | # You should have received a copy of the GNU General Public License | |
|
24 | # along with this program; if not, write to the Free Software | |
|
25 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | |
|
26 | # MA 02110-1301, USA. | |
|
27 | import os | |
|
2 | 28 | import cgi |
|
3 |
import |
|
|
29 | import logging | |
|
4 | 30 | import paste.fileapp |
|
5 | from pylons import tmpl_context as c, app_globals as g, request, config | |
|
6 | from pylons.controllers.util import forward | |
|
31 | ||
|
32 | from pylons import tmpl_context as c, request | |
|
7 | 33 | from pylons.i18n.translation import _ |
|
34 | from pylons.middleware import media_path | |
|
35 | ||
|
8 | 36 | from rhodecode.lib.base import BaseController, render |
|
9 | from pylons.middleware import media_path | |
|
10 | from rhodecode.lib.utils import check_repo | |
|
11 | import rhodecode.lib.helpers as h | |
|
12 | from rhodecode import __version__ | |
|
37 | ||
|
13 | 38 | log = logging.getLogger(__name__) |
|
14 | 39 | |
|
15 | 40 | class ErrorController(BaseController): |
|
16 | """ | |
|
17 | Generates error documents as and when they are required. | |
|
41 | """Generates error documents as and when they are required. | |
|
18 | 42 | |
|
19 | 43 | The ErrorDocuments middleware forwards to ErrorController when error |
|
20 | 44 | related status codes are returned from the application. |
|
21 | 45 | |
|
22 |
This behavio |
|
|
46 | This behavior can be altered by changing the parameters to the | |
|
23 | 47 | ErrorDocuments middleware in your config/middleware.py file. |
|
24 | 48 | """ |
|
49 | ||
|
25 | 50 | def __before__(self): |
|
26 | 51 | pass#disable all base actions since we don't need them here |
|
27 | ||
|
52 | ||
|
28 | 53 | def document(self): |
|
29 | 54 | resp = request.environ.get('pylons.original_response') |
|
30 | ||
|
55 | ||
|
31 | 56 | log.debug('### %s ###', resp.status) |
|
32 | 57 | |
|
33 | 58 | e = request.environ |
|
34 | 59 | c.serv_p = r'%(protocol)s://%(host)s/' % { |
|
35 | 60 | 'protocol': e.get('wsgi.url_scheme'), |
|
36 | 61 | 'host':e.get('HTTP_HOST'), |
|
37 | 62 | } |
|
38 | 63 | |
|
39 | ||
|
64 | ||
|
40 | 65 | c.error_message = cgi.escape(request.GET.get('code', str(resp.status))) |
|
41 | 66 | c.error_explanation = self.get_error_explanation(resp.status_int) |
|
42 | 67 | |
|
43 | 68 | #redirect to when error with given seconds |
|
44 | 69 | c.redirect_time = 0 |
|
45 | 70 | c.redirect_module = _('Home page')# name to what your going to be redirected |
|
46 | 71 | c.url_redirect = "/" |
|
47 | 72 | |
|
48 | 73 | return render('/errors/error_document.html') |
|
49 | 74 | |
|
50 | 75 | |
|
51 | 76 | def img(self, id): |
|
52 | 77 | """Serve Pylons' stock images""" |
|
53 | 78 | return self._serve_file(os.path.join(media_path, 'img', id)) |
|
54 | 79 | |
|
55 | 80 | def style(self, id): |
|
56 | 81 | """Serve Pylons' stock stylesheets""" |
|
57 | 82 | return self._serve_file(os.path.join(media_path, 'style', id)) |
|
58 | 83 | |
|
59 | 84 | def _serve_file(self, path): |
|
60 | 85 | """Call Paste's FileApp (a WSGI application) to serve the file |
|
61 | 86 | at the specified path |
|
62 | 87 | """ |
|
63 | 88 | fapp = paste.fileapp.FileApp(path) |
|
64 | 89 | return fapp(request.environ, self.start_response) |
|
65 | 90 | |
|
66 | 91 | def get_error_explanation(self, code): |
|
67 | 92 | ''' get the error explanations of int codes |
|
68 | 93 | [400, 401, 403, 404, 500]''' |
|
69 | 94 | try: |
|
70 | 95 | code = int(code) |
|
71 | 96 | except: |
|
72 | 97 | code = 500 |
|
73 | 98 | |
|
74 | 99 | if code == 400: |
|
75 | 100 | return _('The request could not be understood by the server due to malformed syntax.') |
|
76 | 101 | if code == 401: |
|
77 | return _('Unathorized access to resource') | |
|
102 | return _('Unauthorized access to resource') | |
|
78 | 103 | if code == 403: |
|
79 | 104 | return _("You don't have permission to view this page") |
|
80 | 105 | if code == 404: |
|
81 | 106 | return _('The resource could not be found') |
|
82 | 107 | if code == 500: |
|
83 | 108 | return _('The server encountered an unexpected condition which prevented it from fulfilling the request.') |
|
84 | 109 | |
|
85 | 110 |
General Comments 0
You need to be logged in to leave comments.
Login now