Skip to content

Commit 8876b78

Browse files
authored
Updated base64 error to be more descriptive (#6746)
1 parent bec38d9 commit 8876b78

File tree

5 files changed

+56
-0
lines changed

5 files changed

+56
-0
lines changed

.changeset/long-shirts-explode.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@firebase/storage": patch
3+
---
4+
5+
Fixed issue where if btoa wasn't supported in the environment, then the user would get a generic message.

packages/storage/src/implementation/error.ts

+7
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,13 @@ export function noDownloadURL(): StorageError {
261261
);
262262
}
263263

264+
export function missingPolyFill(polyFill: string): StorageError {
265+
return new StorageError(
266+
StorageErrorCode.UNSUPPORTED_ENVIRONMENT,
267+
`${polyFill} is missing. Make sure to install the required polyfills. See https://firebase.google.com/docs/web/environments-js-sdk#polyfills for more information.`
268+
);
269+
}
270+
264271
/**
265272
* @internal
266273
*/

packages/storage/src/implementation/string.ts

+3
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ export function base64Bytes_(format: StringFormat, value: string): Uint8Array {
184184
try {
185185
bytes = decodeBase64(value);
186186
} catch (e) {
187+
if ((e as Error).message.includes('polyfill')) {
188+
throw e;
189+
}
187190
throw invalidFormat(format, 'Invalid character found');
188191
}
189192
const array = new Uint8Array(bytes.length);

packages/storage/src/platform/browser/base64.ts

+5
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@
1515
* limitations under the License.
1616
*/
1717

18+
import { missingPolyFill } from '../../implementation/error';
19+
1820
/** Converts a Base64 encoded string to a binary string. */
1921
export function decodeBase64(encoded: string): string {
22+
if (typeof atob === 'undefined') {
23+
throw missingPolyFill('base-64');
24+
}
2025
return atob(encoded);
2126
}
2227

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @license
3+
* Copyright 2022 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { expect } from 'chai';
19+
import { missingPolyFill } from '../../src/implementation/error';
20+
import { dataFromString, StringFormat } from '../../src/implementation/string';
21+
import { assertThrows } from '../unit/testshared';
22+
23+
describe('String browser tests', () => {
24+
it('should reject if atob is undefined', () => {
25+
const originalAToB = global.atob;
26+
// @ts-ignore
27+
global.atob = undefined;
28+
const str = 'CpYlM1-XsGxTd1n6izHMU_yY3Bw=';
29+
30+
const error = assertThrows(() => {
31+
dataFromString(StringFormat.BASE64URL, str);
32+
}, 'storage/unsupported-environment');
33+
expect(error.message).to.equal(missingPolyFill('base-64').message);
34+
global.atob = originalAToB;
35+
});
36+
});

0 commit comments

Comments
 (0)