Merge pull request #348 from mouse-reeve/incoming-errors

Incoming errors
This commit is contained in:
Mouse Reeve 2020-11-12 14:38:57 -08:00 committed by GitHub
commit 211ac4241b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 10 deletions

View file

@ -8,6 +8,10 @@ from django.db.models.fields.related_descriptors \
import ForwardManyToOneDescriptor import ForwardManyToOneDescriptor
class ActivitySerializerError(ValueError):
''' routine problems serializing activitypub json '''
class ActivityEncoder(JSONEncoder): class ActivityEncoder(JSONEncoder):
''' used to convert an Activity object into json ''' ''' used to convert an Activity object into json '''
def default(self, o): def default(self, o):
@ -66,7 +70,8 @@ class ActivityObject:
value = kwargs[field.name] value = kwargs[field.name]
except KeyError: except KeyError:
if field.default == MISSING: if field.default == MISSING:
raise TypeError('Missing required field: %s' % field.name) raise ActivitySerializerError(\
'Missing required field: %s' % field.name)
value = field.default value = field.default
setattr(self, field.name, value) setattr(self, field.name, value)
@ -74,7 +79,7 @@ class ActivityObject:
def to_model(self, model, instance=None): def to_model(self, model, instance=None):
''' convert from an activity to a model instance ''' ''' convert from an activity to a model instance '''
if not isinstance(self, model.activity_serializer): 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 # check for an existing instance, if we're not updating a known obj
if not instance: if not instance:
@ -136,6 +141,7 @@ def resolve_foreign_key(model, remote_id):
).first() ).first()
if not result: 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)) (model.__name__, remote_id))
return result return result

View file

@ -238,7 +238,12 @@ def handle_create(activity):
@app.task @app.task
def handle_delete_status(activity): def handle_delete_status(activity):
''' remove a status ''' ''' remove a status '''
try:
status_id = activity['object']['id'] 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: try:
status = models.Status.objects.select_subclasses().get( status = models.Status.objects.select_subclasses().get(
remote_id=status_id remote_id=status_id
@ -282,7 +287,11 @@ def handle_unfavorite(activity):
@app.task @app.task
def handle_boost(activity): def handle_boost(activity):
''' someone gave us a boost! ''' ''' someone gave us a boost! '''
try:
boost = activitypub.Boost(**activity).to_model(models.Boost) 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: if not boost.user.local:
status_builder.create_notification( status_builder.create_notification(

View file

@ -37,10 +37,14 @@ def get_or_create_remote_user(actor):
def fetch_user_data(actor): def fetch_user_data(actor):
''' load the user's info from the actor url ''' ''' load the user's info from the actor url '''
try:
response = requests.get( response = requests.get(
actor, actor,
headers={'Accept': 'application/activity+json'} headers={'Accept': 'application/activity+json'}
) )
except ConnectionError:
return None
if not response.ok: if not response.ok:
response.raise_for_status() response.raise_for_status()
data = response.json() data = response.json()
@ -83,7 +87,10 @@ def get_avatar(data):
@app.task @app.task
def get_remote_reviews(user_id): def get_remote_reviews(user_id):
''' ingest reviews by a new remote bookwyrm user ''' ''' ingest reviews by a new remote bookwyrm user '''
try:
user = models.User.objects.get(id=user_id) user = models.User.objects.get(id=user_id)
except models.User.DoesNotExist:
return
outbox_page = user.outbox + '?page=true' outbox_page = user.outbox + '?page=true'
response = requests.get( response = requests.get(
outbox_page, outbox_page,