hemerajs / hemera

🔬 Writing reliable & fault-tolerant microservices in Node.js https://hemerajs.github.io/hemera/
MIT License
806 stars 70 forks source link

Not returning error to api #149

Closed vforv closed 7 years ago

vforv commented 7 years ago

On api side where is hemera hapi I have basic act request:

createUser(req: HapiRequest, reply: Hapi.ReplyNoContinue) {
        let user: UserCreateInterface = req.payload;

        return req.hemera.act({ topic: 'user-save', type: 'amerbank', payload: user },
            function (err: any, result: any) {

                reply(err || result)
            })
    }

here is add in service:

this.hemera.add({
      topic: 'user-save',
      type: 'amerbank',
      email: Joi.number().required()
    }

In logs I get error messaga:

[api - 7554]: 170908/201422.097, [error] message: child "email" fails because ["email" is required], stack: PreValidationError: child "email" fails because ["email" is required] [api - 7554]: at Joi.validate (/home/master/Documents/trifecta/user-service/node_modules/hemera-joi/index.js:38:18)

But api returns: Internal server error

StarpTech commented 7 years ago

This is the expected behavior. If you return an error in Hapi the default error code is 500. You can set it with reply(err).code(400) (404 bad request)

createUser(req: HapiRequest, reply: Hapi.ReplyNoContinue) {
        let user: UserCreateInterface = req.payload;

        return req.hemera.act({ topic: 'user-save', type: 'amerbank', payload: user },
            function (err: any, result: any) {
                if (err) {
                   if (err.name === 'PreValidationError') {
                     return reply({ success: false, message: err.message  }).code(404)
                   } else {
                     return reply(err)
                   }
                 }
                reply(result)
            })
    }
StarpTech commented 7 years ago

In the next release it is possible to compare the errors with instanceof this is cleaner

vforv commented 7 years ago

Thanks, I make it works with boom.