Wildcard handler is not handling exceptions


#1

Hello.

The wildcard handlers handles all kinds of exceptions ?

I created using: adonis make:ehandler but it’s not handling errors… nothing in console or error message returns.

my handler:

'use strict'

const BaseExceptionHandler = use('BaseExceptionHandler')

class ExceptionHandler extends BaseExceptionHandler {
  async handle (error, { request, response }) {
    console.log(`wildcard handle method... Error name: ${error.name}`)
    response.status(error.status).send(error.message)
  }

  async report (error, { request }) {
    console.log('Handler report method...')
  }
}

module.exports = ExceptionHandler

I don’t know what i am doing wrong!


#2

Fala Wemerson. Manda um exemplo de função do teu controller, que tá gerando um erro.
Verifica se tu não estás utilizando dentro de um try catch, o que anularia o ehandler.


#3

Hello Alex Henrique. It’s all… Even validators erros, that should pass through Hancler, not works. here an example:

async index ({ request }) {
    const { page, pagesize } = request.desctop
    const users = await User.querry()
      .paginate(page, pagesize)

    return users
  }

Result:

{
  "error": {
    "message": {}
  }
}

The same code (with same querry error), copied and pasted in other server:

{
  "error": {
    "message": "User.querry is not a function",
    "name": "TypeError",
    "status": 500,
    "frames": [
      {
        "file": "homolog/backend/app/Controllers/Http/UserController.js",
        "filePath": "/home/deploy/homolog/backend/app/Controllers/Http/UserController.js",
        "method": "UserController.index",
        "line": 26,
        "column": 30,
        "context": {
          "start": 21,

...etc

#5

@ShadowPaz, I don’t understand what you mean by withdrawn by author!


#6

please forget it, I was answering in a wrong way so that message indicates I DELETE MY answer @wemersonrv


#7

Ah ok. It’s fine!


#8

For validation errors, use the next:

  • Create a validator;
  • Use the validator as below:
    get rules () {
        return {
            prop: 'required'
        }
    }

    get messages () {
        return {
            'nome.required': 'Your custom message'
        }
    }

    async fails (errorMessages) {
        return this.ctx.response
            .status(403)
            .send({ message: 'Fail message', errors: errorMessages })
    }

Do not forget the fails function.


#9

@alexhenriquepv The problem is not with validators… they are fine! I have a lot of validators, including custom validators i’ve made myself and all validators and my handler are working. I’m using this wildcard handler to grab all exceptions, including validation error messages… and it working ind production with no problem.

Now in my new computer, Linux Mint 19 (derived from Ubuntu 18.04) i have this issue about something is interfering on wildcard exception handling. Even as you can see from the previous message, I copied the same code (with error) to a remote server and it worked ok.

So i don’t understand what’s happening!


#10

The name of file by default is '/app/Exceptions/Handler.js'. Are you using this?


#11

Yes… app/Exceptions/Handler.js.

Is the default wildcard handler created by adonis make:ehandler… it works, but not on my computer. And because this, i don’t understand the issue!

  1. adonis make:ehandler
  2. code app/Exceptions/Handler.js
  3. force an exception by causing an error with invalid code, or foce a validation exception
  4. not works in my computer
  5. Copy the same code to other computer/server
  6. Repeat the steps 3 and 4 and works…

WTF?!?! :weary:


#12

Found it!

The problem was in a middleware i made to work with the request… it was returning wrong error messages… i change it to throw an exception, so it goes to default handler.