Generalized exception handling for native API

This commit is contained in:
krateng 2022-04-21 15:05:54 +02:00
parent 62208bf668
commit afc78e75b0

View File

@ -57,6 +57,20 @@ errors = {
}) })
} }
def catch_exceptions(func):
def protector(*args,**kwargs):
try:
return func(*args,**kwargs)
except Exception as e:
for etype in errors:
if isinstance(e,etype):
errorhandling = errors[etype](e)
response.status = errorhandling[0]
return errorhandling[1]
protector.__doc__ = func.__doc__
protector.__annotations__ = func.__annotations__
return protector
def add_common_args_to_docstring(filterkeys=False,limitkeys=False,delimitkeys=False,amountkeys=False): def add_common_args_to_docstring(filterkeys=False,limitkeys=False,delimitkeys=False,amountkeys=False):
@ -365,6 +379,7 @@ def track_info_external(artist:Multi[str],**keys):
@api.post("newscrobble") @api.post("newscrobble")
@authenticated_function(alternate=api_key_correct,api=True,pass_auth_result_as='auth_result') @authenticated_function(alternate=api_key_correct,api=True,pass_auth_result_as='auth_result')
@catch_exceptions
def post_scrobble( def post_scrobble(
artist:Multi=None, artist:Multi=None,
artists:list=[], artists:list=[],
@ -406,40 +421,33 @@ def post_scrobble(
# for logging purposes, don't pass values that we didn't actually supply # for logging purposes, don't pass values that we didn't actually supply
rawscrobble = {k:rawscrobble[k] for k in rawscrobble if rawscrobble[k]} rawscrobble = {k:rawscrobble[k] for k in rawscrobble if rawscrobble[k]}
try:
result = database.incoming_scrobble(
rawscrobble,
client='browser' if auth_result.get('doreah_native_auth_check') else auth_result.get('client'),
api='native/v1',
fix=(nofix is None)
)
responsedict = { result = database.incoming_scrobble(
'status': 'success', rawscrobble,
'track': { client='browser' if auth_result.get('doreah_native_auth_check') else auth_result.get('client'),
'artists':result['track']['artists'], api='native/v1',
'title':result['track']['title'] fix=(nofix is None)
} )
responsedict = {
'status': 'success',
'track': {
'artists':result['track']['artists'],
'title':result['track']['title']
} }
if extra_kwargs: }
responsedict['warnings'] = [ if extra_kwargs:
{'type':'invalid_keyword_ignored','value':k, responsedict['warnings'] = [
'desc':"This key was not recognized by the server and has been discarded."} {'type':'invalid_keyword_ignored','value':k,
for k in extra_kwargs 'desc':"This key was not recognized by the server and has been discarded."}
] for k in extra_kwargs
if artist and artists: ]
responsedict['warnings'] = [ if artist and artists:
{'type':'mixed_schema','value':['artist','artists'], responsedict['warnings'] = [
'desc':"These two fields are meant as alternative methods to submit information. Use of both is discouraged, but works at the moment."} {'type':'mixed_schema','value':['artist','artists'],
] 'desc':"These two fields are meant as alternative methods to submit information. Use of both is discouraged, but works at the moment."}
return responsedict ]
except Exception as e: return responsedict
for etype in errors:
if isinstance(e,etype):
errorhandling = errors[etype](e)
response.status = errorhandling[0]
return errorhandling[1]