From 34e6533e70e1a1b34f8241d60ce1f7924afdddbc Mon Sep 17 00:00:00 2001 From: Barry Gordon Date: Tue, 24 Aug 2021 11:33:42 +0100 Subject: [PATCH] Add API client methods for error reporting and failure --- src/api-client.ts | 54 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/src/api-client.ts b/src/api-client.ts index f09c900..fdcdf07 100644 --- a/src/api-client.ts +++ b/src/api-client.ts @@ -10,6 +10,8 @@ export class JobParameters { ) {} } +// TODO: Populate with enabled values +// TODO: Rescue unsupported values export enum PackageManager { NpmAndYarn = 'npm_and_yarn' } @@ -23,6 +25,15 @@ export type JobDetails = { 'package-manager': PackageManager } +export enum JobErrorType { + Unknown = 'actions_runner_unknown' +} + +export type JobError = { + 'error-type': JobErrorType + 'error-detail': any +} + export type Credential = { type: string host: string @@ -50,8 +61,8 @@ export class APIClient { } async getCredentials(): Promise { - const detailsURL = `/update_jobs/${this.params.jobID}/credentials` - const res = await this.client.get(detailsURL, { + const credentialsURL = `/update_jobs/${this.params.jobID}/credentials` + const res = await this.client.get(credentialsURL, { headers: {Authorization: this.params.credentialsToken} }) if (res.status !== 200) { @@ -60,4 +71,43 @@ export class APIClient { return res.data.data.attributes.credentials } + + async reportJobError(error: JobError): Promise { + const recordErrorURL = `/update_jobs/${this.params.jobID}/record_update_job_error` + const res = await this.client.post(recordErrorURL, error, { + headers: {Authorization: this.params.jobToken} + }) + if (res.status !== 200) { + throw new Error(`Unexpected status code: ${res.status}`) + } + + return res.data.data.attributes + } + + async markJobAsProcessed(): Promise { + const markAsProcessedURL = `/update_jobs/${this.params.jobID}/mark_as_processed` + const res = await this.client.get(markAsProcessedURL, { + headers: {Authorization: this.params.credentialsToken} + }) + if (res.status !== 200) { + throw new Error(`Unexpected status code: ${res.status}`) + } + + return res.data.data.attributes + } + + async failJob(error: Error): Promise { + const jobError = this.jobErrorFor(error) + await this.reportJobError(jobError) + await this.markJobAsProcessed() + } + + private jobErrorFor(error: Error): JobError { + const errorType = JobErrorType.Unknown + + return { + 'error-type': errorType, + 'error-detail': error.message + } + } }