Skip to content

Commit

Permalink
Storage typing updates (#5359)
Browse files Browse the repository at this point in the history
* rename to StorageError

* more type safety

* Update API reports

* update api report

Co-authored-by: Feiyang1 <Feiyang1@users.noreply.github.com>
  • Loading branch information
Feiyang1 and Feiyang1 committed Aug 24, 2021
1 parent a9a4560 commit 636ee41
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 129 deletions.
40 changes: 20 additions & 20 deletions common/api-review/storage.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@ export interface FirebaseStorage extends _FirebaseService {
maxUploadRetryTime: number;
}

// @public
export interface FirebaseStorageError extends FirebaseError {
serverResponse: string | null;
}

// @internal
export class _FirebaseStorageImpl implements FirebaseStorage {
constructor(
Expand Down Expand Up @@ -129,13 +124,13 @@ export function getMetadata(ref: StorageReference): Promise<FullMetadata>;
// @public
export function getStorage(app?: FirebaseApp, bucketUrl?: string): FirebaseStorage;

// Warning: (ae-forgotten-export) The symbol "FirebaseStorageError" needs to be exported by the entry point index.d.ts
// 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): FirebaseStorageError_2;
export function _invalidArgument(message: string): StorageError_2;

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

// @public
export function list(ref: StorageReference, options?: ListOptions): Promise<ListResult>;
Expand Down Expand Up @@ -211,12 +206,17 @@ export interface SettableMetadata {
} | undefined;
}

// @public
export interface StorageError extends FirebaseError {
serverResponse: string | null;
}

// @public
export interface StorageObserver<T> {
// (undocumented)
complete?: CompleteFn | null;
// (undocumented)
error?: (error: FirebaseStorageError) => void | null;
error?: (error: StorageError) => void | null;
// (undocumented)
next?: NextFn<T> | null;
}
Expand All @@ -233,14 +233,14 @@ export interface StorageReference {
}

// @public
export type StringFormat = string;
export type StringFormat = typeof StringFormat[keyof typeof StringFormat];

// @public
export const StringFormat: {
RAW: string;
BASE64: string;
BASE64URL: string;
DATA_URL: string;
readonly RAW: "raw";
readonly BASE64: "base64";
readonly BASE64URL: "base64url";
readonly DATA_URL: "data_url";
};

// @public
Expand Down Expand Up @@ -295,31 +295,31 @@ export function uploadString(ref: StorageReference, value: string, format?: stri
// @public
export interface UploadTask {
cancel(): boolean;
catch(onRejected: (error: FirebaseStorageError) => unknown): Promise<unknown>;
on(event: TaskEvent, nextOrObserver?: StorageObserver<UploadTaskSnapshot> | null | ((snapshot: UploadTaskSnapshot) => unknown), error?: ((a: FirebaseStorageError) => unknown) | null, complete?: Unsubscribe | null): Unsubscribe | Subscribe<UploadTaskSnapshot>;
catch(onRejected: (error: StorageError) => unknown): Promise<unknown>;
on(event: TaskEvent, nextOrObserver?: StorageObserver<UploadTaskSnapshot> | null | ((snapshot: UploadTaskSnapshot) => unknown), error?: ((a: StorageError) => unknown) | null, complete?: Unsubscribe | null): Unsubscribe | Subscribe<UploadTaskSnapshot>;
pause(): boolean;
resume(): boolean;
snapshot: UploadTaskSnapshot;
then(onFulfilled?: ((snapshot: UploadTaskSnapshot) => unknown) | null, onRejected?: ((error: FirebaseStorageError) => unknown) | null): Promise<unknown>;
then(onFulfilled?: ((snapshot: UploadTaskSnapshot) => unknown) | null, onRejected?: ((error: StorageError) => unknown) | null): Promise<unknown>;
}

// @internal
export class _UploadTask {
constructor(ref: _Reference, blob: _FbsBlob, metadata?: Metadata | null);
_blob: _FbsBlob;
cancel(): boolean;
catch<T>(onRejected: (p1: FirebaseStorageError_2) => T | Promise<T>): Promise<T>;
catch<T>(onRejected: (p1: StorageError_2) => T | Promise<T>): Promise<T>;
// 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: FirebaseStorageError_2) => unknown) | null, completed?: Unsubscribe_2 | null): Unsubscribe_2 | Subscribe_2<UploadTaskSnapshot>;
on(type: _TaskEvent, nextOrObserver?: StorageObserver<UploadTaskSnapshot> | null | ((snapshot: UploadTaskSnapshot) => unknown), error?: ((a: StorageError_2) => unknown) | null, completed?: Unsubscribe_2 | 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: FirebaseStorageError_2) => U | Promise<U>) | null): Promise<U>;
then<U>(onFulfilled?: ((value: UploadTaskSnapshot) => U | Promise<U>) | null, onRejected?: ((error: StorageError_2) => U | Promise<U>) | null): Promise<U>;
_transferred: number;
}

Expand Down
110 changes: 55 additions & 55 deletions packages/storage/src/implementation/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import { CONFIG_STORAGE_BUCKET_KEY } from './constants';
* An error returned by the Firebase Storage SDK.
* @public
*/
export class FirebaseStorageError extends FirebaseError {
export class StorageError extends FirebaseError {
private readonly _baseMessage: string;
/**
* Stores custom error data unque to FirebaseStorageError.
* Stores custom error data unque to StorageError.
*/
customData: { serverResponse: string | null } = { serverResponse: null };

Expand All @@ -39,9 +39,9 @@ export class FirebaseStorageError extends FirebaseError {
`Firebase Storage: ${message} (${prependCode(code)})`
);
this._baseMessage = this.message;
// Without this, `instanceof FirebaseStorageError`, in tests for example,
// Without this, `instanceof StorageError`, in tests for example,
// returns false.
Object.setPrototypeOf(this, FirebaseStorageError.prototype);
Object.setPrototypeOf(this, StorageError.prototype);
}

/**
Expand Down Expand Up @@ -72,7 +72,7 @@ export const errors = {};

/**
* @public
* Error codes that can be attached to `FirebaseStorageError`s.
* Error codes that can be attached to `StorageError`s.
*/
export const enum StorageErrorCode {
// Shared between all platforms
Expand Down Expand Up @@ -108,36 +108,36 @@ export function prependCode(code: StorageErrorCode): string {
return 'storage/' + code;
}

export function unknown(): FirebaseStorageError {
export function unknown(): StorageError {
const message =
'An unknown error occurred, please check the error payload for ' +
'server response.';
return new FirebaseStorageError(StorageErrorCode.UNKNOWN, message);
return new StorageError(StorageErrorCode.UNKNOWN, message);
}

export function objectNotFound(path: string): FirebaseStorageError {
return new FirebaseStorageError(
export function objectNotFound(path: string): StorageError {
return new StorageError(
StorageErrorCode.OBJECT_NOT_FOUND,
"Object '" + path + "' does not exist."
);
}

export function bucketNotFound(bucket: string): FirebaseStorageError {
return new FirebaseStorageError(
export function bucketNotFound(bucket: string): StorageError {
return new StorageError(
StorageErrorCode.BUCKET_NOT_FOUND,
"Bucket '" + bucket + "' does not exist."
);
}

export function projectNotFound(project: string): FirebaseStorageError {
return new FirebaseStorageError(
export function projectNotFound(project: string): StorageError {
return new StorageError(
StorageErrorCode.PROJECT_NOT_FOUND,
"Project '" + project + "' does not exist."
);
}

export function quotaExceeded(bucket: string): FirebaseStorageError {
return new FirebaseStorageError(
export function quotaExceeded(bucket: string): StorageError {
return new StorageError(
StorageErrorCode.QUOTA_EXCEEDED,
"Quota for bucket '" +
bucket +
Expand All @@ -146,29 +146,29 @@ export function quotaExceeded(bucket: string): FirebaseStorageError {
);
}

export function unauthenticated(): FirebaseStorageError {
export function unauthenticated(): StorageError {
const message =
'User is not authenticated, please authenticate using Firebase ' +
'Authentication and try again.';
return new FirebaseStorageError(StorageErrorCode.UNAUTHENTICATED, message);
return new StorageError(StorageErrorCode.UNAUTHENTICATED, message);
}

export function unauthorizedApp(): FirebaseStorageError {
return new FirebaseStorageError(
export function unauthorizedApp(): StorageError {
return new StorageError(
StorageErrorCode.UNAUTHORIZED_APP,
'This app does not have permission to access Firebase Storage on this project.'
);
}

export function unauthorized(path: string): FirebaseStorageError {
return new FirebaseStorageError(
export function unauthorized(path: string): StorageError {
return new StorageError(
StorageErrorCode.UNAUTHORIZED,
"User does not have permission to access '" + path + "'."
);
}

export function retryLimitExceeded(): FirebaseStorageError {
return new FirebaseStorageError(
export function retryLimitExceeded(): StorageError {
return new StorageError(
StorageErrorCode.RETRY_LIMIT_EXCEEDED,
'Max retry time for operation exceeded, please try again.'
);
Expand All @@ -178,8 +178,8 @@ export function invalidChecksum(
path: string,
checksum: string,
calculated: string
): FirebaseStorageError {
return new FirebaseStorageError(
): StorageError {
return new StorageError(
StorageErrorCode.INVALID_CHECKSUM,
"Uploaded/downloaded object '" +
path +
Expand All @@ -191,36 +191,36 @@ export function invalidChecksum(
);
}

export function canceled(): FirebaseStorageError {
return new FirebaseStorageError(
export function canceled(): StorageError {
return new StorageError(
StorageErrorCode.CANCELED,
'User canceled the upload/download.'
);
}

export function invalidEventName(name: string): FirebaseStorageError {
return new FirebaseStorageError(
export function invalidEventName(name: string): StorageError {
return new StorageError(
StorageErrorCode.INVALID_EVENT_NAME,
"Invalid event name '" + name + "'."
);
}

export function invalidUrl(url: string): FirebaseStorageError {
return new FirebaseStorageError(
export function invalidUrl(url: string): StorageError {
return new StorageError(
StorageErrorCode.INVALID_URL,
"Invalid URL '" + url + "'."
);
}

export function invalidDefaultBucket(bucket: string): FirebaseStorageError {
return new FirebaseStorageError(
export function invalidDefaultBucket(bucket: string): StorageError {
return new StorageError(
StorageErrorCode.INVALID_DEFAULT_BUCKET,
"Invalid default bucket '" + bucket + "'."
);
}

export function noDefaultBucket(): FirebaseStorageError {
return new FirebaseStorageError(
export function noDefaultBucket(): StorageError {
return new StorageError(
StorageErrorCode.NO_DEFAULT_BUCKET,
'No default bucket ' +
"found. Did you set the '" +
Expand All @@ -229,22 +229,22 @@ export function noDefaultBucket(): FirebaseStorageError {
);
}

export function cannotSliceBlob(): FirebaseStorageError {
return new FirebaseStorageError(
export function cannotSliceBlob(): StorageError {
return new StorageError(
StorageErrorCode.CANNOT_SLICE_BLOB,
'Cannot slice blob for upload. Please retry the upload.'
);
}

export function serverFileWrongSize(): FirebaseStorageError {
return new FirebaseStorageError(
export function serverFileWrongSize(): StorageError {
return new StorageError(
StorageErrorCode.SERVER_FILE_WRONG_SIZE,
'Server recorded incorrect upload file size, please retry the upload.'
);
}

export function noDownloadURL(): FirebaseStorageError {
return new FirebaseStorageError(
export function noDownloadURL(): StorageError {
return new StorageError(
StorageErrorCode.NO_DOWNLOAD_URL,
'The given file does not have any download URLs.'
);
Expand All @@ -253,16 +253,16 @@ export function noDownloadURL(): FirebaseStorageError {
/**
* @internal
*/
export function invalidArgument(message: string): FirebaseStorageError {
return new FirebaseStorageError(StorageErrorCode.INVALID_ARGUMENT, message);
export function invalidArgument(message: string): StorageError {
return new StorageError(StorageErrorCode.INVALID_ARGUMENT, message);
}

export function invalidArgumentCount(
argMin: number,
argMax: number,
fnName: string,
real: number
): FirebaseStorageError {
): StorageError {
let countPart;
let plural;
if (argMin === argMax) {
Expand All @@ -272,7 +272,7 @@ export function invalidArgumentCount(
countPart = 'between ' + argMin + ' and ' + argMax;
plural = 'arguments';
}
return new FirebaseStorageError(
return new StorageError(
StorageErrorCode.INVALID_ARGUMENT_COUNT,
'Invalid argument count in `' +
fnName +
Expand All @@ -286,8 +286,8 @@ export function invalidArgumentCount(
);
}

export function appDeleted(): FirebaseStorageError {
return new FirebaseStorageError(
export function appDeleted(): StorageError {
return new StorageError(
StorageErrorCode.APP_DELETED,
'The Firebase app was deleted.'
);
Expand All @@ -298,8 +298,8 @@ export function appDeleted(): FirebaseStorageError {
*
* @internal
*/
export function invalidRootOperation(name: string): FirebaseStorageError {
return new FirebaseStorageError(
export function invalidRootOperation(name: string): StorageError {
return new StorageError(
StorageErrorCode.INVALID_ROOT_OPERATION,
"The operation '" +
name +
Expand All @@ -315,8 +315,8 @@ export function invalidRootOperation(name: string): FirebaseStorageError {
export function invalidFormat(
format: string,
message: string
): FirebaseStorageError {
return new FirebaseStorageError(
): StorageError {
return new StorageError(
StorageErrorCode.INVALID_FORMAT,
"String does not match format '" + format + "': " + message
);
Expand All @@ -325,8 +325,8 @@ export function invalidFormat(
/**
* @param message - A message describing the internal error.
*/
export function unsupportedEnvironment(message: string): FirebaseStorageError {
throw new FirebaseStorageError(
export function unsupportedEnvironment(message: string): StorageError {
throw new StorageError(
StorageErrorCode.UNSUPPORTED_ENVIRONMENT,
message
);
Expand All @@ -335,8 +335,8 @@ export function unsupportedEnvironment(message: string): FirebaseStorageError {
/**
* @param message - A message describing the internal error.
*/
export function internalError(message: string): FirebaseStorageError {
throw new FirebaseStorageError(
export function internalError(message: string): StorageError {
throw new StorageError(
StorageErrorCode.INTERNAL_ERROR,
'Internal error: ' + message
);
Expand Down
Loading

0 comments on commit 636ee41

Please sign in to comment.