From 517b4ddb0a0433078a2d67d5f928558b0df8abe2 Mon Sep 17 00:00:00 2001 From: Zai Shi Date: Wed, 30 Jul 2025 14:32:40 -0700 Subject: [PATCH] improve error handling --- apps/backend/src/lib/images.tsx | 4 ++-- apps/backend/src/s3.tsx | 11 +++++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/apps/backend/src/lib/images.tsx b/apps/backend/src/lib/images.tsx index c63ae05c9..15bf995aa 100644 --- a/apps/backend/src/lib/images.tsx +++ b/apps/backend/src/lib/images.tsx @@ -28,12 +28,12 @@ export async function parseBase64Image(input: string, options: { try { imageBuffer = Buffer.from(base64Data, 'base64'); } catch (error) { - throw new Error('Invalid base64 image data'); + throw new ImageProcessingError('Invalid base64 image data'); } // Check file size if (options.maxBytes && imageBuffer.length > options.maxBytes) { - throw new Error(`Image size (${imageBuffer.length} bytes) exceeds maximum allowed size (${options.maxBytes} bytes)`); + throw new ImageProcessingError(`Image size (${imageBuffer.length} bytes) exceeds maximum allowed size (${options.maxBytes} bytes)`); } // Dynamically import sharp diff --git a/apps/backend/src/s3.tsx b/apps/backend/src/s3.tsx index 561c354bd..168c3c579 100644 --- a/apps/backend/src/s3.tsx +++ b/apps/backend/src/s3.tsx @@ -29,7 +29,7 @@ export function getS3PublicUrl(key: string): string { return `${S3_ENDPOINT}/${S3_BUCKET}/${key}`; } -export async function uploadBase64Image({ +async function uploadBase64Image({ input, maxBytes = 1024 * 300, folderName, @@ -42,7 +42,14 @@ export async function uploadBase64Image({ throw new StackAssertionError("S3 is not configured"); } - const { buffer, metadata } = await parseBase64Image(input, { maxBytes }); + try { + const { buffer, metadata } = await parseBase64Image(input, { maxBytes }); + } catch (error) { + if (error instanceof ImageProcessingError) { + throw new StatusError(StatusError.BadRequest, error.message); + } + throw error; + } const key = `${folderName}/${crypto.randomUUID()}.${metadata.format}`;