Skip to content

failAction: detailedError to log, defaultError to response #4040

Closed

Description

@mahnunchik

Support plan

  • which support plan is this issue covered by? (e.g. Community, Core, Plus, or Enterprise): Community, Core
  • is this issue currently blocking your project? (yes/no): yes
  • is this issue affecting a production system? (yes/no): no

Context

  • node version: v12.14.1
  • module version: 19.0.5
  • environment (e.g. node, browser, native): node
  • used with (e.g. hapi application, another framework, standalone, ...): hapi app
  • any other relevant information: -

What problem are you trying to solve?

It wold be awesome to have failAction to log detailedError to the console, defaultError to the response.

Current implementation allows:

failAction: 'error'
// defaultError to log and to response

failAction: (request, h, err) => { request.log('error', err); throw err }
// detailedError to log and to response

There is no way to access both detailedError and defaultError.

Do you have a new or modified API suggestion to solve the problem?

https://github.com/hapijs/hapi/blob/master/lib/toolkit.js#L117-L135

if (failAction === 'log-and-error') {
    request._log(options.tags, options.details || err);
    throw err;
}

Activity

  1. hueniverse commented on Mar 13, 2020

    @hueniverse
    Contributor

    You can just create your own default error. It is just a simple Boom.badRequest()...

  2. self-assigned this
    on Mar 13, 2020
  3. mahnunchik commented on Mar 14, 2020

    @mahnunchik
    Author

    @hueniverse Hi, you don't understand the problem. It is impossible to create custom error for each 4xx и 5xx errors in hapi core and plugins...

  4. hueniverse commented on Mar 14, 2020

    @hueniverse
    Contributor

    I have no idea what the problem is.

  5. mahnunchik commented on Mar 16, 2020

    @mahnunchik
    Author

    @hueniverse There are no simple way to do following:

    • send defaultError to the response — this kind of error hides details from the client.
    • send detailedError to the console — with details to better understand what is happening.

    There are options to have defaultError OR detailedError in both places.

  6. hueniverse commented on Mar 16, 2020

    @hueniverse
    Contributor

    Yes there is. You just don't need defaultError. You can create one yourself! It is just a simple Boom.badRequest().

  7. mahnunchik commented on Mar 16, 2020

    @mahnunchik
    Author

    Yep, I may do the following:

    const server = Hapi.server({
      routes: {
        validate: {
          failAction: (request, h, err) => {
            // log to console detailedError
            request.log('validation', err);
            // send to client just 400
            throw Boom.badRequest();
          },
        },
      },
    });

    But it is look like a bit hacky. It wold be great to have this behaviour in core: log details and send to the client without details.

  8. AndriiNyzhnyk commented on Mar 16, 2020

    @AndriiNyzhnyk

    @mahnunchik why do you use throw Boom.badRequest()? You should use return Boom.badRequest('Some Message'). And why you said that this is a bit hacky? The Boom is Hapi.js core plugin, just splited for best maintenance.

  9. mahnunchik commented on Mar 17, 2020

    @mahnunchik
    Author

    Hi @AndriiNyzhnyk

    That is the difference of throw Boom.badRequest() and return Boom.badRequest() in case of failAction handler?

    Boom is Hapi.js core plugin, just splited for best maintenance.

    Yep! But we already have:

    We will loose this message if we use new Boom.badRequest error in the failAction handler.

    It is impossible to access defaultError in the failAction handler. Look at this two lines: https://github.com/hapijs/hapi/blob/master/lib/validation.js#L148 and https://github.com/hapijs/hapi/blob/master/lib/toolkit.js#L133 only details exposed to failAction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

featureNew functionality or improvement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions