Reputation: 30805
I use default Loopback's REST API. When I want to create a new user I just send a POST
request to a default Loopback's endpoint /api/Users
.
The issue is when an email already exists in a DB, Loopback throws an error ValidationError; status 422; Email already exists
. And I don't know how to catch that error. Because if I use a default Loopback's endpoint, I cannot control User.create(...)
method. It is called somewhere in the background throwing an error. And docs do not explain how to handle the error.
I can get rid of Loopback's default REST API and do all registration steps manually, like that:
User.create(..., function(err, user) {
if(err) {
handle error
}
handle success
})
But I'd like to use its default REST API, it is a framework after all. How can I catch and handle such an error?
Upvotes: 0
Views: 576
Reputation: 30805
I figured it out. They provide afterRemoteError()
hook that runs after the remote method has finished with an error https://docs.strongloop.com/display/public/LB/Remote+hooks
User.afterRemoteError('create', function(ctx, next) {
console.log('> Client.afterRemoteError triggered');
console.error('ctx.error: ', ctx.error);
next(err);
});
Upvotes: 0