Custom Error Handling dont work


#1

Hello everybody. I think that I have a problem with my custom error handling.

I have the following custom error handling:

MyCustom.js

'use strict'

const { LogicalException } = require('@adonisjs/generic-exceptions')

class MyCustom extends LogicalException {
  /**
   * Handle this exception by itself
   */
  async handle (error, { request, response }) {
    response.status(error.status).json({
      message: error.message,
      url: request.url()
    })
  }
}

module.exports = MyCustom

And I have my controller:

ColorController.js

'use strict'

const Color = use('App/Models/Color')
const MyCustom = use('App/Exceptions/MyCustom')

class ColorController {
  
  async show ({ request, response, params: { id } }) {
    const color = await Color.find(id)

    if (!color) { throw new MyCustom ('Color not found', 404) }

    response.status(200).json({
      message: 'Showing color',
      object: 'color',
      url: request.url(),
      data: color
    })

  }
}

module.exports = ColorController

But the code in my error handling(MyCustom) don’t run. Can anyone help me?


#2

Well. Strangely, I solved this by putting the code straightly in a respective controller action. show

Before I had put in another method of my class. Example: _setColor()