Skip to content

Commit

Permalink
expose plugin errors to compare them with instanceof
Browse files Browse the repository at this point in the history
  • Loading branch information
StarpTech committed Sep 8, 2017
1 parent f870d88 commit 987536f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 42 deletions.
45 changes: 3 additions & 42 deletions examples/basic/multiple-payload-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,19 @@ const nats = require('nats').connect({
preserveBuffers: true
})
const HemeraJoi = require('./../../packages/hemera-joi')
const HemeraParambulator = require('./../../packages/hemera-parambulator')

const hemera = new Hemera(nats, {
logLevel: 'debug'
})

hemera.use(HemeraJoi)

function myPlugin (options) {
var hemera = this

hemera.use(HemeraParambulator)
hemera.setOption('payloadValidator', 'hemera-parambulator')

hemera.add({
topic: 'math',
cmd: 'sub',
a: {
type$: 'number'
}
}, (req, cb) => {
cb(null, req.a - req.b)
})
}

hemera.use({
plugin: myPlugin,
attributes: {
name: 'myPlugin',
dependencies: []
},
options: {}
})

hemera.ready(() => {
hemera.setOption('payloadValidator', 'hemera-joi')

let Joi = hemera.exposition['hemera-joi'].joi
let joiErrors = hemera.exposition['hemera-joi'].errors

/**
* Your Implementations
*/
hemera.add({
topic: 'math',
cmd: 'add',
Expand All @@ -58,18 +29,8 @@ hemera.ready(() => {
hemera.act({
topic: 'math',
cmd: 'add',
a: 3,
b: 20
}, function (err, resp) {
this.log.info(err, 'Error') // Error: child "a" fails because ["a" must be a number]
})

hemera.act({
topic: 'math',
cmd: 'sub',
a: 3,
b: 5
a: 'ddd'
}, function (err, resp) {
this.log.info(err, 'Error') // Error: The value "ddd" is not of type 'number' (parent: a).
this.log.info(err instanceof joiErrors.PreValidationError)
})
})
4 changes: 4 additions & 0 deletions packages/hemera-joi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ exports.plugin = Hp(function hemeraJoi () {
const PostValidationError = hemera.createError('PostValidationError')

hemera.expose('joi', Joi)
hemera.expose('errors', {
PreValidationError,
PostValidationError
})

hemera.ext('onServerPreHandler', function (ctx, req, res, next) {
let plugin = ctx._actMeta.plugin
Expand Down
4 changes: 4 additions & 0 deletions packages/hemera-jwt-auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ exports.plugin = Hp(function hemeraJwtAuth (options) {

const JwtError = hemera.createError('JwtError')

hemera.expose('errors', {
JwtError
})

hemera.ext('onServerPreHandler', function (ctx, req, res, next) {
// Get auth from server method
const auth = ctx._actMeta.schema.auth$
Expand Down
6 changes: 6 additions & 0 deletions packages/hemera-nats-streaming/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ exports.plugin = Hp(function hemeraNatsStreaming (options, next) {
const stan = Nats.connect(options.clusterId, options.clientId, options.opts)
const subList = {}

hemera.expose('errors', {
DuplicateSubscriberError,
ParsingError,
NotAvailableError
})

hemera.ext('onClose', (ctx, next) => {
hemera.log.debug('Stan closing ...')
stan.close()
Expand Down
4 changes: 4 additions & 0 deletions packages/hemera-parambulator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ exports.plugin = Hp(function hemeraParambulator () {
const hemera = this
const PreValidationError = hemera.createError('PreValidationError')

hemera.expose('errors', {
PreValidationError
})

hemera.ext('onServerPreHandler', function (ctx, req, res, next) {
let plugin = ctx._actMeta.plugin
let schema = ctx._actMeta.schema
Expand Down

0 comments on commit 987536f

Please sign in to comment.