Skip to content

Commit

Permalink
Fix error detail returned to the API
Browse files Browse the repository at this point in the history
  • Loading branch information
Barry Gordon committed Sep 30, 2021
1 parent c0d11c5 commit 3b9b586
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 26 deletions.
16 changes: 12 additions & 4 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ describe('run', () => {

expect(reportJobErrorSpy).toHaveBeenCalledWith({
'error-type': 'actions_workflow_unknown',
'error-detail': 'error getting job details'
'error-details': {
'action-error': 'error getting job details'
}
})
expect(markJobAsProcessedSpy).toHaveBeenCalled()
})
Expand Down Expand Up @@ -173,7 +175,9 @@ describe('run', () => {

expect(reportJobErrorSpy).toHaveBeenCalledWith({
'error-type': 'actions_workflow_unknown',
'error-detail': 'error getting credentials'
'error-details': {
'action-error': 'error getting credentials'
}
})
expect(markJobAsProcessedSpy).toHaveBeenCalled()
})
Expand Down Expand Up @@ -207,7 +211,9 @@ describe('run', () => {

expect(reportJobErrorSpy).toHaveBeenCalledWith({
'error-type': 'actions_workflow_image',
'error-detail': 'error pulling an image'
'error-details': {
'action-error': 'error pulling an image'
}
})
expect(markJobAsProcessedSpy).toHaveBeenCalled()
})
Expand Down Expand Up @@ -241,7 +247,9 @@ describe('run', () => {

expect(reportJobErrorSpy).toHaveBeenCalledWith({
'error-type': 'actions_workflow_updater',
'error-detail': 'error running the update'
'error-details': {
'action-error': 'error running the update'
}
})
expect(markJobAsProcessedSpy).toHaveBeenCalled()
})
Expand Down
19 changes: 12 additions & 7 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

Empty file removed output/.gitkeep
Empty file.
38 changes: 25 additions & 13 deletions src/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export type JobDetails = {

export type JobError = {
'error-type': string
'error-detail': any
'error-details': {
'action-error': string
}
}

export type Credential = {
Expand All @@ -39,6 +41,12 @@ export class ApiClient {
readonly params: JobParameters
) {}

// We use a static unknown SHA when making a job as complete from the action
// to remain in parity with the existing runner.
UnknownSha = {
'base-commit-sha': 'unknown'
}

async getJobDetails(): Promise<JobDetails> {
const detailsURL = `/update_jobs/${this.params.jobId}/details`
const res = await this.client.get(detailsURL, {
Expand All @@ -65,25 +73,29 @@ export class ApiClient {

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) {
const res = await this.client.post(
recordErrorURL,
{data: error},
{
headers: {Authorization: this.params.jobToken}
}
)
if (res.status !== 204) {
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.jobToken}
})
if (res.status !== 200) {
const res = await this.client.patch(
markAsProcessedURL,
{data: this.UnknownSha},
{
headers: {Authorization: this.params.jobToken}
}
)
if (res.status !== 204) {
throw new Error(`Unexpected status code: ${res.status}`)
}

return res.data.data.attributes
}
}
4 changes: 3 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ async function failJob(
): Promise<void> {
await apiClient.reportJobError({
'error-type': errorType,
'error-detail': error.message
'error-details': {
'action-error': error.message
}
})
await apiClient.markJobAsProcessed()
core.setFailed(error.message)
Expand Down

0 comments on commit 3b9b586

Please sign in to comment.