Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How should I use routes in server config (failAction). #113

Closed
marianozunino opened this issue Jan 20, 2018 · 3 comments
Closed

How should I use routes in server config (failAction). #113

marianozunino opened this issue Jan 20, 2018 · 3 comments
Assignees
Labels
support Questions, discussions, and general support

Comments

@marianozunino
Copy link

marianozunino commented Jan 20, 2018

Since in hapi 17, all the joi errors return "Invalid payload" I want to implement this "failAction" in server routes config.

const hapiOptions = {
  host: 'localhost',
  port: 3000,
  routes: {
    validate: {
      failAction: async (request, h, err) => {
        if (process.env.NODE_ENV === 'production') {
          // In prod, log a limited error message and throw the default Bad Request error.
          console.error('ValidationError:', err.message); // Better to use an actual logger here.
          throw Boom.badRequest(`Invalid request payload input`);
        } else {
          // During development, log and respond with the full error.
          console.error(err);
          throw err;
        }
      }
    }
  }
};

const server = Hapi.server(hapiOptions);

How can I implement this failAction with glue?

@WesTyler
Copy link
Contributor

WesTyler commented Jan 21, 2018

You can just pass the routes property with your validate and failAction exactly as they appear in your example into the server object of your Glue manifest:

const manifest = {
    server: {
        host: 'localhost',
        port: 3000,
        routes: {
            validate: {
                failAction: async (request, h, err) => { /*your handler omitted for clarity*/ }
            }
        }
    }
};

const startServer = async function() {
    const server = await Glue.compose(manifest);
    await server.start();
    console.log('Server Started');
};

startServer();

@WesTyler WesTyler self-assigned this Jan 21, 2018
@marianozunino
Copy link
Author

Gotcha. I was trying to keep a JSON file. Now I'm using a Js file, and exporting the object.

image

@Marsup Marsup added support Questions, discussions, and general support and removed question labels Sep 21, 2019
@lock
Copy link

lock bot commented Jan 9, 2020

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
support Questions, discussions, and general support
Projects
None yet
Development

No branches or pull requests

3 participants