1
0
Fork 0

better error capturing for all spawned commands

pull/1935/head
Prajjwal 2024-05-17 17:16:08 +05:30
parent 890c6b520f
commit c597cfaa61
1 changed files with 43 additions and 14 deletions

View File

@ -441,21 +441,47 @@ export async function extractStreamingTar(
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (stream) { const handleStreamError = (
stream.pipe(commandPipes[0].stdin) stream: NodeJS.ReadableStream | NodeJS.WritableStream,
commandName: string
) => {
stream.on('error', error => {
reject(new Error(`Error in ${commandName}: ${error.message}`))
})
} }
for (let i = 0; i < commandPipes.length - 1; i++) {
commandPipes[i].stdout.pipe(commandPipes[i + 1].stdin)
commandPipes[i].stderr.on('data', data => { // Attach error handlers and pipe the streams
commandPipes.forEach(commandPipe => {
handleStreamError(commandPipe.stdin, commandPipe.spawnfile)
handleStreamError(commandPipe.stdout, commandPipe.spawnfile)
handleStreamError(commandPipe.stderr, commandPipe.spawnfile)
commandPipe.stderr.on('data', data => {
reject( reject(
new Error(`Error in ${commandPipes[i].spawnfile}: ${data.toString()}`) new Error(`Error in ${commandPipe.spawnfile}: ${data.toString()}`)
) )
}) })
})
commandPipes[i].on('error', error => { if (stream) {
stream.pipe(commandPipes[0].stdin).on('error', error => {
reject( reject(
new Error(`Error in ${commandPipes[i].spawnfile}: ${error.message}`) new Error(
`Error piping to ${commandPipes[0].spawnfile}: ${error.message}`
)
)
})
}
for (let i = 0; i < commandPipes.length - 1; i++) {
commandPipes[i].stdout
.pipe(commandPipes[i + 1].stdin)
.on('error', error => {
reject(
new Error(
`Error piping between ${commandPipes[i].spawnfile} and ${
commandPipes[i + 1].spawnfile
}: ${error.message}`
)
) )
}) })
} }
@ -472,6 +498,9 @@ export async function extractStreamingTar(
reject(new Error(`Last command exited with code ${code}`)) reject(new Error(`Last command exited with code ${code}`))
} }
}) })
lastCommand.on('error', error => {
reject(new Error(`Error in ${lastCommand.spawnfile}: ${error.message}`))
})
}) })
} }