Skip to content

Exceptions

CodeDescriptionOrigin
E_MISSING_PACKAGEMissing package
E_CANNOT_CREATE_ATTACHMENTUnable to create Attachment Object
E_CANNOT_CREATE_VARIANTUnable to create variant
E_CANNOT_PATH_BY_CONVERTERMissing path by converter
E_ISNOT_BUFFERIs not a Buffer
E_ISNOT_BASE64Is not a Base64
ENOENTUnable to read file
E_CANNOT_WRITE_FILEUnable to write file to the destinationDrive
E_CANNOT_READ_FILEUnable to read fileDrive
E_CANNOT_DELETE_FILEUnable to delete fileDrive
E_CANNOT_SET_VISIBILITYUnable to set file visibilityDrive
E_CANNOT_GENERATE_URLUnable to generate URL for a fileDrive
E_UNALLOWED_CHARACTERSThe file key has unallowed set of charactersDrive
E_INVALID_KEYKey post normalization leads to an empty stringDrive

Adonis documentation exception

Handling exceptions

If you want to handle a specific exception differently, you can do that inside the handle method. Make sure to use the ctx.response.send method to send a response, since the return value from the handle method is discarded.

typescript
import { errors } from '@jrmc/adonis-attachment'

export default class HttpExceptionHandler extends ExceptionHandler {
  async handle(error: unknown, ctx: HttpContext) {
    if (error instanceof errors.E_CANNOT_WRITE_FILE) {
      const err = error as errors.E_CANNOT_WRITE_FILE
      ctx.response.status(422).send(err.messages)
      return
    }

    return super.handle(error, ctx)
  }
}
typescript
import { errors } from '@jrmc/adonis-attachment'

export default class HttpExceptionHandler extends ExceptionHandler {
  async handle(error: unknown, ctx: HttpContext) {
    if (error instanceof errors.E_CANNOT_WRITE_FILE) {
      ctx.session.flash('notification', {
        type: 'error',
        message: err.message,
      })

      return ctx.response.redirect('back')
    }

    return super.handle(error, ctx)
  }
}