From 4307828719ac24f4ae307f9eba1387a65b34becf Mon Sep 17 00:00:00 2001 From: Thomas Boop <52323235+thboop@users.noreply.github.com> Date: Wed, 22 Jun 2022 09:20:48 -0400 Subject: [PATCH] Don't use JSON.stringify for errors (#19) * better error handling * remove unneeded catch * Update index.ts --- packages/k8s/src/hooks/prepare-job.ts | 2 +- packages/k8s/src/hooks/run-script-step.ts | 2 +- packages/k8s/src/index.ts | 2 +- packages/k8s/src/k8s/index.ts | 50 +++++++++++------------ packages/k8s/tests/test-setup.ts | 2 +- 5 files changed, 27 insertions(+), 31 deletions(-) diff --git a/packages/k8s/src/hooks/prepare-job.ts b/packages/k8s/src/hooks/prepare-job.ts index 96c3bc5..12fc1b8 100644 --- a/packages/k8s/src/hooks/prepare-job.ts +++ b/packages/k8s/src/hooks/prepare-job.ts @@ -49,7 +49,7 @@ export async function prepareJob( createdPod = await createPod(container, services, args.registry) } catch (err) { 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) { diff --git a/packages/k8s/src/hooks/run-script-step.ts b/packages/k8s/src/hooks/run-script-step.ts index 64ff1d7..9cae84b 100644 --- a/packages/k8s/src/hooks/run-script-step.ts +++ b/packages/k8s/src/hooks/run-script-step.ts @@ -28,7 +28,7 @@ export async function runScriptStep( JOB_CONTAINER_NAME ) } catch (err) { - throw new Error(`failed to run script step: ${JSON.stringify(err)}`) + throw new Error(`failed to run script step: ${err}`) } finally { fs.rmSync(runnerPath) } diff --git a/packages/k8s/src/index.ts b/packages/k8s/src/index.ts index c41cbf0..d58167b 100644 --- a/packages/k8s/src/index.ts +++ b/packages/k8s/src/index.ts @@ -22,7 +22,7 @@ async function run(): Promise { throw new Error( `The Service account needs the following permissions ${JSON.stringify( 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) { diff --git a/packages/k8s/src/k8s/index.ts b/packages/k8s/src/k8s/index.ts index 05d3c47..b57217a 100644 --- a/packages/k8s/src/k8s/index.ts +++ b/packages/k8s/src/k8s/index.ts @@ -185,33 +185,29 @@ export async function execPodStep( ): Promise { const exec = new k8s.Exec(kc) await new Promise(async function (resolve, reject) { - try { - await exec.exec( - namespace(), - podName, - containerName, - command, - process.stdout, - process.stderr, - stdin ?? null, - false /* tty */, - resp => { - // kube.exec returns an error if exit code is not 0, but we can't actually get the exit code - if (resp.status === 'Success') { - resolve(resp.code) - } else { - reject( - JSON.stringify({ - message: resp?.message, - details: resp?.details - }) - ) - } + await exec.exec( + namespace(), + podName, + containerName, + command, + process.stdout, + process.stderr, + stdin ?? null, + false /* tty */, + resp => { + // kube.exec returns an error if exit code is not 0, but we can't actually get the exit code + if (resp.status === 'Success') { + resolve(resp.code) + } else { + reject( + JSON.stringify({ + message: resp?.message, + details: resp?.details + }) + ) } - ) - } catch (error) { - reject(JSON.stringify(error)) - } + } + ) }) } @@ -372,7 +368,7 @@ export async function getPodLogs( }) logStream.on('error', err => { - process.stderr.write(JSON.stringify(err)) + process.stderr.write(err.message) }) const r = await log.log(namespace(), podName, containerName, logStream, { diff --git a/packages/k8s/tests/test-setup.ts b/packages/k8s/tests/test-setup.ts index 88eba61..25efcf5 100644 --- a/packages/k8s/tests/test-setup.ts +++ b/packages/k8s/tests/test-setup.ts @@ -40,7 +40,7 @@ export class TestHelper { await this.createTestVolume() await this.createTestJobPod() } catch (e) { - console.log(JSON.stringify(e)) + console.log(e) } }