comparison pylons_app/controllers/error.py @ 0:564e40829f80

initial commit.
author Marcin Kuzminski
date Thu, 18 Feb 2010 13:01:57 +0100
parents
children 923f0e6ab010
comparison
equal deleted inserted replaced
-1:000000000000 0:564e40829f80
1 import logging
2 from paste.urlparser import PkgResourcesParser
3 from pylons import request, tmpl_context as c
4 from pylons.controllers.util import forward
5 from pylons.i18n.translation import _
6 from pylons_app.lib.base import BaseController, render
7 import cgi
8
9 log = logging.getLogger(__name__)
10 class ErrorController(BaseController):
11 """
12 Generates error documents as and when they are required.
13
14 The ErrorDocuments middleware forwards to ErrorController when error
15 related status codes are returned from the application.
16
17 This behaviour can be altered by changing the parameters to the
18 ErrorDocuments middleware in your config/middleware.py file.
19 """
20
21 def document(self):
22
23 resp = request.environ.get('pylons.original_response')
24 c.error_message = cgi.escape(request.GET.get('code', str(resp.status)))
25 c.error_explanation = self.get_error_explanation(resp.status_int)
26
27 c.serv_p = ''.join(['http://', request.environ.get('HTTP_HOST', '')])
28
29 #redirect to when error with given seconds
30 c.redirect_time = 5
31 c.redirect_module = _('Home page')# name to what your going to be redirected
32 c.url_redirect = "/"
33
34 return render('/errors/error_document.html')
35
36 def _serve_file(self, path):
37 """Call Paste's FileApp (a WSGI application) to serve the file
38 at the specified path
39 """
40 request.environ['PATH_INFO'] = '/%s' % path
41 return forward(PkgResourcesParser('pylons', 'pylons'))
42
43 def get_error_explanation(self, code):
44 ''' get the error explanations of int codes
45 [400, 401, 403, 404, 500]'''
46 try:
47 code = int(code)
48 except:
49 code = 500
50
51 if code == 400:
52 return _('The request could not be understood by the server due to malformed syntax.')
53 if code == 401:
54 return _('Unathorized access to resource')
55 if code == 403:
56 return _("You don't have permission to view this page")
57 if code == 404:
58 return _('The resource could not be found')
59 if code == 500:
60 return _('The server encountered an unexpected condition which prevented it from fulfilling the request.')
61
62