Hi! I'm working on my storage adapter for ImageKit, and it all works nicely.
However, I'm doing checks for file formats and file sizes within the handleUpload function, and would like to display custom error messages depending on what error gets triggered. Now it's just displaying a "Something went wrong" toast notification, no matter what the error is...
I need this as well, have you found a way ?
Not yet found a way...
I'm in the process of figuring out we can cooperate if you want
I'm rewriting the handleUpload function with logger
console.logs do not get triggered inside, haven't found a way to have logger yet
hey brother i managed to make console.log work inside handleUpload
function,
set this in logger in buildConfig
const destination: DestinationStream = {
write: (message) => {
console.log(message)
},
}
[...buildConfig]
logger: {
destination: destination,
options: {
level: 'info',
},
},Thats nice!
I managed to get some somewhat customizable error messages in admin by throwing ValidationError and APIError:
import type { HandleUpload } from '@payloadcms/plugin-cloud-storage/types'
import { APIError, ValidationError } from 'payload'
import ImageKit from 'imagekit'
type ImageKitArgs = {
getStorageClient: () => ImageKit
prefix?: string
folder: string
}
const maxImageSize = 1024 * 1024 * 1 // 250MB
export const getImageKitUpload = ({ folder, getStorageClient }: ImageKitArgs): HandleUpload => {
return async ({ data, file, req }) => {
// Check if the file size exceeds the limit
if (data.filesize > maxImageSize) {
console.log('File size exceeds the 5MB limit.')
throw new ValidationError({
errors: [
{
label:Upload (File size exceeds the 5MB limit.)
,
message: 'Dont know where this would show up',
path: '???',
},
],
})
}
try {
const res = await getStorageClient().upload({
file: file.buffer,
fileName: file.filename,
isPublished: true,
folder: folder,
})
data.fileId = res.fileId
data.filePath = res.filePath
data.url = res.url
data.thumbnailUrl = res.thumbnailUrl
return data
} catch (error) {
console.log(error)
throw new APIError(Error uploading file: ${JSON.stringify(error)}
, 400, {
error: error,
})
}
}
}very good i'll use the same for my errors thanks !
Star
Discord
online
Get dedicated engineering support directly from the Payload team.