mirror of https://github.com/krateng/maloja.git
Moved exceptions and added handling for more of them
This commit is contained in:
parent
64d4036f55
commit
ac3ca0b5e9
|
@ -40,7 +40,7 @@ api.__apipath__ = "mlj_1"
|
||||||
|
|
||||||
|
|
||||||
errors = {
|
errors = {
|
||||||
database.MissingScrobbleParameters: lambda e: (400,{
|
database.exceptions.MissingScrobbleParameters: lambda e: (400,{
|
||||||
"status":"failure",
|
"status":"failure",
|
||||||
"error":{
|
"error":{
|
||||||
'type':'missing_scrobble_data',
|
'type':'missing_scrobble_data',
|
||||||
|
@ -48,6 +48,14 @@ errors = {
|
||||||
'desc':"The scrobble is missing needed parameters."
|
'desc':"The scrobble is missing needed parameters."
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
|
database.exceptions.MissingEntityParameter: lambda e: (400,{
|
||||||
|
"status":"error",
|
||||||
|
"error":{
|
||||||
|
'type':'missing_entity_parameter',
|
||||||
|
'value':None,
|
||||||
|
'desc':"This API call is not valid without an entity (track or artist)."
|
||||||
|
}
|
||||||
|
}),
|
||||||
database.exceptions.EntityExists: lambda e: (409,{
|
database.exceptions.EntityExists: lambda e: (409,{
|
||||||
"status":"failure",
|
"status":"failure",
|
||||||
"error":{
|
"error":{
|
||||||
|
@ -56,7 +64,16 @@ errors = {
|
||||||
'desc':"This entity already exists in the database. Consider merging instead."
|
'desc':"This entity already exists in the database. Consider merging instead."
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
Exception: lambda e: (500,{
|
database.exceptions.DatabaseNotBuilt: lambda e: (503,{
|
||||||
|
"status":"error",
|
||||||
|
"error":{
|
||||||
|
'type':'server_not_ready',
|
||||||
|
'value':'db_upgrade',
|
||||||
|
'desc':"The database is being upgraded. Please try again later."
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
# for http errors, use their status code
|
||||||
|
Exception: lambda e: ((e.status_code if hasattr(e,'statuscode') else 500),{
|
||||||
"status":"failure",
|
"status":"failure",
|
||||||
"error":{
|
"error":{
|
||||||
'type':'unknown_error',
|
'type':'unknown_error',
|
||||||
|
@ -386,7 +403,7 @@ def artist_info_external(**keys):
|
||||||
@api.get("trackinfo")
|
@api.get("trackinfo")
|
||||||
@catch_exceptions
|
@catch_exceptions
|
||||||
@add_common_args_to_docstring(filterkeys=True)
|
@add_common_args_to_docstring(filterkeys=True)
|
||||||
def track_info_external(artist:Multi[str],**keys):
|
def track_info_external(artist:Multi[str]=[],**keys):
|
||||||
"""Returns information about a track
|
"""Returns information about a track
|
||||||
|
|
||||||
:return: track (Mapping), scrobbles (Integer), position (Integer), medals (Mapping), certification (String), topweeks (Integer)
|
:return: track (Mapping), scrobbles (Integer), position (Integer), medals (Mapping), certification (String), topweeks (Integer)
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
# server
|
# server
|
||||||
from bottle import request, response, FormsDict, HTTPError
|
from bottle import request, response, FormsDict
|
||||||
|
|
||||||
# rest of the project
|
# rest of the project
|
||||||
from ..cleanup import CleanerAgent
|
from ..cleanup import CleanerAgent
|
||||||
|
@ -13,6 +13,7 @@ from ..apis import apikeystore
|
||||||
from . import sqldb
|
from . import sqldb
|
||||||
from . import cached
|
from . import cached
|
||||||
from . import dbcache
|
from . import dbcache
|
||||||
|
from . import exceptions
|
||||||
|
|
||||||
# doreah toolkit
|
# doreah toolkit
|
||||||
from doreah.logging import log
|
from doreah.logging import log
|
||||||
|
@ -42,23 +43,12 @@ dbstatus = {
|
||||||
"rebuildinprogress":False,
|
"rebuildinprogress":False,
|
||||||
"complete":False # information is complete
|
"complete":False # information is complete
|
||||||
}
|
}
|
||||||
class DatabaseNotBuilt(HTTPError):
|
|
||||||
def __init__(self):
|
|
||||||
super().__init__(
|
|
||||||
status=503,
|
|
||||||
body="The Maloja Database is being upgraded to Version 3. This could take quite a long time! (~ 2-5 minutes per 10 000 scrobbles)",
|
|
||||||
headers={"Retry-After":120}
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class MissingScrobbleParameters(Exception):
|
|
||||||
def __init__(self,params=[]):
|
|
||||||
self.params = params
|
|
||||||
|
|
||||||
|
|
||||||
def waitfordb(func):
|
def waitfordb(func):
|
||||||
def newfunc(*args,**kwargs):
|
def newfunc(*args,**kwargs):
|
||||||
if not dbstatus['healthy']: raise DatabaseNotBuilt()
|
if not dbstatus['healthy']: raise exceptions.DatabaseNotBuilt()
|
||||||
return func(*args,**kwargs)
|
return func(*args,**kwargs)
|
||||||
return newfunc
|
return newfunc
|
||||||
|
|
||||||
|
@ -97,7 +87,7 @@ def incoming_scrobble(rawscrobble,fix=True,client=None,api=None,dbconn=None):
|
||||||
missing.append(necessary_arg)
|
missing.append(necessary_arg)
|
||||||
if len(missing) > 0:
|
if len(missing) > 0:
|
||||||
log(f"Invalid Scrobble [Client: {client} | API: {api}]: {rawscrobble} ",color='red')
|
log(f"Invalid Scrobble [Client: {client} | API: {api}]: {rawscrobble} ",color='red')
|
||||||
raise MissingScrobbleParameters(missing)
|
raise exceptions.MissingScrobbleParameters(missing)
|
||||||
|
|
||||||
|
|
||||||
log(f"Incoming scrobble [Client: {client} | API: {api}]: {rawscrobble}")
|
log(f"Incoming scrobble [Client: {client} | API: {api}]: {rawscrobble}")
|
||||||
|
@ -305,6 +295,8 @@ def get_performance(dbconn=None,**keys):
|
||||||
if c["artist"] == artist:
|
if c["artist"] == artist:
|
||||||
rank = c["rank"]
|
rank = c["rank"]
|
||||||
break
|
break
|
||||||
|
else:
|
||||||
|
raise exceptions.MissingEntityParameter()
|
||||||
results.append({"range":rng,"rank":rank})
|
results.append({"range":rng,"rank":rank})
|
||||||
|
|
||||||
return results
|
return results
|
||||||
|
@ -344,6 +336,7 @@ def get_top_tracks(dbconn=None,**keys):
|
||||||
def artist_info(dbconn=None,**keys):
|
def artist_info(dbconn=None,**keys):
|
||||||
|
|
||||||
artist = keys.get('artist')
|
artist = keys.get('artist')
|
||||||
|
if artist is None: raise exceptions.MissingEntityParameter()
|
||||||
|
|
||||||
artist_id = sqldb.get_artist_id(artist,dbconn=dbconn)
|
artist_id = sqldb.get_artist_id(artist,dbconn=dbconn)
|
||||||
artist = sqldb.get_artist(artist_id,dbconn=dbconn)
|
artist = sqldb.get_artist(artist_id,dbconn=dbconn)
|
||||||
|
@ -388,6 +381,7 @@ def artist_info(dbconn=None,**keys):
|
||||||
def track_info(dbconn=None,**keys):
|
def track_info(dbconn=None,**keys):
|
||||||
|
|
||||||
track = keys.get('track')
|
track = keys.get('track')
|
||||||
|
if track is None: raise exceptions.MissingEntityParameter()
|
||||||
|
|
||||||
track_id = sqldb.get_track_id(track,dbconn=dbconn)
|
track_id = sqldb.get_track_id(track,dbconn=dbconn)
|
||||||
track = sqldb.get_track(track_id,dbconn=dbconn)
|
track = sqldb.get_track(track_id,dbconn=dbconn)
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
from bottle import HTTPError
|
||||||
|
|
||||||
class EntityExists(Exception):
|
class EntityExists(Exception):
|
||||||
def __init__(self,entitydict):
|
def __init__(self,entitydict):
|
||||||
self.entitydict = entitydict
|
self.entitydict = entitydict
|
||||||
|
@ -8,3 +10,20 @@ class TrackExists(EntityExists):
|
||||||
|
|
||||||
class ArtistExists(EntityExists):
|
class ArtistExists(EntityExists):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class DatabaseNotBuilt(HTTPError):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__(
|
||||||
|
status=503,
|
||||||
|
body="The Maloja Database is being upgraded to Version 3. This could take quite a long time! (~ 2-5 minutes per 10 000 scrobbles)",
|
||||||
|
headers={"Retry-After":120}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class MissingScrobbleParameters(Exception):
|
||||||
|
def __init__(self,params=[]):
|
||||||
|
self.params = params
|
||||||
|
|
||||||
|
class MissingEntityParameter(Exception):
|
||||||
|
pass
|
||||||
|
|
Loading…
Reference in New Issue