mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2025-01-01 21:08:42 +00:00
Merge pull request #348 from mouse-reeve/incoming-errors
Incoming errors
This commit is contained in:
commit
211ac4241b
3 changed files with 32 additions and 10 deletions
|
@ -8,6 +8,10 @@ from django.db.models.fields.related_descriptors \
|
|||
import ForwardManyToOneDescriptor
|
||||
|
||||
|
||||
class ActivitySerializerError(ValueError):
|
||||
''' routine problems serializing activitypub json '''
|
||||
|
||||
|
||||
class ActivityEncoder(JSONEncoder):
|
||||
''' used to convert an Activity object into json '''
|
||||
def default(self, o):
|
||||
|
@ -66,7 +70,8 @@ class ActivityObject:
|
|||
value = kwargs[field.name]
|
||||
except KeyError:
|
||||
if field.default == MISSING:
|
||||
raise TypeError('Missing required field: %s' % field.name)
|
||||
raise ActivitySerializerError(\
|
||||
'Missing required field: %s' % field.name)
|
||||
value = field.default
|
||||
setattr(self, field.name, value)
|
||||
|
||||
|
@ -74,7 +79,7 @@ class ActivityObject:
|
|||
def to_model(self, model, instance=None):
|
||||
''' convert from an activity to a model instance '''
|
||||
if not isinstance(self, model.activity_serializer):
|
||||
raise TypeError('Wrong activity type for model')
|
||||
raise ActivitySerializerError('Wrong activity type for model')
|
||||
|
||||
# check for an existing instance, if we're not updating a known obj
|
||||
if not instance:
|
||||
|
@ -136,6 +141,7 @@ def resolve_foreign_key(model, remote_id):
|
|||
).first()
|
||||
|
||||
if not result:
|
||||
raise ValueError('Could not resolve remote_id in %s model: %s' % \
|
||||
raise ActivitySerializerError(
|
||||
'Could not resolve remote_id in %s model: %s' % \
|
||||
(model.__name__, remote_id))
|
||||
return result
|
||||
|
|
|
@ -238,7 +238,12 @@ def handle_create(activity):
|
|||
@app.task
|
||||
def handle_delete_status(activity):
|
||||
''' remove a status '''
|
||||
status_id = activity['object']['id']
|
||||
try:
|
||||
status_id = activity['object']['id']
|
||||
except TypeError:
|
||||
# this isn't a great fix, because you hit this when mastadon
|
||||
# is trying to delete a user.
|
||||
return
|
||||
try:
|
||||
status = models.Status.objects.select_subclasses().get(
|
||||
remote_id=status_id
|
||||
|
@ -282,7 +287,11 @@ def handle_unfavorite(activity):
|
|||
@app.task
|
||||
def handle_boost(activity):
|
||||
''' someone gave us a boost! '''
|
||||
boost = activitypub.Boost(**activity).to_model(models.Boost)
|
||||
try:
|
||||
boost = activitypub.Boost(**activity).to_model(models.Boost)
|
||||
except activitypub.ActivitySerializerError:
|
||||
# this probably just means we tried to boost an unknown status
|
||||
return
|
||||
|
||||
if not boost.user.local:
|
||||
status_builder.create_notification(
|
||||
|
|
|
@ -37,10 +37,14 @@ def get_or_create_remote_user(actor):
|
|||
|
||||
def fetch_user_data(actor):
|
||||
''' load the user's info from the actor url '''
|
||||
response = requests.get(
|
||||
actor,
|
||||
headers={'Accept': 'application/activity+json'}
|
||||
)
|
||||
try:
|
||||
response = requests.get(
|
||||
actor,
|
||||
headers={'Accept': 'application/activity+json'}
|
||||
)
|
||||
except ConnectionError:
|
||||
return None
|
||||
|
||||
if not response.ok:
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
|
@ -83,7 +87,10 @@ def get_avatar(data):
|
|||
@app.task
|
||||
def get_remote_reviews(user_id):
|
||||
''' ingest reviews by a new remote bookwyrm user '''
|
||||
user = models.User.objects.get(id=user_id)
|
||||
try:
|
||||
user = models.User.objects.get(id=user_id)
|
||||
except models.User.DoesNotExist:
|
||||
return
|
||||
outbox_page = user.outbox + '?page=true'
|
||||
response = requests.get(
|
||||
outbox_page,
|
||||
|
|
Loading…
Reference in a new issue