comparison piecrust/serving.py @ 229:a951cd4ef361

serve: Print nested exception messages in the dev server.
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 11 Feb 2015 08:27:52 -0800
parents 4f00bb99400e
children 137bcd498ef9
comparison
equal deleted inserted replaced
228:8015fb40c28b 229:a951cd4ef361
147 # Nope. Let's see if it's an actual page. 147 # Nope. Let's see if it's an actual page.
148 try: 148 try:
149 response = self._try_serve_page(app, environ, request) 149 response = self._try_serve_page(app, environ, request)
150 return response(environ, start_response) 150 return response(environ, start_response)
151 except (RouteNotFoundError, SourceNotFoundError) as ex: 151 except (RouteNotFoundError, SourceNotFoundError) as ex:
152 raise NotFound(str(ex)) 152 raise NotFound(str(ex)) from ex
153 except HTTPException: 153 except HTTPException:
154 raise 154 raise
155 except Exception as ex: 155 except Exception as ex:
156 if app.debug: 156 if app.debug:
157 logger.exception(ex) 157 logger.exception(ex)
158 raise 158 raise
159 msg = str(ex) 159 msg = str(ex)
160 logger.error(msg) 160 logger.error(msg)
161 raise InternalServerError(msg) 161 raise InternalServerError(msg) from ex
162 162
163 def _try_special_request(self, environ, request): 163 def _try_special_request(self, environ, request):
164 static_mount = '/__piecrust_static/' 164 static_mount = '/__piecrust_static/'
165 if request.path.startswith(static_mount): 165 if request.path.startswith(static_mount):
166 rel_req_path = request.path[len(static_mount):] 166 rel_req_path = request.path[len(static_mount):]
243 rel_path = tax_page_ref.rel_path 243 rel_path = tax_page_ref.rel_path
244 source = tax_page_ref.source 244 source = tax_page_ref.source
245 fac_metadata = {taxonomy.term_name: term_value} 245 fac_metadata = {taxonomy.term_name: term_value}
246 break 246 break
247 else: 247 else:
248 raise SourceNotFoundError("Can't find path for: %s " 248 raise SourceNotFoundError(
249 "(looked in: %s)" % 249 "Can't find path for: %s (looked in: %s)" %
250 (req_path, [r.source_name for r, _ in routes])) 250 (req_path, [r.source_name for r, _ in routes]))
251 251
252 # Build the page. 252 # Build the page.
253 fac = PageFactory(source, rel_path, fac_metadata) 253 fac = PageFactory(source, rel_path, fac_metadata)
254 page = fac.buildPage() 254 page = fac.buildPage()
356 ext.lstrip('.'), 'text/plain') 356 ext.lstrip('.'), 'text/plain')
357 return response 357 return response
358 358
359 def _handle_error(self, exception, environ, start_response): 359 def _handle_error(self, exception, environ, start_response):
360 code = 500 360 code = 500
361 path = 'error'
362 description = str(exception)
363 if isinstance(exception, HTTPException): 361 if isinstance(exception, HTTPException):
364 code = exception.code 362 code = exception.code
365 description = exception.description 363
366 if isinstance(exception, NotFound): 364 path = 'error'
367 path += '404' 365 if isinstance(exception, NotFound):
366 path += '404'
367
368 descriptions = self._get_exception_descriptions(exception)
369
368 env = Environment(loader=ErrorMessageLoader()) 370 env = Environment(loader=ErrorMessageLoader())
369 template = env.get_template(path) 371 template = env.get_template(path)
370 context = {'details': description} 372 context = {'details': descriptions}
371 response = Response(template.render(context), mimetype='text/html') 373 response = Response(template.render(context), mimetype='text/html')
372 response.status_code = code 374 response.status_code = code
373 return response(environ, start_response) 375 return response(environ, start_response)
376
377 def _get_exception_descriptions(self, exception):
378 desc = []
379 while exception is not None:
380 if isinstance(exception, HTTPException):
381 desc.append(exception.description)
382 else:
383 desc.append(str(exception))
384
385 inner_ex = exception.__cause__
386 if inner_ex is None:
387 inner_ex = exception.__context__
388 exception = inner_ex
389 return desc
374 390
375 391
376 class RouteNotFoundError(Exception): 392 class RouteNotFoundError(Exception):
377 pass 393 pass
378 394