Skip to content

Commit 4731795

Browse files
feat: add error communication
1 parent 3dbf562 commit 4731795

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export type Definition = {
4343
id: number
4444
inputValue: string
4545
}
46+
error: Error
4647
}
4748
child2parent: {
4849
search: {

src/view.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,16 @@ async function uniqueCommand(
102102
// interrupted proc will be replaced by latter proc
103103
child = undefined
104104
} catch (e) {
105-
console.error(e)
105+
console.info(`search aborted: `, e)
106106
}
107107
}
108108

109-
async function getPatternRes(pattern: string, handler: StreamingHandler) {
109+
interface Handlers {
110+
onData: StreamingHandler
111+
onError: (e: Error) => void
112+
}
113+
114+
function getPatternRes(pattern: string, handlers: Handlers) {
110115
if (!pattern) {
111116
return
112117
}
@@ -119,7 +124,11 @@ async function getPatternRes(pattern: string, handler: StreamingHandler) {
119124
let proc = spawn(command, ['run', '--pattern', pattern, '--json=stream'], {
120125
cwd: uris[0]
121126
})
122-
return uniqueCommand(proc, handler)
127+
proc.on('error', error => {
128+
console.debug('ast-grep CLI runs error')
129+
handlers.onError(error)
130+
})
131+
return uniqueCommand(proc, handlers.onData)
123132
}
124133

125134
function openFile({
@@ -170,11 +179,17 @@ function setupParentPort(webviewView: vscode.WebviewView) {
170179
})
171180

172181
parentPort.onMessage('search', async payload => {
173-
await getPatternRes(payload.inputValue, ret => {
182+
const onData = (ret: SgSearch[]) => {
174183
parentPort.postMessage('searchResultStreaming', {
175184
...payload,
176185
searchResult: ret.map(splitByHighLightToken)
177186
})
187+
}
188+
await getPatternRes(payload.inputValue, {
189+
onData,
190+
onError(e) {
191+
parentPort.postMessage('error', e)
192+
}
178193
})
179194
parentPort.postMessage('searchEnd', payload)
180195
})

0 commit comments

Comments
 (0)