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で行いました
- 関数の定義をonCallにする
- 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>')
}
参考