improve error handling

This commit is contained in:
Zai Shi 2025-07-30 14:32:40 -07:00
parent 48debe36d0
commit 517b4ddb0a
2 changed files with 11 additions and 4 deletions

View File

@ -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

View File

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