Skip to content

Commit 4307828

Browse files
authored
Don't use JSON.stringify for errors (#19)
* better error handling * remove unneeded catch * Update index.ts
1 parent 5c6995d commit 4307828

File tree

5 files changed

+27
-31
lines changed

5 files changed

+27
-31
lines changed

packages/k8s/src/hooks/prepare-job.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export async function prepareJob(
4949
createdPod = await createPod(container, services, args.registry)
5050
} catch (err) {
5151
await prunePods()
52-
throw new Error(`failed to create job pod: ${JSON.stringify(err)}`)
52+
throw new Error(`failed to create job pod: ${err}`)
5353
}
5454

5555
if (!createdPod?.metadata?.name) {

packages/k8s/src/hooks/run-script-step.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export async function runScriptStep(
2828
JOB_CONTAINER_NAME
2929
)
3030
} catch (err) {
31-
throw new Error(`failed to run script step: ${JSON.stringify(err)}`)
31+
throw new Error(`failed to run script step: ${err}`)
3232
} finally {
3333
fs.rmSync(runnerPath)
3434
}

packages/k8s/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ async function run(): Promise<void> {
2222
throw new Error(
2323
`The Service account needs the following permissions ${JSON.stringify(
2424
requiredPermissions
25-
)} on the pod resource in the '${namespace}' namespace. Please contact your self hosted runner administrator.`
25+
)} on the pod resource in the '${namespace()}' namespace. Please contact your self hosted runner administrator.`
2626
)
2727
}
2828
switch (command) {

packages/k8s/src/k8s/index.ts

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -185,33 +185,29 @@ export async function execPodStep(
185185
): Promise<void> {
186186
const exec = new k8s.Exec(kc)
187187
await new Promise(async function (resolve, reject) {
188-
try {
189-
await exec.exec(
190-
namespace(),
191-
podName,
192-
containerName,
193-
command,
194-
process.stdout,
195-
process.stderr,
196-
stdin ?? null,
197-
false /* tty */,
198-
resp => {
199-
// kube.exec returns an error if exit code is not 0, but we can't actually get the exit code
200-
if (resp.status === 'Success') {
201-
resolve(resp.code)
202-
} else {
203-
reject(
204-
JSON.stringify({
205-
message: resp?.message,
206-
details: resp?.details
207-
})
208-
)
209-
}
188+
await exec.exec(
189+
namespace(),
190+
podName,
191+
containerName,
192+
command,
193+
process.stdout,
194+
process.stderr,
195+
stdin ?? null,
196+
false /* tty */,
197+
resp => {
198+
// kube.exec returns an error if exit code is not 0, but we can't actually get the exit code
199+
if (resp.status === 'Success') {
200+
resolve(resp.code)
201+
} else {
202+
reject(
203+
JSON.stringify({
204+
message: resp?.message,
205+
details: resp?.details
206+
})
207+
)
210208
}
211-
)
212-
} catch (error) {
213-
reject(JSON.stringify(error))
214-
}
209+
}
210+
)
215211
})
216212
}
217213

@@ -372,7 +368,7 @@ export async function getPodLogs(
372368
})
373369

374370
logStream.on('error', err => {
375-
process.stderr.write(JSON.stringify(err))
371+
process.stderr.write(err.message)
376372
})
377373

378374
const r = await log.log(namespace(), podName, containerName, logStream, {

packages/k8s/tests/test-setup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export class TestHelper {
4040
await this.createTestVolume()
4141
await this.createTestJobPod()
4242
} catch (e) {
43-
console.log(JSON.stringify(e))
43+
console.log(e)
4444
}
4545
}
4646

0 commit comments

Comments
 (0)