LoginSignup
0
0

More than 3 years have passed since last update.

FireBaseのStorageは存在しないパスを指定するとErrorを投げるので困る

Posted at

firebaseのstorageのダウンロードURLを生成する場合、

const url = await storage.ref(path).getDownloadUrl();

と言うような感じに書きますが、このgetDownloadUrl、pathに間違ったものを渡すとエラーを投げてきます。
確かに、エラー以外に何を返して欲しいのかと言うと思いつかないんですが、
存在しない場合URLは404返すだけだろうという感覚でいると穴に落ちます。

対応策

エラー時用のURLを用意して以下のようにcatchで対応するのがいいかと。

const url = await storage.ref(path).getDownloadUrl().catch((err) => {
 //エラーのlogging等が必要な場合はこの辺りで。
 return urlForEmpty;
});
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