Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

How can I show custom error messages in media upload collections?

default discord avatar
oysteinwangenlast year
5

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

  • default discord avatar
    teneburulast year

    I need this as well, have you found a way ?

  • default discord avatar
    oysteinwangenlast year

    Not yet found a way...

  • default discord avatar
    teneburulast year

    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',
        },
      },


    @614188569338314752
  • default discord avatar
    oysteinwangenlast year

    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, }) } } }
  • default discord avatar
    teneburulast year

    very good i'll use the same for my errors thanks !

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.