0
0

More than 1 year has passed since last update.

<firebase functions> custom errorを投げる

Posted at

firebase functions でオリジナルのエラーを投げたい場面。

自前でerrorを返しても常にinternalエラーが返ってきてしまう

[admin.FirebaseError: internal] {
   code: 'functions/internal',
   customData: undefined,
   name: 'FirebaseError',
   details: undefined
 }
functions.https.onRequest(async (req, res): Promise<void> => {
  res.status(200).send({message: 'error'});
}
functions.https.onRequest(async (req, res): Promise<void> => {
  res.status(500).send({message: 'error'});
}

解決法

今回は以下の2stepで行いました

  1. 関数の定義をonCallにする
  2. https.errorをthrowする

onCall は firebase sdkで呼び出す時のみ。
corsや認証の対応が楽にできる。

import { https } from 'firebase-functions';

functions.https.onCall(async (data, context): Promise<void> => {  
// do some
...

// error handling
throw new https.HttpsError('<定義されているcode>', '<custom message>')
}

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0