Skip to content

Commit

Permalink
Export StorageError class instead of interface (#6974)
Browse files Browse the repository at this point in the history
  • Loading branch information
maneesht committed Jan 27, 2023
1 parent f8ddadb commit 825e648
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 35 deletions.
8 changes: 8 additions & 0 deletions .changeset/hungry-glasses-impress.md
@@ -0,0 +1,8 @@
---
"firebase": minor
"@firebase/storage": minor
"@firebase/storage-types": minor
"@firebase/storage-compat": 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
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 enum StorageErrorCode {
// (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
52 changes: 50 additions & 2 deletions packages/firebase/compat/index.d.ts
Expand Up @@ -8017,11 +8017,59 @@ declare namespace firebase.storage {
md5Hash?: string | null;
}

/**
* Error codes that can be attached to `StorageError`s.
*/
export enum StorageErrorCode {
UNKNOWN = 'unknown',
OBJECT_NOT_FOUND = 'object-not-found',
BUCKET_NOT_FOUND = 'bucket-not-found',
PROJECT_NOT_FOUND = 'project-not-found',
QUOTA_EXCEEDED = 'quota-exceeded',
UNAUTHENTICATED = 'unauthenticated',
UNAUTHORIZED = 'unauthorized',
UNAUTHORIZED_APP = 'unauthorized-app',
RETRY_LIMIT_EXCEEDED = 'retry-limit-exceeded',
INVALID_CHECKSUM = 'invalid-checksum',
CANCELED = 'canceled',
INVALID_EVENT_NAME = 'invalid-event-name',
INVALID_URL = 'invalid-url',
INVALID_DEFAULT_BUCKET = 'invalid-default-bucket',
NO_DEFAULT_BUCKET = 'no-default-bucket',
CANNOT_SLICE_BLOB = 'cannot-slice-blob',
SERVER_FILE_WRONG_SIZE = 'server-file-wrong-size',
NO_DOWNLOAD_URL = 'no-download-url',
INVALID_ARGUMENT = 'invalid-argument',
INVALID_ARGUMENT_COUNT = 'invalid-argument-count',
APP_DELETED = 'app-deleted',
INVALID_ROOT_OPERATION = 'invalid-root-operation',
INVALID_FORMAT = 'invalid-format',
INTERNAL_ERROR = 'internal-error',
UNSUPPORTED_ENVIRONMENT = 'unsupported-environment'
}

/**
* An error returned by the Firebase Storage SDK.
*/
interface FirebaseStorageError extends FirebaseError {
serverResponse: string | null;
export interface FirebaseStorageError extends FirebaseError {
/**
* Stores custom error data unque to StorageError.
*/
customData: {
serverResponse: string | null;
};

get status(): number;
set status(status: number);
/**
* Compares a StorageErrorCode against this error's code, filtering out the prefix.
*/
_codeEquals(code: StorageErrorCode): boolean;
/**
* Optional response message that was added by the server.
*/
get serverResponse(): null | string;
set serverResponse(serverResponse: string | null);
}

interface StorageObserver<T> {
Expand Down
2 changes: 1 addition & 1 deletion packages/storage-compat/src/task.ts
Expand Up @@ -65,7 +65,7 @@ export class UploadTaskCompat implements types.UploadTask, Compat<UploadTask> {
| types.StorageObserver<UploadTaskSnapshotCompat>
| null
| ((a: UploadTaskSnapshotCompat) => unknown),
error?: (error: StorageError) => void | null,
error?: ((error: StorageError) => void) | null,
completed?: () => void | null
): Unsubscribe | Subscribe<UploadTaskSnapshotCompat> {
let wrappedNextOrObserver:
Expand Down
52 changes: 48 additions & 4 deletions packages/storage-types/index.d.ts
Expand Up @@ -91,16 +91,60 @@ export interface UploadMetadata extends SettableMetadata {
md5Hash?: string | null;
}

interface FirebaseStorageError extends FirebaseError {
serverResponse: string | null;
}

export interface StorageObserver<T> {
next?: NextFn<T> | null;
error?: (error: FirebaseStorageError) => void | null;
complete?: CompleteFn | null;
}

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

export interface FirebaseStorageError extends FirebaseError {
/**
* Stores custom error data unque to StorageError.
*/
customData: {
serverResponse: string | null;
};

get status(): number;
set status(status: number);
/**
* Compares a StorageErrorCode against this error's code, filtering out the prefix.
*/
_codeEquals(code: StorageErrorCode): boolean;
/**
* Optional response message that was added by the server.
*/
get serverResponse(): null | string;
set serverResponse(serverResponse: string | null);
}
export interface UploadTask {
cancel(): boolean;
catch(onRejected: (error: FirebaseStorageError) => any): Promise<any>;
Expand Down
2 changes: 2 additions & 0 deletions packages/storage/src/api.ts
Expand Up @@ -59,6 +59,8 @@ import { StringFormat } from './implementation/string';

export { EmulatorMockTokenOptions } from '@firebase/util';

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

/**
* Public types.
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/storage/src/implementation/error.ts
Expand Up @@ -85,7 +85,7 @@ export const errors = {};
* @public
* Error codes that can be attached to `StorageError`s.
*/
export const enum StorageErrorCode {
export enum StorageErrorCode {
// Shared between all platforms
UNKNOWN = 'unknown',
OBJECT_NOT_FOUND = 'object-not-found',
Expand Down
20 changes: 2 additions & 18 deletions packages/storage/src/public-types.ts
Expand Up @@ -17,13 +17,8 @@

// 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';

/**
* A Firebase Storage instance.
Expand Down Expand Up @@ -249,17 +244,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

0 comments on commit 825e648

Please sign in to comment.