@@ -31,30 +31,39 @@ def __init__(self, handler, config):
3131 super (ErrorPageApplicationWrapper , self ).__init__ (handler , config )
3232
3333 options = {
34- ' enabled' : False ,
35- ' status_codes' : tuple (),
36- ' handle_exceptions' : not asbool (config .get (' debug' , False )),
37- ' path' : ' /error/document' ,
38- ' content_types' : ["text/html" , None ]
34+ " enabled" : False ,
35+ " status_codes" : tuple (),
36+ " handle_exceptions" : not asbool (config .get (" debug" , False )),
37+ " path" : " /error/document" ,
38+ " content_types" : ["text/html" , None ],
3939 }
40- options .update (coerce_config (config , 'errorpage.' , {
41- 'enabled' : asbool ,
42- 'status_codes' : aslist ,
43- 'handle_exceptions' : asbool ,
44- 'content_types' : aslist
45- }))
46-
47- self .handle_error_enabled = options ['enabled' ]
48- self .handle_status_codes = set (asint (s ) for s in options ['status_codes' ])
49- self .handle_exceptions = options ['handle_exceptions' ]
50- self .handle_error_path = options ['path' ]
51- self .handle_content_types = options ['content_types' ]
40+ options .update (
41+ coerce_config (
42+ config ,
43+ "errorpage." ,
44+ {
45+ "enabled" : asbool ,
46+ "status_codes" : aslist ,
47+ "handle_exceptions" : asbool ,
48+ "content_types" : aslist ,
49+ },
50+ )
51+ )
52+
53+ self .handle_error_enabled = options ["enabled" ]
54+ self .handle_status_codes = set (asint (s ) for s in options ["status_codes" ])
55+ self .handle_exceptions = options ["handle_exceptions" ]
56+ self .handle_error_path = options ["path" ]
57+ self .handle_content_types = options ["content_types" ]
5258
5359 if self .handle_exceptions and 500 not in self .handle_status_codes :
5460 self .handle_status_codes .add (500 )
5561
56- log .debug ('ErrorPageApplicationWrapper enabled: %s -> %s' ,
57- self .handle_error_enabled , options )
62+ log .debug (
63+ "ErrorPageApplicationWrapper enabled: %s -> %s" ,
64+ self .handle_error_enabled ,
65+ options ,
66+ )
5867
5968 @property
6069 def injected (self ):
@@ -67,41 +76,50 @@ def __call__(self, controller, environ, context):
6776 if self .handle_exceptions is False :
6877 raise
6978 # Provide crash details to backlash
70- environ [' backlash.exc_environ' ] = environ .copy ()
71- environ [' backlash.exc_info' ] = sys .exc_info ()
79+ environ [" backlash.exc_environ" ] = environ .copy ()
80+ environ [" backlash.exc_info" ] = sys .exc_info ()
7281 # Force response to a 500 Error, otherwise it will be a 200
7382 resp = context .response
7483 resp .status_code = 500
7584
76- if not environ .get (' tg.status_code_redirect' , True ):
85+ if not environ .get (" tg.status_code_redirect" , True ):
7786 # status_code_redirect disabled per this request
7887 return resp
7988
8089 status_code = resp .status_code
8190 content_type = resp .content_type
82- log .debug ('ErrorPageApplicationWrapper response: %s -> %s @ %s' ,
83- environ ['PATH_INFO' ], status_code , content_type )
84- if status_code in self .handle_status_codes and \
85- (not self .handle_content_types or content_type in self .handle_content_types ):
86- environ ['tg.original_request' ] = context .request .copy ()
87- environ ['tg.original_response' ] = resp
91+ log .debug (
92+ "ErrorPageApplicationWrapper response: %s -> %s @ %s" ,
93+ environ ["PATH_INFO" ],
94+ status_code ,
95+ content_type ,
96+ )
97+ if status_code in self .handle_status_codes and (
98+ not self .handle_content_types or content_type in self .handle_content_types
99+ ):
100+ environ ["tg.original_request" ] = context .request .copy ()
101+ environ ["tg.original_response" ] = resp
88102
89103 # Reset the response, so the error controller starts
90104 # with a clean one and can provide the wished response.
91105 # The original one will be available in environ as
92106 # tg.original_response
93- resp_options = context .config .get ('tg.response_options' ,
94- Response ._DEFAULT_RESPONSE_OPTIONS )
107+ resp_options = context .config .get (
108+ "tg.response_options" , Response ._DEFAULT_RESPONSE_OPTIONS
109+ )
95110 context .response = Response (
96- content_type = resp_options [' content_type' ],
97- charset = resp_options [' charset' ],
98- headers = resp_options [' headers' ],
99- status_code = status_code
111+ content_type = resp_options [" content_type" ],
112+ charset = resp_options [" charset" ],
113+ headers = resp_options [" headers" ],
114+ status_code = status_code ,
100115 )
101116
102- environ ['PATH_INFO' ] = self .handle_error_path
103- log .debug ('ErrorPageApplicationWrapper serving %s:%s' ,
104- controller , self .handle_error_path )
117+ environ ["PATH_INFO" ] = self .handle_error_path
118+ log .debug (
119+ "ErrorPageApplicationWrapper serving %s:%s" ,
120+ controller ,
121+ self .handle_error_path ,
122+ )
105123 resp = self .next_handler (controller , environ , context )
106124
107125 return resp
0 commit comments