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}`;