Skip to content

Commit 0b84f48

Browse files
CV3-92-story(bugsnagconfigure):introducing bugsnag to converge backend
- write logic to run functionality and log errors in dashboard - verify that errors are not coming on running tests [Delivers CV3-92]
1 parent 03c5c13 commit 0b84f48

38 files changed

+338
-233
lines changed

api/bugsnag_error.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import bugsnag
2+
import os
3+
from graphql import GraphQLError
4+
5+
6+
class Errors:
7+
8+
def report_errors_bugsnag_and_graphQL(error_message):
9+
if os.getenv("APP_SETTINGS") != "testing":
10+
bugsnag.notify(Exception(error_message), severity="error")
11+
raise GraphQLError(error_message)
12+
13+
14+
return_error = Errors

api/devices/schema.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import graphene
2-
from graphql import GraphQLError
32
from datetime import datetime
43
from graphene_sqlalchemy import SQLAlchemyObjectType
54
from sqlalchemy import func, String, cast
@@ -14,6 +13,7 @@
1413
from helpers.room_filter.room_filter import location_join_room
1514
from helpers.auth.user_details import get_user_from_db
1615
from helpers.auth.admin_roles import admin_roles
16+
from api.bugsnag_error import return_error
1717

1818

1919
class Devices(SQLAlchemyObjectType):
@@ -43,7 +43,7 @@ def mutate(self, info, **kwargs):
4343
RoomModel.state == 'active'
4444
).first()
4545
if not room_location:
46-
raise GraphQLError("Room not found")
46+
return_error.report_errors_bugsnag_and_graphQL("Room not found")
4747
user = get_user_from_db()
4848
device = DevicesModel(
4949
**kwargs,
@@ -77,7 +77,8 @@ def mutate(self, info, device_id, **kwargs):
7777
DevicesModel.id == device_id
7878
).first()
7979
if not exact_device:
80-
raise GraphQLError("Device ID not found")
80+
return_error.report_errors_bugsnag_and_graphQL(
81+
"Device ID not found")
8182
update_entity_fields(exact_device, **kwargs)
8283

8384
exact_device.save()
@@ -102,7 +103,7 @@ def mutate(self, info, device_id, **kwargs):
102103
DevicesModel.id == device_id
103104
).first()
104105
if not exact_device:
105-
raise GraphQLError("Device not found")
106+
return_error.report_errors_bugsnag_and_graphQL("Device not found")
106107
update_entity_fields(exact_device, state="archived", **kwargs)
107108
exact_device.save()
108109
return DeleteDevice(device=exact_device)
@@ -160,7 +161,7 @@ def resolve_specific_device(self, info, device_id):
160161
device = query.filter(DevicesModel.id == device_id).first()
161162

162163
if not device:
163-
raise GraphQLError("Device not found")
164+
return_error.report_errors_bugsnag_and_graphQL("Device not found")
164165

165166
return device
166167

@@ -169,7 +170,8 @@ def resolve_device_by_name(self, info, device_name):
169170
devices = Devices.get_query(info)
170171
device_name = ''.join(device_name.split()).lower()
171172
if not device_name:
172-
raise GraphQLError("Please provide the device name")
173+
return_error.report_errors_bugsnag_and_graphQL(
174+
"Please provide the device name")
173175
found_devices = []
174176
for device in devices:
175177
exact_name = ''.join(device.name.split()).lower()

api/events/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
from sqlalchemy import (Column, String, Integer, Boolean, ForeignKey, Enum)
22
from sqlalchemy.orm import relationship
33
from sqlalchemy.schema import Sequence
4-
from graphql import GraphQLError
54

65
from helpers.database import Base
76
from utilities.utility import Utility, StateType
87
from helpers.events_filter.events_filter import (
98
validate_date_input,
109
format_range_dates,
1110
)
11+
from api.bugsnag_error import return_error
1212

1313

1414
class Events(Base, Utility):
@@ -38,7 +38,7 @@ def filter_event(start_date, end_date, room_id=None):
3838
or returns all events otherwise.
3939
"""
4040
def error_message(error):
41-
raise GraphQLError(error)
41+
return_error.report_errors_bugsnag_and_graphQL(error)
4242

4343
validate_date_input(start_date, end_date)
4444

api/events/schema.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import graphene
22
from graphene_sqlalchemy import SQLAlchemyObjectType
3-
from graphql import GraphQLError
43
import pytz
54
from dateutil import parser
65
from datetime import timedelta
@@ -29,6 +28,7 @@
2928
calendar_dates_format,
3029
empty_string_checker
3130
)
31+
from api.bugsnag_error import return_error
3232

3333
utc = pytz.utc
3434

@@ -175,7 +175,7 @@ def mutate(self, info, **kwargs):
175175
device_last_seen = parser.parse(
176176
kwargs['start_time']) + timedelta(minutes=10)
177177
except ValueError:
178-
raise GraphQLError("Invalid start time")
178+
return_error.report_errors_bugsnag_and_graphQL("Invalid start time")
179179
update_device_last_activity(
180180
info, room_id, device_last_seen, 'cancel meeting')
181181
if not event:
@@ -200,7 +200,8 @@ def mutate(self, info, **kwargs):
200200
room_id,
201201
event_reject_reason
202202
):
203-
raise GraphQLError("Event cancelled but email not sent")
203+
return_error.report_errors_bugsnag_and_graphQL(
204+
"Event cancelled but email not sent")
204205
return CancelEvent(event=event)
205206

206207

@@ -281,7 +282,8 @@ def check_event_in_db(instance, info, event_check, **kwargs):
281282
return room_id, event
282283
elif event and event_check == 'ended':
283284
if event.meeting_end_time:
284-
raise GraphQLError("Event has already ended")
285+
return_error.report_errors_bugsnag_and_graphQL(
286+
"Event has already ended")
285287
event.meeting_end_time = kwargs['meeting_end_time']
286288
event.save()
287289
return room_id, event
@@ -389,7 +391,7 @@ def resolve_all_events(self, info, **kwargs):
389391
per_page = kwargs.get('per_page')
390392
page, per_page = validate_page_and_per_page(page, per_page)
391393
response = filter_event(
392-
start_date, end_date
394+
start_date, end_date
393395
)
394396
sort_events_by_date(response)
395397

@@ -422,7 +424,8 @@ def resolve_all_events_by_room(self, info, **kwargs):
422424
calendar_id=calendar_id
423425
).first()
424426
if not room:
425-
raise GraphQLError("No rooms with the given CalendarId")
427+
return_error.report_errors_bugsnag_and_graphQL(
428+
"No rooms with the given CalendarId")
426429
response = filter_event(
427430
start_date, end_date, room.id
428431
)

api/location/schema.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import graphene
22
from sqlalchemy import func
3-
from graphql import GraphQLError
43
from graphene_sqlalchemy import SQLAlchemyObjectType
54

65
from api.location.models import Location as LocationModel
@@ -20,6 +19,7 @@
2019
from helpers.room_filter.room_filter import room_join_location
2120
from helpers.auth.authentication import Auth
2221
from helpers.auth.admin_roles import admin_roles
22+
from api.bugsnag_error import return_error
2323

2424

2525
class Location(SQLAlchemyObjectType):
@@ -60,15 +60,15 @@ def mutate(self, info, **kwargs):
6060
template = 'location_success.html'
6161
payload = {
6262
'model': LocationModel, 'field': 'name', 'value': kwargs['name']
63-
}
63+
}
6464
with SaveContextManager(location, 'Location', payload):
6565
if not notification.send_email_notification(
6666
email=email, subject=subject, template=template,
6767
location_name=location.name, user_name=admin_name
6868
):
69-
raise GraphQLError(
69+
return_error.report_errors_bugsnag_and_graphQL(
7070
"Location created but email not sent"
71-
)
71+
)
7272
return CreateLocation(location=location)
7373

7474

@@ -93,7 +93,7 @@ def mutate(self, info, location_id, **kwargs):
9393
location_object = result.filter(
9494
LocationModel.id == location_id).first()
9595
if not location_object:
96-
raise GraphQLError("Location not found")
96+
return_error.report_errors_bugsnag_and_graphQL("Location not found")
9797
admin_roles.verify_admin_location(location_id)
9898
if "time_zone" in kwargs:
9999
validate_timezone_field(**kwargs)
@@ -130,7 +130,7 @@ def mutate(self, info, location_id, **kwargs):
130130
location = result.filter(
131131
LocationModel.id == location_id).first() # noqa: E501
132132
if not location:
133-
raise GraphQLError("location not found")
133+
return_error.report_errors_bugsnag_and_graphQL("location not found")
134134
admin_roles.verify_admin_location(location_id)
135135
update_entity_fields(location, state="archived", **kwargs)
136136
location.save()

api/office/schema.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import graphene
22
from graphene_sqlalchemy import SQLAlchemyObjectType
3-
from graphql import GraphQLError
43
from sqlalchemy import exc, func
54

65
from api.office.models import Office as OfficeModel
@@ -16,6 +15,7 @@
1615
from helpers.pagination.paginate import Paginate, validate_page
1716
from helpers.email.email import notification
1817
from helpers.auth.user_details import get_user_from_db
18+
from api.bugsnag_error import return_error
1919

2020

2121
class Office(SQLAlchemyObjectType):
@@ -41,7 +41,7 @@ class Arguments:
4141
def mutate(self, info, **kwargs):
4242
location = Location.query.filter_by(id=kwargs['location_id']).first()
4343
if not location:
44-
raise GraphQLError("Location not found")
44+
return_error.report_errors_bugsnag_and_graphQL("Location not found")
4545
admin_roles.verify_admin_location(location_id=kwargs['location_id'])
4646
office = OfficeModel(**kwargs)
4747
admin = get_user_from_db()
@@ -50,15 +50,16 @@ def mutate(self, info, **kwargs):
5050
admin_name = username.split(".")[0]
5151
payload = {
5252
'model': OfficeModel, 'field': 'name', 'value': kwargs['name']
53-
}
53+
}
5454
with SaveContextManager(
55-
office, 'Office', payload
55+
office, 'Office', payload
5656
):
5757
new_office = kwargs['name']
5858
if not notification.send_email_notification(
5959
email, new_office, location.name, admin_name
6060
):
61-
raise GraphQLError("Office created but Emails not Sent")
61+
return_error.report_errors_bugsnag_and_graphQL(
62+
"Office created but Emails not Sent")
6263
return CreateOffice(office=office)
6364

6465

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

8485
admin_roles.create_rooms_update_delete_office(office_id)
8586
update_entity_fields(exact_office, state="archived", **kwargs)
@@ -104,13 +105,13 @@ def mutate(self, info, office_id, **kwargs):
104105
result = get_office.filter(OfficeModel.state == "active")
105106
exact_office = result.filter(OfficeModel.id == office_id).first()
106107
if not exact_office:
107-
raise GraphQLError("Office not found")
108+
return_error.report_errors_bugsnag_and_graphQL("Office not found")
108109
admin_roles.create_rooms_update_delete_office(office_id)
109110
try:
110111
update_entity_fields(exact_office, **kwargs)
111112
exact_office.save()
112113
except exc.SQLAlchemyError:
113-
raise GraphQLError("Action Failed")
114+
return_error.report_errors_bugsnag_and_graphQL("Action Failed")
114115

115116
return UpdateOffice(office=exact_office)
116117

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

140141

@@ -166,7 +167,7 @@ def resolve_get_office_by_name(self, info, name):
166167
active_offices = query.filter(OfficeModel.state == "active")
167168
check_office = active_offices.filter(OfficeModel.name == name).first()
168169
if not check_office:
169-
raise GraphQLError("Office Not found")
170+
return_error.report_errors_bugsnag_and_graphQL("Office Not found")
170171
if name == "Epic tower":
171172
exact_query = lagos_office_join_location(active_offices)
172173
result = exact_query.filter(OfficeModel.name == name)

api/office_structure/schema.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import graphene
22
from graphene_sqlalchemy import SQLAlchemyObjectType
3-
from graphql import GraphQLError
43
from enum import Enum
54
from sqlalchemy import asc, desc
65
from api.office_structure.models import OfficeStructure as StructureModel
@@ -12,6 +11,7 @@
1211
from helpers.auth.admin_roles import admin_roles
1312
from helpers.database import db_session
1413
from utilities.utility import update_entity_fields
14+
from api.bugsnag_error import return_error
1515

1616

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

4545
if not node:
46-
raise GraphQLError("node not found")
46+
return_error.report_errors_bugsnag_and_graphQL("node not found")
4747

4848
validate_empty_fields(**kwargs)
4949

@@ -78,7 +78,7 @@ def mutate(self, info, node_id):
7878
exact_node = db_session.query(StructureModel).filter(
7979
StructureModel.id == node_id).first()
8080
if not exact_node:
81-
raise GraphQLError(
81+
return_error.report_errors_bugsnag_and_graphQL(
8282
"The specified node does not exist"
8383
)
8484
db_session.delete(exact_node)
@@ -111,7 +111,7 @@ def mutate(self, info, node_list):
111111
node['name'] = node.name.strip()
112112
node['tag'] = node.tag.strip()
113113
nodes.append(StructureModel(
114-
**node, location_id=admin_location_id))
114+
**node, location_id=admin_location_id))
115115
db_session.add_all(nodes)
116116
db_session.commit()
117117
return CreateStructure(structure=nodes)
@@ -146,7 +146,7 @@ class Query(graphene.ObjectType):
146146
node_name=graphene.String(required=True),
147147
order=graphene.Argument(graphene.Enum.from_enum(Order)),
148148
description="Returns the path from root to the node within structure"
149-
)
149+
)
150150

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

0 commit comments

Comments
 (0)