importloggingimportcgiimportosimportpaste.fileappfrompylonsimporttmpl_contextasc,app_globalsasg,request,configfrompylons.controllers.utilimportforwardfrompylons.i18n.translationimport_fromrhodecode.lib.baseimportBaseController,renderfrompylons.middlewareimportmedia_pathfromrhodecode.lib.utilsimportcheck_repoimportrhodecode.lib.helpersashfromrhodecodeimport__version__log=logging.getLogger(__name__)classErrorController(BaseController):""" Generates error documents as and when they are required. The ErrorDocuments middleware forwards to ErrorController when error related status codes are returned from the application. This behaviour can be altered by changing the parameters to the ErrorDocuments middleware in your config/middleware.py file. """def__before__(self):pass#disable all base actions since we don't need them heredefdocument(self):resp=request.environ.get('pylons.original_response')log.debug('### %s ###',resp.status)e=request.environc.serv_p=r'%(protocol)s://%(host)s/'%{'protocol':e.get('wsgi.url_scheme'),'host':e.get('HTTP_HOST'),}c.error_message=cgi.escape(request.GET.get('code',str(resp.status)))c.error_explanation=self.get_error_explanation(resp.status_int)#redirect to when error with given secondsc.redirect_time=0c.redirect_module=_('Home page')# name to what your going to be redirectedc.url_redirect="/"returnrender('/errors/error_document.html')defimg(self,id):"""Serve Pylons' stock images"""returnself._serve_file(os.path.join(media_path,'img',id))defstyle(self,id):"""Serve Pylons' stock stylesheets"""returnself._serve_file(os.path.join(media_path,'style',id))def_serve_file(self,path):"""Call Paste's FileApp (a WSGI application) to serve the file at the specified path """fapp=paste.fileapp.FileApp(path)returnfapp(request.environ,self.start_response)defget_error_explanation(self,code):''' get the error explanations of int codes [400, 401, 403, 404, 500]'''try:code=int(code)except:code=500ifcode==400:return_('The request could not be understood by the server due to malformed syntax.')ifcode==401:return_('Unathorized access to resource')ifcode==403:return_("You don't have permission to view this page")ifcode==404:return_('The resource could not be found')ifcode==500:return_('The server encountered an unexpected condition which prevented it from fulfilling the request.')