From ce6d7161bbfa5d0410e632afa5f4aa179dbb9491 Mon Sep 17 00:00:00 2001 From: Harsh Patel Date: Thu, 23 May 2024 22:45:25 +0530 Subject: [PATCH] chore(api): add missing error handling --- apps/api/src/provider/minio.provider.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/apps/api/src/provider/minio.provider.ts b/apps/api/src/provider/minio.provider.ts index 8f05d895..a2355c2e 100644 --- a/apps/api/src/provider/minio.provider.ts +++ b/apps/api/src/provider/minio.provider.ts @@ -45,8 +45,16 @@ export const MinioProvider: Provider = { async function createBucketIfNotExists() { if (!minioClient) return + let bucketExists: boolean = false - const bucketExists = await minioClient.bucketExists(bucketName) + try { + bucketExists = await minioClient.bucketExists(bucketName) + } catch (error) { + logger.error('Error while checking if bucket exists in Minio', error) + throw new InternalServerErrorException( + 'Error while checking if bucket exists in Minio' + ) + } if (!bucketExists) { logger.warn(`Bucket ${bucketName} does not exist. Creating it.`) try { @@ -66,7 +74,9 @@ export const MinioProvider: Provider = { if (!isServiceLoaded) { return new InternalServerErrorException('Minio Client has not loaded') } + const fileName = `${Date.now()}-${file.originalname}` + try { await minioClient.putObject( bucketName, @@ -85,6 +95,7 @@ export const MinioProvider: Provider = { if (!isServiceLoaded) { return new InternalServerErrorException('Minio Client has not loaded') } + try { return await minioClient.presignedUrl('GET', bucketName, fileName) } catch (error) { @@ -99,8 +110,10 @@ export const MinioProvider: Provider = { if (!isServiceLoaded) { return new InternalServerErrorException('Minio Client has not loaded') } + try { await minioClient.removeObject(bucketName, fileName) + return true } catch (error) { logger.error('Error deleting file from Minio', error) throw new InternalServerErrorException('Error deleting file from Minio')