4

我有一个带有 jinja2 的 Google App Engine 应用程序,当我强制 404 错误时,我有这个错误:

errors/default_error.html
Traceback (most recent call last):
   File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1536, in __call__
rv = self.handle_exception(request, response, e)
   File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1596, in handle_exception
return handler(request, response, e)
   File "/base/data/home/apps/s~sandengine/latest.360189283466406656/main.py", line 28, in handle_404
t = jinja2.get_jinja2(app=app).render_template(template, **c)
   File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2_extras/jinja2.py", line 158, in render_template
return self.environment.get_template(_filename).render(**context)
   File "/base/python27_runtime/python27_lib/versions/third_party/jinja2-2.6/jinja2/environment.py", line 719, in get_template
return self._load_template(name, self.make_globals(globals))
   File "/base/python27_runtime/python27_lib/versions/third_party/jinja2-2.6/jinja2/environment.py", line 693, in _load_template
template = self.loader.load(self, name, globals)
   File "/base/python27_runtime/python27_lib/versions/third_party/jinja2-2.6/jinja2/loaders.py", line 115, in load
source, filename, uptodate = self.get_source(environment, name)
   File "/base/python27_runtime/python27_lib/versions/third_party/jinja2-2.6/jinja2/loaders.py", line 180, in get_source
raise TemplateNotFound(template)
TemplateNotFound: errors/default_error.html

这是我的yaml文件:

application: sandengine
version: latest
runtime: python27
api_version: 1
threadsafe: true

default_expiration: "30d"

skip_files:
- ^(.*/)?app\.yaml
- ^(.*/)?app\.yml
- ^(.*/)?index\.yaml
- ^(.*/)?index\.yml
- ^(.*/)?#.*#
- ^(.*/)?.*~
- ^(.*/)?.*\.py[co]
- ^(.*/)?.*/RCS/.*
- ^(.*/)?\..*
- ^(.*/)?tests$
- ^(.*/)?test$
- ^Makefile
- ^COPYING.LESSER
- ^README.rdoc
- \.gitignore
- ^\.git/.*
- \.*\.lint$

builtins:
- appstats: on #/_ah/stats/
- remote_api: on #/_ah/remote_api/

handlers:
- url: /favicon\.ico
  mime_type: image/vnd.microsoft.icon
  static_files: static/favicon.ico
  upload: static/favicon.ico

- url: /apple-touch-icon\.png
  static_files: static/apple-touch-icon.png
  upload: static/apple-touch-icon.png

- url: /(robots\.txt|humans\.txt|crossdomain\.xml)
  static_files: static/\1
  upload: static/(robots\.txt|humans\.txt|crossdomain\.xml)

- url: /img/(.*\.(gif|png|jpg))
  static_files: static/img/\1
  upload: static/img/(.*\.(gif|png|jpg))

- url: /css
  mime_type: text/css
  static_dir: static/css

- url: /js
  mime_type: text/javascript
  static_dir: static/js

- url: /.*
  script: main.app

libraries:
- name: jinja2
  version: "2.6"
- name: webapp2
  version: "2.5.1"
- name: markupsafe
  version: "0.15"

error_handlers:
  - file: templates/errors/default_error.html

  - error_code: over_quota
    file: templates/errors/over_quota.html

  - error_code: dos_api_denial
    file: templates/errors/dos_api_denial.html

  - error_code: timeout
    file: templates/errors/timeout.html

编码:

def handle_404(request, response, exception):
    c = { 'exception': exception.status }
    template = config.error_templates[404]

    t = jinja2.get_jinja2(app=app).render_template(template, **c)
    response.write(t)
    response.set_status(exception.status_int)

app = webapp2.WSGIApplication(debug = os.environ['SERVER_SOFTWARE'].startswith('Dev'), config=config.webapp2_config)

app.error_handlers[404] = handle_404
routes.add_routes(app)

配置文件:

error_templates = {
    404: 'errors/default_error.html',
    500: 'errors/default_error.html',
}

这是文件夹结构

在此处输入图像描述

另一个重要的事情是它在本地机器(SDK)中没有问题,但问题出现在生产中

您可以探索完整的代码,因为这是一个开源代码 提前感谢您的帮助

4

1 回答 1

2

我通过删除 app.yaml 上的“default_error”来修复它

错误处理程序现在只有:

error_handlers:
  - error_code: over_quota
    file: templates/errors/over_quota.html

  - error_code: dos_api_denial
    file: templates/errors/dos_api_denial.html

  - error_code: timeout
    file: templates/errors/timeout.html

我改进了控制 404 错误的代码,添加了 500 错误,其中:

def handle_error(request, response, exception):
    c = { 'exception': str(exception) }
    status_int = hasattr(exception, 'status_int') and exception.status_int or 500
    template = config.error_templates[status_int]
    t = jinja2.get_jinja2(app=app).render_template(template, **c)
    response.write(t)
    response.set_status(status_int)

app = webapp2.WSGIApplication(debug = os.environ['SERVER_SOFTWARE'].startswith('Dev'), config=config.webapp2_config)

app.error_handlers[404] = handle_error
app.error_handlers[500] = handle_error
于 2012-07-14T18:24:42.393 回答