Skip to content

Commit 5789b86

Browse files
committed
handle errors consistently
Currently, document.createElement is used in an attempt to generate an error similar to those thrown by native implementations of window.btoa and window.atob, throwing a ReferenceError in non-browser environments. This commit adds and makes use of an InvalidCharacterError constructor for environment-independent handling of errors.
1 parent 546f83c commit 5789b86

File tree

4 files changed

+24
-13
lines changed

4 files changed

+24
-13
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Base64.js
22

3-
< 500 byte* polyfill for browsers which don't provide [`window.btoa`][1] and
3+
500 byte* polyfill for browsers which don't provide [`window.btoa`][1] and
44
[`window.atob`][2].
55

66
Although the script does no harm in browsers which do provide these functions,

base64.js

+14-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
;(function () {
22

3-
var
4-
object = typeof exports != 'undefined' ? exports : this, // #8: web workers
5-
chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
6-
INVALID_CHARACTER_ERR = (function () {
7-
// fabricate a suitable error object
8-
try { document.createElement('$'); }
9-
catch (error) { return error; }}());
3+
var object = typeof exports != 'undefined' ? exports : this; // #8: web workers
4+
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
5+
6+
function InvalidCharacterError(message) {
7+
this.message = message;
8+
}
9+
InvalidCharacterError.prototype = new Error;
10+
InvalidCharacterError.prototype.name = 'InvalidCharacterError';
1011

1112
// encoder
1213
// [https://gist.github.com/999166] by [https://github.com/nignag]
@@ -23,7 +24,9 @@
2324
output += map.charAt(63 & block >> 8 - idx % 1 * 8)
2425
) {
2526
charCode = input.charCodeAt(idx += 3/4);
26-
if (charCode > 0xFF) throw INVALID_CHARACTER_ERR;
27+
if (charCode > 0xFF) {
28+
throw new InvalidCharacterError("'btoa' failed: The string to be encoded contains characters outside of the Latin1 range.");
29+
}
2730
block = block << 8 | charCode;
2831
}
2932
return output;
@@ -34,7 +37,9 @@
3437
object.atob || (
3538
object.atob = function (input) {
3639
input = input.replace(/=+$/, '')
37-
if (input.length % 4 == 1) throw INVALID_CHARACTER_ERR;
40+
if (input.length % 4 == 1) {
41+
throw new InvalidCharacterError("'atob' failed: The string to be decoded is not correctly encoded.");
42+
}
3843
for (
3944
// initialize result and counters
4045
var bc = 0, bs, buffer, idx = 0, output = '';

base64.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/base64.coffee

+8-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ describe 'Base64.js', ->
2323
assert.strictEqual btoa('qrstuvwxyz{|}~'), 'cXJzdHV2d3h5ent8fX4='
2424

2525
it 'cannot encode non-ASCII input', ->
26-
assert.throws -> btoa ''
26+
assert.throws (-> btoa ''), (err) ->
27+
err instanceof Error and
28+
err.name is 'InvalidCharacterError' and
29+
err.message is "'btoa' failed: The string to be encoded contains characters outside of the Latin1 range."
2730

2831
it 'can decode Base64-encoded input', ->
2932
assert.strictEqual atob(''), ''
@@ -43,4 +46,7 @@ describe 'Base64.js', ->
4346
assert.strictEqual atob('cXJzdHV2d3h5ent8fX4='), 'qrstuvwxyz{|}~'
4447

4548
it 'cannot decode invalid input', ->
46-
assert.throws -> atob 'a'
49+
assert.throws (-> atob 'a'), (err) ->
50+
err instanceof Error and
51+
err.name is 'InvalidCharacterError' and
52+
err.message is "'atob' failed: The string to be decoded is not correctly encoded."

0 commit comments

Comments
 (0)