mirror of
https://github.com/webrecorder/pywb.git
synced 2025-03-24 06:59:52 +01:00
exceptions: add optional url param to WbException, move handler_exception()
into WSGIApp for easier customization
This commit is contained in:
parent
e7957a5cae
commit
89da165467
@ -62,45 +62,50 @@ class WSGIApp(object):
|
|||||||
response = wb_router(env)
|
response = wb_router(env)
|
||||||
|
|
||||||
if not response:
|
if not response:
|
||||||
msg = 'No handler for "{0}"'.format(env['REL_REQUEST_URI'])
|
msg = 'No handler for "{0}".'.format(env['REL_REQUEST_URI'])
|
||||||
raise NotFoundException(msg)
|
raise NotFoundException(msg)
|
||||||
|
|
||||||
except WbException as e:
|
except WbException as e:
|
||||||
response = handle_exception(env, wb_router, e, False)
|
response = self.handle_exception(env, e, False)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
response = handle_exception(env, wb_router, e, True)
|
response = self.handle_exception(env, e, True)
|
||||||
|
|
||||||
return response(env, start_response)
|
return response(env, start_response)
|
||||||
|
|
||||||
|
def handle_exception(self, env, exc, print_trace):
|
||||||
|
error_view = None
|
||||||
|
|
||||||
#=================================================================
|
if hasattr(self.wb_router, 'error_view'):
|
||||||
def handle_exception(env, wb_router, exc, print_trace):
|
error_view = self.wb_router.error_view
|
||||||
error_view = None
|
|
||||||
if hasattr(wb_router, 'error_view'):
|
|
||||||
error_view = wb_router.error_view
|
|
||||||
|
|
||||||
if hasattr(exc, 'status'):
|
if hasattr(exc, 'status'):
|
||||||
status = exc.status()
|
status = exc.status()
|
||||||
else:
|
else:
|
||||||
status = '400 Bad Request'
|
status = '400 Bad Request'
|
||||||
|
|
||||||
if print_trace:
|
if hasattr(exc, 'url'):
|
||||||
import traceback
|
err_url = exc.url
|
||||||
err_details = traceback.format_exc(exc)
|
else:
|
||||||
print err_details
|
err_url = None
|
||||||
else:
|
|
||||||
logging.info(str(exc))
|
|
||||||
err_details = None
|
|
||||||
|
|
||||||
if error_view:
|
if print_trace:
|
||||||
import traceback
|
import traceback
|
||||||
return error_view.render_response(err_msg=str(exc),
|
err_details = traceback.format_exc(exc)
|
||||||
err_details=err_details,
|
print err_details
|
||||||
status=status)
|
else:
|
||||||
else:
|
logging.info(str(exc))
|
||||||
return WbResponse.text_response(status + ' Error: ' + str(exc),
|
err_details = None
|
||||||
status=status)
|
|
||||||
|
if error_view:
|
||||||
|
return error_view.render_response(exc_type=type(exc).__name__,
|
||||||
|
err_msg=str(exc),
|
||||||
|
err_details=err_details,
|
||||||
|
status=status,
|
||||||
|
err_url=err_url)
|
||||||
|
else:
|
||||||
|
return WbResponse.text_response(status + ' Error: ' + str(exc),
|
||||||
|
status=status)
|
||||||
|
|
||||||
#=================================================================
|
#=================================================================
|
||||||
DEFAULT_CONFIG_FILE = 'config.yaml'
|
DEFAULT_CONFIG_FILE = 'config.yaml'
|
||||||
|
@ -2,6 +2,10 @@
|
|||||||
|
|
||||||
#=================================================================
|
#=================================================================
|
||||||
class WbException(Exception):
|
class WbException(Exception):
|
||||||
|
def __init__(self, msg=None, url=None):
|
||||||
|
Exception.__init__(self, msg)
|
||||||
|
self.url = url
|
||||||
|
|
||||||
def status(self):
|
def status(self):
|
||||||
return '500 Internal Server Error'
|
return '500 Internal Server Error'
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user