Skip to content

Introducing bugsnag to converge backend #519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions api/bugsnag_error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import bugsnag
import os
from graphql import GraphQLError


class Errors:

def report_errors_bugsnag_and_graphQL(error_message):
if os.getenv("APP_SETTINGS") != "testing":
bugsnag.notify(Exception(error_message), severity="error")
raise GraphQLError(error_message)


return_error = Errors
14 changes: 8 additions & 6 deletions api/devices/schema.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import graphene
from graphql import GraphQLError
from datetime import datetime
from graphene_sqlalchemy import SQLAlchemyObjectType
from sqlalchemy import func, String, cast
Expand All @@ -14,6 +13,7 @@
from helpers.room_filter.room_filter import location_join_room
from helpers.auth.user_details import get_user_from_db
from helpers.auth.admin_roles import admin_roles
from api.bugsnag_error import return_error


class Devices(SQLAlchemyObjectType):
Expand Down Expand Up @@ -43,7 +43,7 @@ def mutate(self, info, **kwargs):
RoomModel.state == 'active'
).first()
if not room_location:
raise GraphQLError("Room not found")
return_error.report_errors_bugsnag_and_graphQL("Room not found")
user = get_user_from_db()
device = DevicesModel(
**kwargs,
Expand Down Expand Up @@ -77,7 +77,8 @@ def mutate(self, info, device_id, **kwargs):
DevicesModel.id == device_id
).first()
if not exact_device:
raise GraphQLError("Device ID not found")
return_error.report_errors_bugsnag_and_graphQL(
"Device ID not found")
update_entity_fields(exact_device, **kwargs)

exact_device.save()
Expand All @@ -102,7 +103,7 @@ def mutate(self, info, device_id, **kwargs):
DevicesModel.id == device_id
).first()
if not exact_device:
raise GraphQLError("Device not found")
return_error.report_errors_bugsnag_and_graphQL("Device not found")
update_entity_fields(exact_device, state="archived", **kwargs)
exact_device.save()
return DeleteDevice(device=exact_device)
Expand Down Expand Up @@ -160,7 +161,7 @@ def resolve_specific_device(self, info, device_id):
device = query.filter(DevicesModel.id == device_id).first()

if not device:
raise GraphQLError("Device not found")
return_error.report_errors_bugsnag_and_graphQL("Device not found")

return device

Expand All @@ -169,7 +170,8 @@ def resolve_device_by_name(self, info, device_name):
devices = Devices.get_query(info)
device_name = ''.join(device_name.split()).lower()
if not device_name:
raise GraphQLError("Please provide the device name")
return_error.report_errors_bugsnag_and_graphQL(
"Please provide the device name")
found_devices = []
for device in devices:
exact_name = ''.join(device.name.split()).lower()
Expand Down
4 changes: 2 additions & 2 deletions api/events/models.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from sqlalchemy import (Column, String, Integer, Boolean, ForeignKey, Enum)
from sqlalchemy.orm import relationship
from sqlalchemy.schema import Sequence
from graphql import GraphQLError

from helpers.database import Base
from utilities.utility import Utility, StateType
from helpers.events_filter.events_filter import (
validate_date_input,
format_range_dates,
)
from api.bugsnag_error import return_error


class Events(Base, Utility):
Expand Down Expand Up @@ -38,7 +38,7 @@ def filter_event(start_date, end_date, room_id=None):
or returns all events otherwise.
"""
def error_message(error):
raise GraphQLError(error)
return_error.report_errors_bugsnag_and_graphQL(error)

validate_date_input(start_date, end_date)

Expand Down
15 changes: 9 additions & 6 deletions api/events/schema.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import graphene
from graphene_sqlalchemy import SQLAlchemyObjectType
from graphql import GraphQLError
import pytz
from dateutil import parser
from datetime import timedelta
Expand Down Expand Up @@ -29,6 +28,7 @@
calendar_dates_format,
empty_string_checker
)
from api.bugsnag_error import return_error

utc = pytz.utc

Expand Down Expand Up @@ -175,7 +175,7 @@ def mutate(self, info, **kwargs):
device_last_seen = parser.parse(
kwargs['start_time']) + timedelta(minutes=10)
except ValueError:
raise GraphQLError("Invalid start time")
return_error.report_errors_bugsnag_and_graphQL("Invalid start time")
update_device_last_activity(
info, room_id, device_last_seen, 'cancel meeting')
if not event:
Expand All @@ -200,7 +200,8 @@ def mutate(self, info, **kwargs):
room_id,
event_reject_reason
):
raise GraphQLError("Event cancelled but email not sent")
return_error.report_errors_bugsnag_and_graphQL(
"Event cancelled but email not sent")
return CancelEvent(event=event)


Expand Down Expand Up @@ -281,7 +282,8 @@ def check_event_in_db(instance, info, event_check, **kwargs):
return room_id, event
elif event and event_check == 'ended':
if event.meeting_end_time:
raise GraphQLError("Event has already ended")
return_error.report_errors_bugsnag_and_graphQL(
"Event has already ended")
event.meeting_end_time = kwargs['meeting_end_time']
event.save()
return room_id, event
Expand Down Expand Up @@ -389,7 +391,7 @@ def resolve_all_events(self, info, **kwargs):
per_page = kwargs.get('per_page')
page, per_page = validate_page_and_per_page(page, per_page)
response = filter_event(
start_date, end_date
start_date, end_date
)
sort_events_by_date(response)

Expand Down Expand Up @@ -422,7 +424,8 @@ def resolve_all_events_by_room(self, info, **kwargs):
calendar_id=calendar_id
).first()
if not room:
raise GraphQLError("No rooms with the given CalendarId")
return_error.report_errors_bugsnag_and_graphQL(
"No rooms with the given CalendarId")
response = filter_event(
start_date, end_date, room.id
)
Expand Down
12 changes: 6 additions & 6 deletions api/location/schema.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import graphene
from sqlalchemy import func
from graphql import GraphQLError
from graphene_sqlalchemy import SQLAlchemyObjectType

from api.location.models import Location as LocationModel
Expand All @@ -20,6 +19,7 @@
from helpers.room_filter.room_filter import room_join_location
from helpers.auth.authentication import Auth
from helpers.auth.admin_roles import admin_roles
from api.bugsnag_error import return_error


class Location(SQLAlchemyObjectType):
Expand Down Expand Up @@ -60,15 +60,15 @@ def mutate(self, info, **kwargs):
template = 'location_success.html'
payload = {
'model': LocationModel, 'field': 'name', 'value': kwargs['name']
}
}
with SaveContextManager(location, 'Location', payload):
if not notification.send_email_notification(
email=email, subject=subject, template=template,
location_name=location.name, user_name=admin_name
):
raise GraphQLError(
return_error.report_errors_bugsnag_and_graphQL(
"Location created but email not sent"
)
)
return CreateLocation(location=location)


Expand All @@ -93,7 +93,7 @@ def mutate(self, info, location_id, **kwargs):
location_object = result.filter(
LocationModel.id == location_id).first()
if not location_object:
raise GraphQLError("Location not found")
return_error.report_errors_bugsnag_and_graphQL("Location not found")
admin_roles.verify_admin_location(location_id)
if "time_zone" in kwargs:
validate_timezone_field(**kwargs)
Expand Down Expand Up @@ -130,7 +130,7 @@ def mutate(self, info, location_id, **kwargs):
location = result.filter(
LocationModel.id == location_id).first() # noqa: E501
if not location:
raise GraphQLError("location not found")
return_error.report_errors_bugsnag_and_graphQL("location not found")
admin_roles.verify_admin_location(location_id)
update_entity_fields(location, state="archived", **kwargs)
location.save()
Expand Down
21 changes: 11 additions & 10 deletions api/office/schema.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import graphene
from graphene_sqlalchemy import SQLAlchemyObjectType
from graphql import GraphQLError
from sqlalchemy import exc, func

from api.office.models import Office as OfficeModel
Expand All @@ -16,6 +15,7 @@
from helpers.pagination.paginate import Paginate, validate_page
from helpers.email.email import notification
from helpers.auth.user_details import get_user_from_db
from api.bugsnag_error import return_error


class Office(SQLAlchemyObjectType):
Expand All @@ -41,7 +41,7 @@ class Arguments:
def mutate(self, info, **kwargs):
location = Location.query.filter_by(id=kwargs['location_id']).first()
if not location:
raise GraphQLError("Location not found")
return_error.report_errors_bugsnag_and_graphQL("Location not found")
admin_roles.verify_admin_location(location_id=kwargs['location_id'])
office = OfficeModel(**kwargs)
admin = get_user_from_db()
Expand All @@ -50,15 +50,16 @@ def mutate(self, info, **kwargs):
admin_name = username.split(".")[0]
payload = {
'model': OfficeModel, 'field': 'name', 'value': kwargs['name']
}
}
with SaveContextManager(
office, 'Office', payload
office, 'Office', payload
):
new_office = kwargs['name']
if not notification.send_email_notification(
email, new_office, location.name, admin_name
):
raise GraphQLError("Office created but Emails not Sent")
return_error.report_errors_bugsnag_and_graphQL(
"Office created but Emails not Sent")
return CreateOffice(office=office)


Expand All @@ -79,7 +80,7 @@ def mutate(self, info, office_id, **kwargs):
exact_office = result.filter(
OfficeModel.id == office_id).first() # noqa: E501
if not exact_office:
raise GraphQLError("Office not found")
return_error.report_errors_bugsnag_and_graphQL("Office not found")

admin_roles.create_rooms_update_delete_office(office_id)
update_entity_fields(exact_office, state="archived", **kwargs)
Expand All @@ -104,13 +105,13 @@ def mutate(self, info, office_id, **kwargs):
result = get_office.filter(OfficeModel.state == "active")
exact_office = result.filter(OfficeModel.id == office_id).first()
if not exact_office:
raise GraphQLError("Office not found")
return_error.report_errors_bugsnag_and_graphQL("Office not found")
admin_roles.create_rooms_update_delete_office(office_id)
try:
update_entity_fields(exact_office, **kwargs)
exact_office.save()
except exc.SQLAlchemyError:
raise GraphQLError("Action Failed")
return_error.report_errors_bugsnag_and_graphQL("Action Failed")

return UpdateOffice(office=exact_office)

Expand All @@ -134,7 +135,7 @@ def resolve_offices(self, info, **kwargs):
func.lower(OfficeModel.name)).limit(
per_page).offset(page * per_page)
if result.count() == 0:
return GraphQLError("No more offices")
return_error.report_errors_bugsnag_and_graphQL("No more offices")
return result


Expand Down Expand Up @@ -166,7 +167,7 @@ def resolve_get_office_by_name(self, info, name):
active_offices = query.filter(OfficeModel.state == "active")
check_office = active_offices.filter(OfficeModel.name == name).first()
if not check_office:
raise GraphQLError("Office Not found")
return_error.report_errors_bugsnag_and_graphQL("Office Not found")
if name == "Epic tower":
exact_query = lagos_office_join_location(active_offices)
result = exact_query.filter(OfficeModel.name == name)
Expand Down
14 changes: 7 additions & 7 deletions api/office_structure/schema.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import graphene
from graphene_sqlalchemy import SQLAlchemyObjectType
from graphql import GraphQLError
from enum import Enum
from sqlalchemy import asc, desc
from api.office_structure.models import OfficeStructure as StructureModel
Expand All @@ -12,6 +11,7 @@
from helpers.auth.admin_roles import admin_roles
from helpers.database import db_session
from utilities.utility import update_entity_fields
from api.bugsnag_error import return_error


class StructureNode(SQLAlchemyObjectType):
Expand Down Expand Up @@ -43,7 +43,7 @@ def mutate(self, info, **kwargs):
StructureModel.id == kwargs['node_id']).first()

if not node:
raise GraphQLError("node not found")
return_error.report_errors_bugsnag_and_graphQL("node not found")

validate_empty_fields(**kwargs)

Expand Down Expand Up @@ -78,7 +78,7 @@ def mutate(self, info, node_id):
exact_node = db_session.query(StructureModel).filter(
StructureModel.id == node_id).first()
if not exact_node:
raise GraphQLError(
return_error.report_errors_bugsnag_and_graphQL(
"The specified node does not exist"
)
db_session.delete(exact_node)
Expand Down Expand Up @@ -111,7 +111,7 @@ def mutate(self, info, node_list):
node['name'] = node.name.strip()
node['tag'] = node.tag.strip()
nodes.append(StructureModel(
**node, location_id=admin_location_id))
**node, location_id=admin_location_id))
db_session.add_all(nodes)
db_session.commit()
return CreateStructure(structure=nodes)
Expand Down Expand Up @@ -146,7 +146,7 @@ class Query(graphene.ObjectType):
node_name=graphene.String(required=True),
order=graphene.Argument(graphene.Enum.from_enum(Order)),
description="Returns the path from root to the node within structure"
)
)

@Auth.user_roles('Admin', 'Default User', 'Super Admin')
def resolve_node_path_by_name(self, info, **kwargs):
Expand All @@ -155,7 +155,7 @@ def resolve_node_path_by_name(self, info, **kwargs):
query = StructureNode.get_query(info)
order = asc if order == 'asc' else desc
node = query.filter(
StructureModel.name.ilike(node_name.strip())).first()
StructureModel.name.ilike(node_name.strip())).first()
if not node:
raise GraphQLError('node not found')
return_error.report_errors_bugsnag_and_graphQL('node not found')
return node.path_to_root(order=order)
Loading