Skip to content

Export StorageError class instead of interface #6974

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/hungry-glasses-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"firebase": minor
"@firebase/storage": minor
---

Fixed issue where users were unable to check if an Error was an instance of `StorageError`.
79 changes: 70 additions & 9 deletions common/api-review/storage.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,11 @@ export function getStorage(app?: FirebaseApp, bucketUrl?: string): FirebaseStora
// @public
export function getStream(ref: StorageReference, maxDownloadSizeBytes?: number): NodeJS.ReadableStream;

// Warning: (ae-forgotten-export) The symbol "StorageError" needs to be exported by the entry point index.d.ts
//
// @internal (undocumented)
export function _invalidArgument(message: string): StorageError_2;
export function _invalidArgument(message: string): StorageError;

// @internal (undocumented)
export function _invalidRootOperation(name: string): StorageError_2;
export function _invalidRootOperation(name: string): StorageError;

// @public
export function list(ref: StorageReference, options?: ListOptions): Promise<ListResult>;
Expand Down Expand Up @@ -217,8 +215,71 @@ export interface SettableMetadata {
}

// @public
export interface StorageError extends FirebaseError {
serverResponse: string | null;
export class StorageError extends FirebaseError {
constructor(code: StorageErrorCode, message: string, status_?: number);
_codeEquals(code: StorageErrorCode): boolean;
customData: {
serverResponse: string | null;
};
get serverResponse(): null | string;
set serverResponse(serverResponse: string | null);
// (undocumented)
get status(): number;
set status(status: number);
}

// @public
export const enum StorageErrorCode {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, wasn't paying attention earlier to a new enum being exported as well, I think there are some problems with exporting a const enum (for a certain set of users) so I think this needs to be changed to just enum here

export const enum StorageErrorCode {

Background on const enum issues https://ncjamieson.com/dont-export-const-enums/

// (undocumented)
APP_DELETED = "app-deleted",
// (undocumented)
BUCKET_NOT_FOUND = "bucket-not-found",
// (undocumented)
CANCELED = "canceled",
// (undocumented)
CANNOT_SLICE_BLOB = "cannot-slice-blob",
// (undocumented)
INTERNAL_ERROR = "internal-error",
// (undocumented)
INVALID_ARGUMENT = "invalid-argument",
// (undocumented)
INVALID_ARGUMENT_COUNT = "invalid-argument-count",
// (undocumented)
INVALID_CHECKSUM = "invalid-checksum",
// (undocumented)
INVALID_DEFAULT_BUCKET = "invalid-default-bucket",
// (undocumented)
INVALID_EVENT_NAME = "invalid-event-name",
// (undocumented)
INVALID_FORMAT = "invalid-format",
// (undocumented)
INVALID_ROOT_OPERATION = "invalid-root-operation",
// (undocumented)
INVALID_URL = "invalid-url",
// (undocumented)
NO_DEFAULT_BUCKET = "no-default-bucket",
// (undocumented)
NO_DOWNLOAD_URL = "no-download-url",
// (undocumented)
OBJECT_NOT_FOUND = "object-not-found",
// (undocumented)
PROJECT_NOT_FOUND = "project-not-found",
// (undocumented)
QUOTA_EXCEEDED = "quota-exceeded",
// (undocumented)
RETRY_LIMIT_EXCEEDED = "retry-limit-exceeded",
// (undocumented)
SERVER_FILE_WRONG_SIZE = "server-file-wrong-size",
// (undocumented)
UNAUTHENTICATED = "unauthenticated",
// (undocumented)
UNAUTHORIZED = "unauthorized",
// (undocumented)
UNAUTHORIZED_APP = "unauthorized-app",
// (undocumented)
UNKNOWN = "unknown",
// (undocumented)
UNSUPPORTED_ENVIRONMENT = "unsupported-environment"
}

// @public
Expand Down Expand Up @@ -318,20 +379,20 @@ export class _UploadTask {
constructor(ref: _Reference, blob: _FbsBlob, metadata?: Metadata | null);
_blob: _FbsBlob;
cancel(): boolean;
catch<T>(onRejected: (p1: StorageError_2) => T | Promise<T>): Promise<T>;
catch<T>(onRejected: (p1: StorageError) => T | Promise<T>): Promise<T>;
// (undocumented)
isExponentialBackoffExpired(): boolean;
// Warning: (ae-forgotten-export) The symbol "Metadata" needs to be exported by the entry point index.d.ts
_metadata: Metadata | null;
// Warning: (ae-forgotten-export) The symbol "Unsubscribe" needs to be exported by the entry point index.d.ts
// Warning: (ae-forgotten-export) The symbol "Subscribe" needs to be exported by the entry point index.d.ts
on(type: _TaskEvent, nextOrObserver?: StorageObserver<UploadTaskSnapshot> | null | ((snapshot: UploadTaskSnapshot) => unknown), error?: ((a: StorageError_2) => unknown) | null, completed?: CompleteFn | null): Unsubscribe_2 | Subscribe_2<UploadTaskSnapshot>;
on(type: _TaskEvent, nextOrObserver?: StorageObserver<UploadTaskSnapshot> | null | ((snapshot: UploadTaskSnapshot) => unknown), error?: ((a: StorageError) => unknown) | null, completed?: CompleteFn | null): Unsubscribe_2 | Subscribe_2<UploadTaskSnapshot>;
pause(): boolean;
resume(): boolean;
get snapshot(): UploadTaskSnapshot;
// Warning: (ae-forgotten-export) The symbol "InternalTaskState" needs to be exported by the entry point index.d.ts
_state: InternalTaskState;
then<U>(onFulfilled?: ((value: UploadTaskSnapshot) => U | Promise<U>) | null, onRejected?: ((error: StorageError_2) => U | Promise<U>) | null): Promise<U>;
then<U>(onFulfilled?: ((value: UploadTaskSnapshot) => U | Promise<U>) | null, onRejected?: ((error: StorageError) => U | Promise<U>) | null): Promise<U>;
_transferred: number;
}

Expand Down
22 changes: 4 additions & 18 deletions packages/storage/src/public-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@

// eslint-disable-next-line import/no-extraneous-dependencies
import { FirebaseApp, _FirebaseService } from '@firebase/app';
import {
CompleteFn,
FirebaseError,
NextFn,
Subscribe,
Unsubscribe
} from '@firebase/util';
import { CompleteFn, NextFn, Subscribe, Unsubscribe } from '@firebase/util';
import { StorageError } from './implementation/error';

export { StorageError, StorageErrorCode } from './implementation/error';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be exported directly from src/api.ts. I think we had some weird bugs before when non-types were exported from a file that otherwise only exported types, but even if that's not a problem, just for organizational purposes.


/**
* A Firebase Storage instance.
Expand Down Expand Up @@ -249,17 +246,6 @@ export type TaskEvent = 'state_changed';
*/
export type TaskState = 'running' | 'paused' | 'success' | 'canceled' | 'error';

/**
* An error returned by the Firebase Storage SDK.
* @public
*/
export interface StorageError extends FirebaseError {
/**
* A server response message for the error, if applicable.
*/
serverResponse: string | null;
}

/**
* A stream observer for Firebase Storage.
* @public
Expand Down