mirror of
https://github.com/stack-auth/stack.git
synced 2026-06-13 21:01:21 +08:00
improve error handling
This commit is contained in:
parent
48debe36d0
commit
517b4ddb0a
@ -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
|
||||
|
||||
@ -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}`;
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user