Don't use JSON.stringify for errors (#19)

* better error handling

* remove unneeded catch

* Update index.ts
This commit is contained in:
Thomas Boop
2022-06-22 09:20:48 -04:00
committed by GitHub
parent 5c6995dba1
commit 4307828719
5 changed files with 27 additions and 31 deletions

View File

@@ -49,7 +49,7 @@ export async function prepareJob(
createdPod = await createPod(container, services, args.registry) createdPod = await createPod(container, services, args.registry)
} catch (err) { } catch (err) {
await prunePods() await prunePods()
throw new Error(`failed to create job pod: ${JSON.stringify(err)}`) throw new Error(`failed to create job pod: ${err}`)
} }
if (!createdPod?.metadata?.name) { if (!createdPod?.metadata?.name) {

View File

@@ -28,7 +28,7 @@ export async function runScriptStep(
JOB_CONTAINER_NAME JOB_CONTAINER_NAME
) )
} catch (err) { } catch (err) {
throw new Error(`failed to run script step: ${JSON.stringify(err)}`) throw new Error(`failed to run script step: ${err}`)
} finally { } finally {
fs.rmSync(runnerPath) fs.rmSync(runnerPath)
} }

View File

@@ -22,7 +22,7 @@ async function run(): Promise<void> {
throw new Error( throw new Error(
`The Service account needs the following permissions ${JSON.stringify( `The Service account needs the following permissions ${JSON.stringify(
requiredPermissions requiredPermissions
)} on the pod resource in the '${namespace}' namespace. Please contact your self hosted runner administrator.` )} on the pod resource in the '${namespace()}' namespace. Please contact your self hosted runner administrator.`
) )
} }
switch (command) { switch (command) {

View File

@@ -185,33 +185,29 @@ export async function execPodStep(
): Promise<void> { ): Promise<void> {
const exec = new k8s.Exec(kc) const exec = new k8s.Exec(kc)
await new Promise(async function (resolve, reject) { await new Promise(async function (resolve, reject) {
try { await exec.exec(
await exec.exec( namespace(),
namespace(), podName,
podName, containerName,
containerName, command,
command, process.stdout,
process.stdout, process.stderr,
process.stderr, stdin ?? null,
stdin ?? null, false /* tty */,
false /* tty */, resp => {
resp => { // kube.exec returns an error if exit code is not 0, but we can't actually get the exit code
// kube.exec returns an error if exit code is not 0, but we can't actually get the exit code if (resp.status === 'Success') {
if (resp.status === 'Success') { resolve(resp.code)
resolve(resp.code) } else {
} else { reject(
reject( JSON.stringify({
JSON.stringify({ message: resp?.message,
message: resp?.message, details: resp?.details
details: resp?.details })
}) )
)
}
} }
) }
} catch (error) { )
reject(JSON.stringify(error))
}
}) })
} }
@@ -372,7 +368,7 @@ export async function getPodLogs(
}) })
logStream.on('error', err => { logStream.on('error', err => {
process.stderr.write(JSON.stringify(err)) process.stderr.write(err.message)
}) })
const r = await log.log(namespace(), podName, containerName, logStream, { const r = await log.log(namespace(), podName, containerName, logStream, {

View File

@@ -40,7 +40,7 @@ export class TestHelper {
await this.createTestVolume() await this.createTestVolume()
await this.createTestJobPod() await this.createTestJobPod()
} catch (e) { } catch (e) {
console.log(JSON.stringify(e)) console.log(e)
} }
} }