Невозможно выдать ошибку из облачной функции в отчеты об ошибках Stackdriver - PullRequest
2 голосов
/ 07 мая 2019

Я пробовал этот способ (вручную сообщать об ошибках), но это не работает в облачных функциях Firebase.

https://firebase.google.com/docs/functions/reporting-errors?hl=en

Я пытался вызвать эту функцию в локальной среде, и она работала,Я пытался вызвать эту функцию в облачных функциях Firebase, и она не сработала.

Это функция, которую я развернул.

exports.hello = https.onRequest((_, response) => {
  const error = new Error('ERROR!!!!')
  return reportError(error)
    .then(result => {
      response.json(result); // {}
    })
    .catch(err => {
      console.error(err); // No error
    });
})

// https://firebase.google.com/docs/functions/reporting-errors?hl=en#sending_to_stackdriver
function reportError(err, context = {}) {
  // This is the name of the StackDriver log stream that will receive the log
  // entry. This name can be any valid log stream name, but must contain "err"
  // in order for the error to be picked up by StackDriver Error Reporting.
  const logName = 'errors';
  const log = logging.log(logName);

  // https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/MonitoredResource
  const metadata = {
    resource: {
      type: 'cloud_function',
      labels: {function_name: process.env.FUNCTION_NAME},
    },
  };

  // https://cloud.google.com/error-reporting/reference/rest/v1beta1/ErrorEvent
  const errorEvent = {
    message: err.stack,
    serviceContext: {
      service: process.env.FUNCTION_NAME,
      resourceType: 'cloud_function',
    },
    context: context,
  };

  // Write the error log entry
  return new Promise((resolve, reject) => {
    log.write(log.entry(metadata, errorEvent), (error) => {
      if (error) {
       return reject(error);
      }
      return resolve();
    });
  });
}

function reportError здесь: https://firebase.google.com/docs/functions/reporting-errors?hl=en#sending_to_stackdriver

Это package.json, который я использовал.

{
  "dependencies": {
    "firebase-admin": "^7.3.0",
    "firebase-functions": "^2.3.0"
  },
  "devDependencies": {
    "firebase-tools": "6.8.0"
  }
}

Функция reportError должна отправлять отчет в отчеты об ошибках Stackdriver.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...