Feathers: How to return an error from hook?

Created on 18 Jan 2018  路  7Comments  路  Source: feathersjs/feathers

Hello,
How to return an error from before hook, and "stop" the code?
I have:
screenshot_2
the error works, but the row in the database is added:
screenshot_1

Most helpful comment

Your error handler (.catch(error => {}) resolves the promise with undefined so what is ultimately returned will never be a rejected Promise. You either have to return Promise.reject(error) or throw error in the .catch handler.

As with any Promise problem, I highly recommend to look into async/await which will make things much more clear and avoid many problems like this.

All 7 comments

What you are showing does what you are asking. It stops the code and returns the error (you btw, don't have to set hook.error, just returning the rejected promise with the error should be enough).

Here, You can see:

  1. Error empty.name - _work_
  2. user-telegram - create - _work_
    how to stop on the error?
    screenshot_1

How does the whole hook look like?

const { MTProto } = require('telegram-mtproto');
// const errors = require('feathers-errors');
const checkPhone = require('./telegram/check-phone');
const sendCode = require('./telegram/send-code');

module.exports = function (options = {}) { // eslint-disable-line no-unused-vars
  return function sendTelegramCode (hook) {

      if (hook.data.phone.length == 11 || hook.data.phone.length == 12) {
        const phoneNumber = hook.data.phone;
        const phoneInternational = '+' + hook.data.phone;

        const userService = hook.app.services.user.Model;
        const response = userService.find({ where: { phone: phoneNumber }, limit: 1});

        let phoneRegistered = null;

        let client = null;

        if (hook.app.telegramClient) {
          client = hook.app.telegramClient;
        }else {
          client = MTProto();
        }

        console.log('hook.data', hook.data);
        return  response.then(result => {
            if (result) {
              // account with this phone is found
              return Promise.resolve(hook);
            }else {
              // account with this phone is NOT FOUND
              return checkPhone({
                phone: phoneNumber,
                app: hook.app,
                client
              }).then(
                data => {
                  phoneRegistered = data.phone_registered;
                  console.log('E 卯nregistrat telefonul?', phoneRegistered);
                  // phoneRegistered = true;  //// !!!!!!!!!!!!!!!!!!!!!!!!
                  if (phoneRegistered) {
                    // if user have a telegram account
                    return sendCode({
                      phone: phoneNumber,
                      app: hook.app,
                      client
                    }).then(
                      data => {
                        if (data.phone_code_hash) {
                          hook.data.phone_hash = data.phone_code_hash;
                        }
                        console.log('Sent code > ', data);
                        return Promise.resolve(hook);
                      }, error => {
                        console.error('Error send code', error);
                        return Promise.resolve(hook);
                      }
                    );
                  } else {
                    if (!hook.data.firstName || !hook.data.lastName) {
                      // hook.error = new errors.BadRequest({errors: { name: 'empty.name' }});
                      hook.error = new Error('empty.name');
                      // throw new Error('Text has to be provided');
                      // return Promise.resolve(hook);
                      return Promise.reject(hook.error);
                    }else {
                      return Promise.resolve(hook);
                    }
                  }
                },
                error => {
                  console.error('Error get telegram check code', error);
                  return Promise.resolve(hook);
                }
              );
            }
          }).catch(error => {
            console.error('Error get user', error);
          });
      }else {
        return Promise.resolve(hook);
      }
  };
};

Your error handler (.catch(error => {}) resolves the promise with undefined so what is ultimately returned will never be a rejected Promise. You either have to return Promise.reject(error) or throw error in the .catch handler.

As with any Promise problem, I highly recommend to look into async/await which will make things much more clear and avoid many problems like this.

@daffl Thank You!
Now I can get the error >
screenshot_7

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue with a link to this issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings