Skip to content

Commit

Permalink
Handle database error in event handler
Browse files Browse the repository at this point in the history
  • Loading branch information
andchiind committed Jul 24, 2024
1 parent cc1759a commit 3b2af29
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions backend/api/EventHandlers/MissionEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ private async void OnLocalizationMissionSuccessful(object? sender, LocalizationM
_startMissionSemaphore.WaitOne();
try { await MissionScheduling.StartNextMissionRunIfSystemIsAvailable(robot.Id); }
catch (MissionRunNotFoundException) { return; }
catch (DatabaseUpdateException) { return; }
finally { _startMissionSemaphore.Release(); }
}

Expand Down
5 changes: 4 additions & 1 deletion backend/api/EventHandlers/MqttEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,10 @@ private async void OnIsarTaskUpdate(object? sender, MqttReceivedArgs mqttArgs)
catch (MissionTaskNotFoundException) { return; }

var missionRun = await MissionRunService.ReadByIsarMissionId(task.MissionId);
if (missionRun is null) _logger.LogWarning("Mission run with ID {Id} was not found", task.MissionId);
if (missionRun is null)
{
_logger.LogWarning("Mission run with ID {Id} was not found", task.MissionId);
}

_ = SignalRService.SendMessageAsync("Mission run updated", missionRun?.Area?.Installation, missionRun != null ? new MissionRunResponse(missionRun) : null);

Expand Down
10 changes: 9 additions & 1 deletion backend/api/Services/MissionRunService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,15 @@ public async Task<MissionRun> Create(MissionRun missionRun, bool triggerCreatedM
{
missionRun.Id ??= Guid.NewGuid().ToString(); // Useful for signalR messages
// Making sure database does not try to create new robot
context.Entry(missionRun.Robot).State = EntityState.Unchanged;
try
{
context.Entry(missionRun.Robot).State = EntityState.Unchanged;
}
catch (InvalidOperationException e)
{
throw new DatabaseUpdateException($"Unable to create mission. {e}");
}


if (IncludesUnsupportedInspectionType(missionRun))
{
Expand Down
4 changes: 4 additions & 0 deletions backend/api/Utilities/Exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,8 @@ public class RobotCurrentAreaMissingException(string message) : Exception(messag
public class UnsupportedRobotCapabilityException(string message) : Exception(message)
{
}

public class DatabaseUpdateException(string message) : Exception(message)
{
}
}

0 comments on commit 3b2af29

Please sign in to comment.