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(app-shell): forward usb request errors and log #15959

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 9 additions & 4 deletions app-shell/src/system-info/usb-devices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,15 @@ function upstreamDeviceFromUsbDeviceWinAPI(
const parsePoshJsonOutputToWmiObjectArray = (
dump: string
): WmiObject[] => {
if (dump[0] === '[') {
return JSON.parse(dump) as WmiObject[]
} else {
return [JSON.parse(dump) as WmiObject]
try {
if (dump[0] === '[') {
return JSON.parse(dump) as WmiObject[]
} else {
return [JSON.parse(dump) as WmiObject]
}
} catch (e: any) {
log.error(`Failed to parse posh json output: ${dump}`)
throw e
}
}
if (dump.stderr !== '') {
Expand Down
24 changes: 20 additions & 4 deletions app-shell/src/usb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ function reconstructFormData(ipcSafeFormData: IPCSafeFormData): FormData {
return result
}

const cloneError = (e: any): Record<string, unknown> =>
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
Object.entries(axios.isAxiosError(e) ? e.toJSON() : e).reduce<
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, in practice, is this ever not an axios error? It's probably safer to do this regardless.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not as far as I know, but this way if we ever make a new bug it will at least get reported in the browser logs

Record<string, unknown>
>((acc, [k, v]) => {
try {
acc[k] = structuredClone(v)
return acc
} catch (e) {
return acc
}
}, {})

async function usbListener(
_event: IpcMainInvokeEvent,
config: AxiosRequestConfig
Expand All @@ -114,21 +127,24 @@ async function usbListener(

const usbHttpAgent = getSerialPortHttpAgent()
try {
usbLog.silly(`${config.method} ${config.url} timeout=${config.timeout}`)
const response = await axios.request({
httpAgent: usbHttpAgent,
...config,
data,
headers: { ...config.headers, ...formHeaders },
})
usbLog.silly(`${config.method} ${config.url} resolved ok`)
return {
error: false,
error: null,
data: response.data,
status: response.status,
statusText: response.statusText,
}
} catch (e) {
if (e instanceof Error) {
console.log(`axios request error ${e?.message ?? 'unknown'}`)
} catch (e: any) {
usbLog.info(`${config.method} ${config.url} failed: ${e}`)
return {
error: cloneError(e),
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion app/src/redux/shell/remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ export async function appShellRequestor<Data>(
: data
const configProxy = { ...config, data: formDataProxy }

return await remote.ipcRenderer.invoke('usb:request', configProxy)
const result = await remote.ipcRenderer.invoke('usb:request', configProxy)
if (result?.error != null) {
throw result.error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah

}
return result
}

interface CallbackStore {
Expand Down
Loading