Skip to content

Commit b9eb296

Browse files
committed
improve file transfer error messaging
1 parent 86a10fd commit b9eb296

1 file changed

Lines changed: 27 additions & 5 deletions

File tree

apps/sim/lib/api/contracts/storage-transfer.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ import {
1818

1919
const jsonResponseSchema = z.unknown()
2020

21+
function formatFileSize(bytes: number): string {
22+
if (!Number.isFinite(bytes) || bytes <= 0) return '0 Bytes'
23+
const k = 1024
24+
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']
25+
const i = Math.min(Math.floor(Math.log(bytes) / Math.log(k)), sizes.length - 1)
26+
const value = bytes / k ** i
27+
return `${value.toFixed(value >= 100 || i === 0 ? 0 : 1)} ${sizes[i]}`
28+
}
29+
2130
const multipartPartUrlSchema = z.object({
2231
partNumber: z.number(),
2332
url: z.string(),
@@ -334,7 +343,7 @@ export const presignedUrlBodySchema = z
334343
if (val > MAX_FILE_SIZE) {
335344
ctx.addIssue({
336345
code: 'custom',
337-
message: `File size ${val} exceeds maximum allowed size (${MAX_FILE_SIZE} bytes)`,
346+
message: `File size ${formatFileSize(val)} exceeds maximum allowed size of ${formatFileSize(MAX_FILE_SIZE)}`,
338347
})
339348
}
340349
}),
@@ -355,12 +364,25 @@ export const batchPresignedUrlBodySchema = z
355364
contentType: z.string().refine((value) => value.trim().length > 0, {
356365
message: 'contentType is required for all files',
357366
}),
358-
fileSize: z
359-
.number()
360-
.positive('fileSize must be positive for all files')
361-
.max(MAX_FILE_SIZE, `File exceeds maximum size of ${MAX_FILE_SIZE} bytes`),
367+
fileSize: z.number(),
362368
})
363369
.passthrough()
370+
.superRefine((file, ctx) => {
371+
const name = typeof file.fileName === 'string' ? file.fileName : 'file'
372+
if (!Number.isFinite(file.fileSize) || file.fileSize <= 0) {
373+
ctx.addIssue({
374+
code: 'custom',
375+
path: ['fileSize'],
376+
message: `${name} is empty (fileSize must be greater than 0)`,
377+
})
378+
} else if (file.fileSize > MAX_FILE_SIZE) {
379+
ctx.addIssue({
380+
code: 'custom',
381+
path: ['fileSize'],
382+
message: `${name} (${formatFileSize(file.fileSize)}) exceeds maximum allowed size of ${formatFileSize(MAX_FILE_SIZE)}`,
383+
})
384+
}
385+
})
364386
)
365387
.min(1, 'files array is required and cannot be empty')
366388
.max(100, 'Cannot process more than 100 files at once'),

0 commit comments

Comments
 (0)