Firebase-js-sdk: Wrong type information for `firebase.storage.UploadTask.on(.., .., error, ..)`

Created on 2 Feb 2019  路  9Comments  路  Source: firebase/firebase-js-sdk


Describe your environment

  • Operating System version: win10
  • Browser version: Chrome 71.0.3578.98
  • Firebase SDK version: 5.8.0
  • Firebase Product: Storage

Describe the problem

The firebase.storage.UploadTask interface has the method on( .., .., error?: ((a: Error) => any) | null, ..).
Checking the error code Error interface doesn't have the code property.

Steps to reproduce:


Using the example provided here, in the callback function for error a switch-case statement is used to switch among the error codes. The problem is that code is not recognized as property.

```javascript
...
switch (error.code) {
case 'storage/unauthorized':
// User doesn't have permission to access the object
break;
...
````

Possible solution

I suppose the solution is to use FirebaseError instead of Error line 720 of type definition.
https://github.com/firebase/firebase-js-sdk/blob/6b53e0058483c9002d2fe56119f86fc9fb96b56c/packages/storage-types/index.d.ts#L84

Thanks

firestore storage

All 9 comments

The problem is also present with Firestore's types:

Waiting for the solution, a workaround could be the explicit casting as follow:

var error = <Firebase.FirebaseError>e;

I guess this will work in the same way with your types @romain-faust

Hope this helps
Ciao

Discussing internally for Firestore, changing Error to FirestoreError sounds like a good idea as long as this doesn't cause a breaking change for anybody (hopefully not, but I'm 100% sure how TypeScript deals with this).

We also need to audit our code to make sure we really do guarantee that it's always a FirestoreError.

Any progress on this?

Fixing this on the Firestore types would be nice as well.

Thank you for bringing this back to our attention. We are re-evaluating the fix and will update this issue once a decision is made.

Doing this for now

      (error) => {
        const firebaseError = error as firebase.FirebaseError;

        // A full list of error codes is available at
        // https://firebase.google.com/docs/storage/web/handle-errors
        switch (firebaseError.code) {
          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 error.serverResponse
            break;
        }
    }

Update: The changes to the Firestore API are being reviewed internally. This API review process is standard for any changes to the public API. If approved, I'll merge #3418. I'm aiming for the first week of August 2020 to have it merged.

Update: Make that the 2nd week of August 2020 :)

The fix for Firestore has been merged: https://github.com/firebase/firebase-js-sdk/pull/3418.

The fix for firebase.storage is still TODO.

Was this page helpful?
0 / 5 - 0 ratings