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
Show file tree
Hide file tree
Changes from 5 commits
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
4 changes: 2 additions & 2 deletions packages/api-explorer/src/components/SideNav/SideNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import type {
TagList,
TypeTagList,
} from '@looker/sdk-codegen'
import { CriteriaToSet, tagTypes } from '@looker/sdk-codegen'
import { criteriaToSet, tagTypes } from '@looker/sdk-codegen'
import { useSelector } from 'react-redux'

import type { SpecAction } from '../../reducers'
Expand Down Expand Up @@ -125,7 +125,7 @@ export const SideNav: FC<SideNavProps> = ({ headless = false, spec }) => {
const api = spec.api || ({} as ApiModel)

if (debouncedPattern && api.search) {
results = api.search(pattern, CriteriaToSet(searchCriteria))
results = api.search(pattern, criteriaToSet(searchCriteria))
newTags = results.tags
newTypes = results.types
newTypeTags = tagTypes(api, results.types)
Expand Down
4 changes: 2 additions & 2 deletions packages/api-explorer/src/state/settings/slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { createSlice } from '@reduxjs/toolkit'
import type { PayloadAction } from '@reduxjs/toolkit'
import { createSliceHooks } from '@looker/redux'
import type { SearchCriterionTerm } from '@looker/sdk-codegen'
import { SearchAll, SetToCriteria } from '@looker/sdk-codegen'
import { SearchAll, setToCriteria } from '@looker/sdk-codegen'

import { saga } from './sagas'

Expand All @@ -45,7 +45,7 @@ export interface SettingState extends UserDefinedSettings {
export const defaultSettings = {
sdkLanguage: 'Python',
searchPattern: '',
searchCriteria: SetToCriteria(SearchAll) as SearchCriterionTerm[],
searchCriteria: setToCriteria(SearchAll) as SearchCriterionTerm[],
}

export const defaultSettingsState: SettingState = {
Expand Down
14 changes: 7 additions & 7 deletions packages/sdk-codegen/src/sdkModels.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {
ApiModel,
ArrayType,
camelCase,
CriteriaToSet,
criteriaToSet,
DelimArrayType,
EnumType,
firstCase,
Expand All @@ -54,7 +54,7 @@ import {
safeName,
SearchAll,
SearchCriterion,
SetToCriteria,
setToCriteria,
titleCase,
typeRefs,
} from './sdkModels'
Expand Down Expand Up @@ -1088,7 +1088,7 @@ describe('sdkModels', () => {
])
// this declaration pattern assures correct enum names
const names: SearchCriterionTerm[] = ['method', 'type', 'name']
const actual = CriteriaToSet(names)
const actual = criteriaToSet(names)
expect(actual).toEqual(expected)
})

Expand All @@ -1099,7 +1099,7 @@ describe('sdkModels', () => {
SearchCriterion.name,
])
const expected: SearchCriterionTerm[] = ['method', 'type', 'name']
const actual = SetToCriteria(criteria)
const actual = setToCriteria(criteria)
expect(actual).toEqual(expected)
})

Expand All @@ -1111,15 +1111,15 @@ describe('sdkModels', () => {
])
const values = ['method', 'type', 'name']
const names = values as SearchCriterionTerm[]
const actual = CriteriaToSet(names)
const actual = criteriaToSet(names)
expect(actual).toEqual(expected)
})

it('criteria is case sensitive', () => {
const expected = new Set([SearchCriterion.method, SearchCriterion.name])
const values = ['method', 'Type', 'name']
const names = values as SearchCriterionTerm[]
const actual = CriteriaToSet(names)
const actual = criteriaToSet(names)
expect(actual).toEqual(expected)
})

Expand All @@ -1130,7 +1130,7 @@ describe('sdkModels', () => {
SearchCriterion.name,
])
const expected = ['method', 'type', 'name']
const actual = SetToCriteria(criteria)
const actual = setToCriteria(criteria)
expect(actual).toEqual(expected)
})
})
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk-codegen/src/sdkModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ export const SearchAll: SearchCriteria = new Set([
SearchCriterion.response,
])

export const CriteriaToSet = (criteria: string[]): SearchCriteria => {
export const criteriaToSet = (criteria: string[]): SearchCriteria => {
const result: SearchCriteria = new Set()
criteria.forEach((name) => {
const val = SearchCriterion[name as SearchCriterionTerm]
Expand All @@ -396,7 +396,7 @@ export const CriteriaToSet = (criteria: string[]): SearchCriteria => {
return result
}

export const SetToCriteria = (criteria: SearchCriteria): string[] => {
export const setToCriteria = (criteria: SearchCriteria): string[] => {
const result: string[] = []
criteria.forEach((value) => result.push(SearchCriterion[value]))
return result
Expand Down
27 changes: 25 additions & 2 deletions packages/sdk-rtl/src/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,9 +425,32 @@ 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
try {
if (val instanceof Buffer) {
result = Buffer.from(val).toString(utf8)
} else {
result = JSON.stringify(val)
}
} catch (err: any) {
// The fallback logic here will at least give us some information about the error being thrown
result = JSON.stringify(val)
}
}
return result
}

/**
Expand Down