简体   繁体   English

使用Promises更好地处理错误?

[英]Better error handling with Promises?

I am currently experimenting Google Firebase functions to access Google APIs. 我目前正在尝试使用Google Firebase函数来访问Google API。 It's running fine, but I am a little bit lost in trying to manage the errors that could be detected ... 它运行良好,但是我在尝试管理可以检测到的错误时有点迷路了...

In the .HTTPS getGoogleUsers functions , I would like to return an HTTP status code ( 200 or error code ) , and the data ( or error message ) .HTTPS getGoogleUsers函数中,我想返回一个HTTP状态代码(200或错误代码)和数据(或错误消息)

As far as I can see , I can get errors: 据我所见,我会得到错误:

  • from the connect() function ( 500: Internal server error or 401 Unauthorized ) connect()函数(500:内部服务器错误或401未经授权)

  • from the listUsers() function ( 500: Internal server error or 400 Bad Request ) listUsers()函数(500:内部服务器错误或400错误的请求)

Am I totally or partially wrong ? 我是全部还是部分错误? what should be my strategy in this case ? 在这种情况下,我应该采取什么策略? thanks for feedback .. 感谢您的反馈..

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

const {google} = require('googleapis');
const KEY = require('./service-key.json');

 // Create JSON Web Token Client
 function connect () {
  return new Promise((resolve, reject) => {
    const jwtClient = new google.auth.JWT(
      KEY.client_email,
      null,
      KEY.private_key,
      ['https://www.googleapis.com/auth/admin.directory.user'],
      'adminuser@mydomain.com'
    );
    jwtClient.authorize((err) => {
      if(err) {
        reject(err);
      } else {
        resolve(jwtClient);
      }
    });
  });
}

function listUsers (client) {
  return new Promise((resolve, reject) => {
    google.admin('directory_v1').users.list({
      auth: client,
      domain: 'mydomain.com',
    }, (err, response) => {
      if (err) {
        reject(err);
      }
      resolve(response.data.users);
    });
  });
}

function getAllUsers () {
  connect()
    .then(client => {
      return listUsers(client);
    })
    .catch(error => {
      return error;
    })
}
exports.getGoogleUsers = functions.https.onRequest((req, res) => {
  const users = getAllUsers();
  if (error) {
     status = error.status;
     data = error.message;
  } else {
    status = 200;
    data = users;
  }
  res.send({ status: status, datas: data })
});

I think you are looking for 我想你在找

function getAllUsers () {
  return connect().then(listUsers);
//^^^^^^
}

exports.getGoogleUsers = functions.https.onRequest((req, res) => {
  getAllUsers().then(users => {
    return {status: 200, datas: users};
  }, error => {
    return {status: error.status, datas: error.message};
  }).then(response => {
    res.send(response);
  });
});

This uses the .then(…, …) method with two callbacks to distinguish between success and error case, and to wait for the result of the promise. 它使用.then(…, …)方法和两个回调来区分成功和错误情况,并等待promise的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM