Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: another TS SDK error handling tweak #873

Merged
merged 6 commits into from
Oct 26, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions packages/sdk-rtl/src/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,9 +425,28 @@ export function addQueryParams(path: string, obj?: Values) {
return `${path}${qp ? '?' + qp : ''}`
}

const utf8 = 'utf-8'

/**
* Convert this value to a string representation however we can do it
* @param val
*/
function bufferString(val: any) {
const decoder = new TextDecoder('utf-8')
return decoder.decode(val)
let result = 'Unknown error'
try {
const decoder = new TextDecoder(utf8)
result = decoder.decode(val)
} catch (e: any) {
// Supremely ugly hack. If we get here, we must be in Node (or IE 11, but who cares about that?)
// Node requires an import from `util` for TextDecoder to be found BUT it "just has" Buffer unless WebPack messes us up
if (val instanceof Buffer) {
result = Buffer.from(val).toString(utf8)
} else {
// The fallback logic here will at least give us some information about the error being thrown
result = JSON.stringify(val)
}
}
return result
}

/**
Expand Down