hapijs / glue

Server composer for hapi.js
Other
245 stars 62 forks source link

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

Closed marianozunino closed 6 years ago

marianozunino commented 6 years ago

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 commented 6 years ago

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();
marianozunino commented 6 years ago

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

image

lock[bot] commented 4 years ago

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.