Skip to content

Commit

Permalink
Use exception handling for updating mission
Browse files Browse the repository at this point in the history
  • Loading branch information
aeshub committed Nov 10, 2023
1 parent 856b5bb commit 00b4554
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
5 changes: 4 additions & 1 deletion backend/api/EventHandlers/MqttEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,10 @@ private async void OnMissionUpdate(object? sender, MqttReceivedArgs mqttArgs)
return;
}

var flotillaMissionRun = await missionRunService.UpdateMissionRunStatusByIsarMissionId(isarMission.MissionId, status);
MissionRun? flotillaMissionRun;
try { flotillaMissionRun = await missionRunService.UpdateMissionRunStatusByIsarMissionId(isarMission.MissionId, status); }
catch (MissionRunNotFoundException) { return; }

if (flotillaMissionRun is null)
{
_logger.LogError("No mission found with ISARMissionId '{IsarMissionId}'. Could not update status to '{Status}'", isarMission.MissionId, status);
Expand Down
15 changes: 4 additions & 11 deletions backend/api/Services/MissionRunService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
using Api.Database.Context;
using Api.Database.Models;
using Api.Services.Events;
using Api.Services.Models;
using Api.Utilities;
using Microsoft.EntityFrameworkCore;
using TaskStatus = Api.Database.Models.TaskStatus;
namespace Api.Services
{
public interface IMissionRunService
Expand Down Expand Up @@ -321,19 +319,14 @@ MissionRunQueryStringParameters parameters
);
}

public async Task<MissionRun?> UpdateMissionRunStatusByIsarMissionId(
string isarMissionId,
MissionStatus missionStatus
)
public async Task<MissionRun?> UpdateMissionRunStatusByIsarMissionId(string isarMissionId, MissionStatus missionStatus)
{
var missionRun = await ReadByIsarMissionId(isarMissionId);
if (missionRun is null)
{
_logger.LogWarning(
"Could not update mission status for ISAR mission with id: {id} as the mission was not found",
isarMissionId
);
return null;
string errorMessage = $"Mission with isar mission Id {isarMissionId} was not found";
_logger.LogError("{Message}", errorMessage);
throw new MissionRunNotFoundException(errorMessage);
}

missionRun.Status = missionStatus;
Expand Down
5 changes: 5 additions & 0 deletions backend/api/Utilities/Exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public class MissionTaskNotFoundException : Exception
public MissionTaskNotFoundException(string message) : base(message) { }
}

public class MissionRunNotFoundException : Exception
{
public MissionRunNotFoundException(string message) : base(message) { }
}

public class RobotPositionNotFoundException : Exception
{
public RobotPositionNotFoundException(string message) : base(message) { }
Expand Down

0 comments on commit 00b4554

Please sign in to comment.