mirror of
https://github.com/webrecorder/pywb.git
synced 2025-03-24 06:59:52 +01:00
webagg app: support bottle debug properly as opt param
This commit is contained in:
parent
7884d4394b
commit
0399cc1046
@ -1,6 +1,5 @@
|
|||||||
from webagg.inputrequest import DirectWSGIInputRequest, POSTInputRequest
|
from webagg.inputrequest import DirectWSGIInputRequest, POSTInputRequest
|
||||||
from bottle import route, request, response, abort, Bottle
|
from bottle import route, request, response, abort, Bottle, debug as bottle_debug
|
||||||
import bottle
|
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
import traceback
|
import traceback
|
||||||
@ -15,6 +14,10 @@ class ResAggApp(object):
|
|||||||
self.application = Bottle()
|
self.application = Bottle()
|
||||||
self.application.default_error_handler = self.err_handler
|
self.application.default_error_handler = self.err_handler
|
||||||
self.route_dict = {}
|
self.route_dict = {}
|
||||||
|
self.debug = kwargs.get('debug', False)
|
||||||
|
|
||||||
|
if self.debug:
|
||||||
|
bottle_debug(True)
|
||||||
|
|
||||||
@self.application.route('/')
|
@self.application.route('/')
|
||||||
def list_routes():
|
def list_routes():
|
||||||
@ -22,7 +25,7 @@ class ResAggApp(object):
|
|||||||
|
|
||||||
def add_route(self, path, handler):
|
def add_route(self, path, handler):
|
||||||
@self.application.route([path, path + '/<mode:path>'], 'ANY')
|
@self.application.route([path, path + '/<mode:path>'], 'ANY')
|
||||||
@wrap_error
|
@self.wrap_error
|
||||||
def direct_input_request(mode=''):
|
def direct_input_request(mode=''):
|
||||||
params = dict(request.query)
|
params = dict(request.query)
|
||||||
params['mode'] = mode
|
params['mode'] = mode
|
||||||
@ -30,7 +33,7 @@ class ResAggApp(object):
|
|||||||
return handler(params)
|
return handler(params)
|
||||||
|
|
||||||
@self.application.route([path + '/postreq', path + '/<mode:path>/postreq'], 'POST')
|
@self.application.route([path + '/postreq', path + '/<mode:path>/postreq'], 'POST')
|
||||||
@wrap_error
|
@self.wrap_error
|
||||||
def post_fullrequest(mode=''):
|
def post_fullrequest(mode=''):
|
||||||
params = dict(request.query)
|
params = dict(request.query)
|
||||||
params['mode'] = mode
|
params['mode'] = mode
|
||||||
@ -42,7 +45,7 @@ class ResAggApp(object):
|
|||||||
self.route_dict[path + '/postreq'] = handler_dict
|
self.route_dict[path + '/postreq'] = handler_dict
|
||||||
|
|
||||||
def err_handler(self, exc):
|
def err_handler(self, exc):
|
||||||
if bottle.debug:
|
if self.debug:
|
||||||
print(exc)
|
print(exc)
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
response.status = exc.status_code
|
response.status = exc.status_code
|
||||||
@ -51,47 +54,45 @@ class ResAggApp(object):
|
|||||||
response.headers['ResErrors'] = err_msg
|
response.headers['ResErrors'] = err_msg
|
||||||
return err_msg
|
return err_msg
|
||||||
|
|
||||||
|
def wrap_error(self, func):
|
||||||
|
def wrap_func(*args, **kwargs):
|
||||||
|
try:
|
||||||
|
out_headers, res, errs = func(*args, **kwargs)
|
||||||
|
|
||||||
#=============================================================================
|
if out_headers:
|
||||||
def wrap_error(func):
|
for n, v in out_headers.items():
|
||||||
def wrap_func(*args, **kwargs):
|
response.headers[n] = v
|
||||||
try:
|
|
||||||
out_headers, res, errs = func(*args, **kwargs)
|
|
||||||
|
|
||||||
if out_headers:
|
if res:
|
||||||
for n, v in out_headers.items():
|
if errs:
|
||||||
response.headers[n] = v
|
response.headers['ResErrors'] = json.dumps(errs)
|
||||||
|
return res
|
||||||
|
|
||||||
if res:
|
last_exc = errs.pop('last_exc', None)
|
||||||
|
if last_exc:
|
||||||
|
if self.debug:
|
||||||
|
traceback.print_exc()
|
||||||
|
|
||||||
|
response.status = last_exc.status()
|
||||||
|
message = last_exc.msg
|
||||||
|
else:
|
||||||
|
response.status = 404
|
||||||
|
message = 'No Resource Found'
|
||||||
|
|
||||||
|
response.content_type = JSON_CT
|
||||||
|
res = {'message': message}
|
||||||
if errs:
|
if errs:
|
||||||
response.headers['ResErrors'] = json.dumps(errs)
|
res['errors'] = errs
|
||||||
return res
|
|
||||||
|
|
||||||
last_exc = errs.pop('last_exc', None)
|
err_msg = json.dumps(res)
|
||||||
if last_exc:
|
response.headers['ResErrors'] = err_msg
|
||||||
if bottle.debug:
|
return err_msg
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
if self.debug:
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
abort(500, 'Internal Error: ' + str(e))
|
||||||
|
|
||||||
response.status = last_exc.status()
|
return wrap_func
|
||||||
message = last_exc.msg
|
|
||||||
else:
|
|
||||||
response.status = 404
|
|
||||||
message = 'No Resource Found'
|
|
||||||
|
|
||||||
response.content_type = JSON_CT
|
|
||||||
res = {'message': message}
|
|
||||||
if errs:
|
|
||||||
res['errors'] = errs
|
|
||||||
|
|
||||||
err_msg = json.dumps(res)
|
|
||||||
response.headers['ResErrors'] = err_msg
|
|
||||||
return err_msg
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
if bottle.debug:
|
|
||||||
traceback.print_exc()
|
|
||||||
abort(500, 'Internal Error: ' + str(e))
|
|
||||||
|
|
||||||
return wrap_func
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user