From cc1548068d978f5ada888d7623142f8f91c1c088 Mon Sep 17 00:00:00 2001 From: andchiind Date: Wed, 18 Dec 2024 14:37:35 +0100 Subject: [PATCH] Add exception handling to blob store --- backend/api.test/Mocks/BlobServiceMock.cs | 2 +- backend/api.test/Mocks/MapServiceMock.cs | 2 +- backend/api/Controllers/InspectionController.cs | 5 ++++- backend/api/Controllers/MapController.cs | 14 +++++--------- backend/api/Services/BlobService.cs | 15 ++++++++++++--- backend/api/Services/InspectionService.cs | 4 ++-- backend/api/Services/MapService.cs | 4 ++-- 7 files changed, 27 insertions(+), 19 deletions(-) diff --git a/backend/api.test/Mocks/BlobServiceMock.cs b/backend/api.test/Mocks/BlobServiceMock.cs index 97a2dc7b0..ded5b749c 100644 --- a/backend/api.test/Mocks/BlobServiceMock.cs +++ b/backend/api.test/Mocks/BlobServiceMock.cs @@ -7,7 +7,7 @@ namespace Api.Test.Mocks { public class MockBlobService : IBlobService { - public async Task DownloadBlob(string blobName, string containerName, string accountName) + public async Task DownloadBlob(string blobName, string containerName, string accountName) { using var memoryStream = new System.IO.MemoryStream(); await Task.CompletedTask; diff --git a/backend/api.test/Mocks/MapServiceMock.cs b/backend/api.test/Mocks/MapServiceMock.cs index 946c33bc4..38cc24dd1 100644 --- a/backend/api.test/Mocks/MapServiceMock.cs +++ b/backend/api.test/Mocks/MapServiceMock.cs @@ -21,7 +21,7 @@ public class MockMapService : IMapService return new MapMetadata(); } - public async Task FetchMapImage(string mapName, string installationCode) + public async Task FetchMapImage(string mapName, string installationCode) { await Task.Run(() => Thread.Sleep(1)); string filePath = Directory.GetCurrentDirectory() + "Images/MockMapImage.png"; diff --git a/backend/api/Controllers/InspectionController.cs b/backend/api/Controllers/InspectionController.cs index ce8fa303f..6d2e8974c 100644 --- a/backend/api/Controllers/InspectionController.cs +++ b/backend/api/Controllers/InspectionController.cs @@ -96,7 +96,10 @@ public async Task> GetInspectionImageById([FromRoute] s try { - byte[] inspectionStream = await inspectionService.FetchInpectionImage(inspectionData.BlobName, inspectionData.BlobContainer, inspectionData.StorageAccount); + byte[]? inspectionStream = await inspectionService.FetchInpectionImage(inspectionData.BlobName, inspectionData.BlobContainer, inspectionData.StorageAccount); + + if (inspectionStream == null) return NotFound($"Could not retrieve inspection with task Id {taskId}"); + return File(inspectionStream, "image/png"); } catch (Azure.RequestFailedException) diff --git a/backend/api/Controllers/MapController.cs b/backend/api/Controllers/MapController.cs index aa9b41d35..4d819c6d7 100644 --- a/backend/api/Controllers/MapController.cs +++ b/backend/api/Controllers/MapController.cs @@ -24,15 +24,11 @@ public class MapController(IMapService mapService) : ControllerBase [ProducesResponseType(StatusCodes.Status502BadGateway)] public async Task> GetMap([FromRoute] string installationCode, string mapName) { - try - { - byte[] mapStream = await mapService.FetchMapImage(mapName, installationCode); - return File(mapStream, "image/png"); - } - catch (RequestFailedException) - { - return NotFound("Could not find map for this area"); - } + byte[]? mapStream = await mapService.FetchMapImage(mapName, installationCode); + + if (mapStream == null) return NotFound($"Could not retrieve map '{mapName}' in installation {installationCode}"); + + return File(mapStream, "image/png"); } } } diff --git a/backend/api/Services/BlobService.cs b/backend/api/Services/BlobService.cs index d0591b5db..5c7cdf6a0 100644 --- a/backend/api/Services/BlobService.cs +++ b/backend/api/Services/BlobService.cs @@ -11,7 +11,7 @@ namespace Api.Services { public interface IBlobService { - public Task DownloadBlob(string blobName, string containerName, string accountName); + public Task DownloadBlob(string blobName, string containerName, string accountName); public AsyncPageable FetchAllBlobs(string containerName, string accountName); @@ -20,13 +20,22 @@ public interface IBlobService public class BlobService(ILogger logger, IOptions azureOptions) : IBlobService { - public async Task DownloadBlob(string blobName, string containerName, string accountName) + public async Task DownloadBlob(string blobName, string containerName, string accountName) { var blobContainerClient = GetBlobContainerClient(containerName, accountName); var blobClient = blobContainerClient.GetBlobClient(blobName); using var memoryStream = new MemoryStream(); - await blobClient.DownloadToAsync(memoryStream); + try + { + await blobClient.DownloadToAsync(memoryStream); + } + catch (RequestFailedException) + { + logger.LogWarning("Failed to download blob {blobName} from container {containerName}", blobName, containerName); + return null; + } + return memoryStream.ToArray(); } diff --git a/backend/api/Services/InspectionService.cs b/backend/api/Services/InspectionService.cs index 36e8f7c89..243ff7acd 100644 --- a/backend/api/Services/InspectionService.cs +++ b/backend/api/Services/InspectionService.cs @@ -13,7 +13,7 @@ namespace Api.Services { public interface IInspectionService { - public Task FetchInpectionImage(string inpectionName, string installationCode, string storageAccount); + public Task FetchInpectionImage(string inpectionName, string installationCode, string storageAccount); public Task UpdateInspectionStatus(string isarTaskId, IsarTaskStatus isarTaskStatus); public Task ReadByIsarTaskId(string id, bool readOnly = true); public Task AddFinding(InspectionFindingQuery inspectionFindingsQuery, string isarTaskId); @@ -31,7 +31,7 @@ public class InspectionService(FlotillaDbContext context, ILogger FetchInpectionImage(string inpectionName, string installationCode, string storageAccount) + public async Task FetchInpectionImage(string inpectionName, string installationCode, string storageAccount) { return await blobService.DownloadBlob(inpectionName, installationCode, storageAccount); } diff --git a/backend/api/Services/MapService.cs b/backend/api/Services/MapService.cs index eab825049..9cbe7e1e6 100644 --- a/backend/api/Services/MapService.cs +++ b/backend/api/Services/MapService.cs @@ -8,7 +8,7 @@ namespace Api.Services { public interface IMapService { - public Task FetchMapImage(string mapName, string installationCode); + public Task FetchMapImage(string mapName, string installationCode); public Task ChooseMapFromPositions(IList positions, string installationCode); public Task ChooseMapFromMissionRunTasks(MissionRun mission); } @@ -17,7 +17,7 @@ public class MapService(ILogger logger, IOptions blobOptions, IBlobService blobService) : IMapService { - public async Task FetchMapImage(string mapName, string installationCode) + public async Task FetchMapImage(string mapName, string installationCode) { return await blobService.DownloadBlob(mapName, installationCode, blobOptions.Value.StorageAccount); }