Home > other >  Firebase Storage handle error for getDownloadURL
Firebase Storage handle error for getDownloadURL

Time:11-07

I have some minor issues with logging error messages and codes from Firebase storage.

Here is my code:

fileRef
  .getDownloadURL()
  .then((url) => {
    // some code
  })
  .catch((error) => {
    console.log(error)
  })

Into developer console I get this:

FirebaseError: Firebase Storage: Object 'bucketname/filename' does not exist. (storage/object-not-found)
{
  "error": {
    "code": 404,
    "message": "Not Found."
  }
}

console.log(error.code) doesn't work(

How can I get just error.code and error.message?

Thank you

CodePudding user response:

As a matter of fact, error.code will return one of the error codes listed in this documentation page.

Therefore, you should do as shown in the example in the documentation (Look a the "Web version 8" tab):

fileRef
  .getDownloadURL()
  .then((url) => {
    // some code
  })
.catch((error) => {
  // A full list of error codes is available at
  // https://firebase.google.com/docs/storage/web/handle-errors
  switch (error.code) {
    case 'storage/object-not-found':
      // File doesn't exist
      break;
    case 'storage/unauthorized':
      // User doesn't have permission to access the object
      break;
    case 'storage/canceled':
      // User canceled the upload
      break;

    // ...

    case 'storage/unknown':
      // Unknown error occurred, inspect the server response
      break;
  }
});
  • Related