Skip to content

Commit 512387b

Browse files
committed
consolidate error handling
1 parent 0e446d6 commit 512387b

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

packages/utils/src/string.ts

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,6 @@ export function isMatchingPattern(value: string, pattern: RegExp | string): bool
114114
export function unicodeToBase64(plaintext: string): string {
115115
const global = getGlobalObject();
116116

117-
// Cast to a string just in case we're given something else
118-
const stringifiedInput = String(plaintext);
119-
const errMsg = `Unable to convert to base64: ${
120-
stringifiedInput.length > 256 ? `${stringifiedInput.slice(0, 256)}...` : stringifiedInput
121-
}`;
122-
123117
// To account for the fact that different platforms use different character encodings natively, our `tracestate`
124118
// spec calls for all jsonified data to be encoded in UTF-8 bytes before being passed to the base64 encoder.
125119
try {
@@ -142,12 +136,17 @@ export function unicodeToBase64(plaintext: string): string {
142136
// unlike the browser, Node can go straight from bytes to base64
143137
return bytes.toString('base64');
144138
}
139+
140+
// we shouldn't ever get here, because one of `btoa` and `Buffer` should exist, but just in case...
141+
throw new SentryError('Neither `window.btoa` nor `global.Buffer` is defined.');
145142
} catch (err) {
143+
// Cast to a string just in case we're given something else
144+
const stringifiedInput = String(plaintext);
145+
const errMsg = `Unable to convert to base64: ${
146+
stringifiedInput.length > 256 ? `${stringifiedInput.slice(0, 256)}...` : stringifiedInput
147+
}`;
146148
throw new SentryError(`${errMsg}\nGot error: ${err}`);
147149
}
148-
149-
// we shouldn't ever get here, because one of `btoa` and `Buffer` should exist, but just in case...
150-
throw new SentryError(errMsg);
151150
}
152151

153152
/**
@@ -160,12 +159,6 @@ export function unicodeToBase64(plaintext: string): string {
160159
export function base64ToUnicode(base64String: string): string {
161160
const globalObject = getGlobalObject();
162161

163-
// we cast to a string just in case we're given something else
164-
const stringifiedInput = String(base64String);
165-
const errMsg = `Unable to convert from base64: ${
166-
stringifiedInput.length > 256 ? `${stringifiedInput.slice(0, 256)}...` : stringifiedInput
167-
}`;
168-
169162
// To account for the fact that different platforms use different character encodings natively, our `tracestate` spec
170163
// calls for all jsonified data to be encoded in UTF-8 bytes before being passed to the base64 encoder. So to reverse
171164
// the process, decode from base64 to bytes, then feed those bytes to a UTF-8 decoder.
@@ -188,10 +181,15 @@ export function base64ToUnicode(base64String: string): string {
188181
// decode using UTF-8
189182
return bytes.toString('utf-8');
190183
}
184+
185+
// we shouldn't ever get here, because one of `atob` and `Buffer` should exist, but just in case...
186+
throw new SentryError('Neither `window.atob` nor `global.Buffer` is defined.');
191187
} catch (err) {
188+
// we cast to a string just in case we're given something else
189+
const stringifiedInput = String(base64String);
190+
const errMsg = `Unable to convert from base64: ${
191+
stringifiedInput.length > 256 ? `${stringifiedInput.slice(0, 256)}...` : stringifiedInput
192+
}`;
192193
throw new SentryError(`${errMsg}\nGot error: ${err}`);
193194
}
194-
195-
// we shouldn't ever get here, because one of `atob` and `Buffer` should exist, but just in case...
196-
throw new SentryError(errMsg);
197195
}

0 commit comments

Comments
 (0)