Skip to content
This repository has been archived by the owner on Feb 15, 2024. It is now read-only.

Commit

Permalink
Merge 465a684 into ac9d968
Browse files Browse the repository at this point in the history
  • Loading branch information
kinson committed Jun 11, 2018
2 parents ac9d968 + 465a684 commit 936858c
Show file tree
Hide file tree
Showing 9 changed files with 287 additions and 584 deletions.
1 change: 0 additions & 1 deletion .travis.yml
@@ -1,7 +1,6 @@
script: npm run test-ci
language: node_js
node_js:
- "7"
- "8"
- "9"
- "10"
2 changes: 1 addition & 1 deletion API.md
Expand Up @@ -45,7 +45,7 @@ const failAction = Relish.options({
}).failAction
```

## `failAction(request, reply, source, error)`
## `failAction(request, h, err)`
A helper function that can be used as a custom failAction handler in your Hapi.js [Route Options][hapi-route-options].

<!-- URLs -->
Expand Down
8 changes: 4 additions & 4 deletions README.md
Expand Up @@ -92,16 +92,16 @@ server.route({
}
}
},
handler: (request, reply) => reply()
handler: (request, h) => h.response()
});
```

#### Global Usage (alternative)
You can apply this module to all routes by setting the failAction in your server connection options.
You can apply this module to all routes by setting the failAction in your server options.

```js
server.connection({
// ... other connection options
Hapi.server({
// ... other server options

routes: {
validate: {
Expand Down
15 changes: 9 additions & 6 deletions example/route.js
Expand Up @@ -4,8 +4,8 @@ const Hapi = require('hapi')
const Joi = require('joi')
const Relish = require('../')()

const server = new Hapi.Server()
server.connection({
const server = new Hapi.Server({
host: '0.0.0.0',
port: 3000
})

Expand Down Expand Up @@ -38,9 +38,12 @@ server.route({
}
}
},
handler: (request, reply) => reply()
handler: (request, h) => h.response()
})

server.start(() => {
console.log('Server running at:', server.info.uri)
})
const init = async () => {
await server.start()
console.log(`Server is running at ${server.info.uri}`)
}

init()
15 changes: 9 additions & 6 deletions example/server.js
Expand Up @@ -12,8 +12,8 @@ const Relish = require('../')({
}
})

const server = new Hapi.Server()
server.connection({
const server = Hapi.Server({
host: '0.0.0.0',
port: 3000,
routes: {
validate: {
Expand Down Expand Up @@ -42,9 +42,12 @@ server.route({
}
}
},
handler: (request, reply) => reply()
handler: (request, h) => h.response()
})

server.start(() => {
console.log('Server running at:', server.info.uri)
})
const init = async () => {
await server.start()
console.log(`Server is running at ${server.info.uri}`)
}

init()
13 changes: 8 additions & 5 deletions index.js
Expand Up @@ -14,7 +14,7 @@ const Relish = function Relish (opts) {
this._opts = opts ? Hoek.applyToDefaults(internals.defaults, opts) : internals.defaults

this.parseError = (error) => {
return error.data.details.map((i) => {
return error.details.map((i) => {
let err = {
key: i.context.key,
path: i.path.join('.'),
Expand Down Expand Up @@ -56,17 +56,20 @@ const Relish = function Relish (opts) {
return this.exports
}

this.exports.failAction = (request, reply, source, error) => {
this.exports.failAction = (request, h, err) => {
// parse error object
const errors = this.parseError(error)
const errors = this.parseError(err)

// build main error message
const errorMessage = errors.map((e) => e.message).join(', ')

// retrieve validation failure source
const source = err.output.payload.validation.source

// format error response
error = this.formatResponse(error, source, errorMessage, errors)
err = this.formatResponse(err, source, errorMessage, errors)

return reply(error)
return err
}

return this.exports
Expand Down

0 comments on commit 936858c

Please sign in to comment.