Skip to content

Commit

Permalink
Add API client methods for error reporting and failure
Browse files Browse the repository at this point in the history
  • Loading branch information
Barry Gordon committed Aug 25, 2021
1 parent ec342a7 commit 34e6533
Showing 1 changed file with 52 additions and 2 deletions.
54 changes: 52 additions & 2 deletions src/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export class JobParameters {
) {}
}

// TODO: Populate with enabled values
// TODO: Rescue unsupported values
export enum PackageManager {
NpmAndYarn = 'npm_and_yarn'
}
Expand All @@ -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
Expand Down Expand Up @@ -50,8 +61,8 @@ export class APIClient {
}

async getCredentials(): Promise<Credential[]> {
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) {
Expand All @@ -60,4 +71,43 @@ export class APIClient {

return res.data.data.attributes.credentials
}

async reportJobError(error: JobError): Promise<void> {
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<void> {
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<void> {
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
}
}
}

0 comments on commit 34e6533

Please sign in to comment.