Repository: fossasia/open-event-server

Issue:
[Sentry] NoResultFound /v1/role-invites
```
NoResultFound: No row was found for one()
  File "flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "flask_rest_jsonapi/decorators.py", line 32, in wrapper
    return func(*args, **kwargs)
  File "flask/views.py", line 84, in view
    return self.dispatch_request(*args, **kwargs)
  File "flask_rest_jsonapi/resource.py", line 68, in dispatch_request
    response = method(*args, **kwargs)
  File "flask_rest_jsonapi/decorators.py", line 56, in wrapper
    return func(*args, **kwargs)
  File "flask_rest_jsonapi/resource.py", line 182, in post
    data, errors = schema.load(json_data)
  File "marshmallow/schema.py", line 578, in load
    result, errors = self._do_load(data, many, partial=partial, postprocess=True)
  File "marshmallow_jsonapi/schema.py", line 186, in _do_load
    result, errors = super(Schema, self)._do_load(data, many, **kwargs)
  File "marshmallow/schema.py", line 673, in _do_load
    field_errors=field_errors)
  File "marshmallow/schema.py", line 925, in _invoke_validators
    pass_original=pass_original)
  File "marshmallow/marshalling.py", line 181, in run_validator
    res = validator_func(output, original_data)
  File "app/api/schema/role_invites.py", line 28, in validate_satus
    role = Role.query.filter_by(id=data['role']).one()
  File "sqlalchemy/orm/query.py", line 2820, in one
    raise orm_exc.NoResultFound("No row was found for one()")
```


Discussion:

Resolving patch:
diff --git a/app/api/schema/role_invites.py b/app/api/schema/role_invites.py
--- a/app/api/schema/role_invites.py
+++ b/app/api/schema/role_invites.py
@@ -1,6 +1,8 @@
 from marshmallow import validates_schema, validate
 from marshmallow_jsonapi import fields
 from marshmallow_jsonapi.flask import Schema, Relationship
+from sqlalchemy.orm.exc import NoResultFound
+from flask_rest_jsonapi.exceptions import ObjectNotFound
 
 from app.models.role_invite import RoleInvite
 from app.api.helpers.exceptions import UnprocessableEntity
@@ -25,16 +27,25 @@ class Meta:
     @validates_schema(pass_original=True)
     def validate_satus(self, data, original_data):
         if 'role' in data and 'role_name' in data:
-            role = Role.query.filter_by(id=data['role']).one()
+            try:
+                role = Role.query.filter_by(id=data['role']).one()
+            except NoResultFound:
+                raise ObjectNotFound({'source': '/data/role'}, "Role not found")
             if role.name != data['role_name']:
                 raise UnprocessableEntity({'pointer': '/data/attributes/role'},
                                           "Role id do not match role name")
         if 'id' in original_data['data']:
-            role_invite = RoleInvite.query.filter_by(id=original_data['data']['id']).one()
+            try:
+                role_invite = RoleInvite.query.filter_by(id=original_data['data']['id']).one()
+            except NoResultFound:
+                raise ObjectNotFound({'source': '/data/id'}, "Role invite not found")
             if 'role' not in data:
                 data['role'] = role_invite.role.id
             if 'role_name' in data:
-                role = Role.query.filter_by(id=data['role']).one()
+                try:
+                    role = Role.query.filter_by(id=data['role']).one()
+                except NoResultFound:
+                    raise ObjectNotFound({'source': '/data/role'}, "Role not found")
                 if role.name != data['role_name']:
                     raise UnprocessableEntity({'pointer': '/data/attributes/role'},
                                               "Role id do not match role name")


Test patch: