Skip to content

Commit

Permalink
chore: improved error message
Browse files Browse the repository at this point in the history
  • Loading branch information
subzero10 committed Nov 9, 2023
1 parent a76c985 commit 494c309
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
12 changes: 10 additions & 2 deletions packages/js/examples/checkins-manager/honeybadger.config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
module.exports = {
personalAuthToken: process.env.HONEYBADGER_PERSONAL_AUTH_TOKEN,
personalAuthToken: process.env.HONEYBADGER_PERSONAL_AUTH_TOKEN || 'UrCxXYNnp2s-U4KgqVXv',
checkins: [
{
name: 'Weekly Exports',
slug: 'weekly-exports-custom-slug',
projectId: process.env.HONEYBADGER_PROJECT_ID,
projectId: '68958',
scheduleType: 'simple',
reportPeriod: '1 week',
gracePeriod: '10 minutes'
},
{
name: 'Cron That Should Not Be Here',
slug: 'cron-that-should-not-be-here-custom-slug',
projectId: '68958',
scheduleType: 'cron',
cronSchedule: '* * 5 * *',
gracePeriod: '5 minutes'
}
]
Expand Down
30 changes: 20 additions & 10 deletions packages/js/src/server/checkins-manager/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ export class CheckinsClient {
})

if (response.statusCode !== 200) {
this.logger.debug(`Failed to fetch checkins for project[${projectId}]: ${response.body}`)
throw new Error(`Failed to fetch checkins for project[${projectId}]`,)
throw new Error(`Failed to fetch checkins for project[${projectId}]: ${this.getErrorMessage(response.body)}`,)
}

const data: { results: CheckinResponsePayload[] } = JSON.parse(response.body)
Expand All @@ -57,8 +56,7 @@ export class CheckinsClient {
})

if (response.statusCode !== 200) {
this.logger.debug(`Failed to fetch checkin[${checkinId}] for project[${projectId}]: ${response.body}`)
throw new Error(`Failed to fetch checkin[${checkinId}] for project[${projectId}]`)
throw new Error(`Failed to fetch checkin[${checkinId}] for project[${projectId}]: ${this.getErrorMessage(response.body)}`)
}

const data: CheckinResponsePayload = JSON.parse(response.body)
Expand All @@ -81,8 +79,7 @@ export class CheckinsClient {
}, { check_in: checkin.asRequestPayload() })

if (response.statusCode !== 201) {
this.logger.debug(`Failed to create checkin[${checkin.name}] for project[${checkin.projectId}]: ${response.body}`)
throw new Error(`Failed to create checkin[${checkin.name}] for project[${checkin.projectId}]`)
throw new Error(`Failed to create checkin[${checkin.name}] for project[${checkin.projectId}]: ${this.getErrorMessage(response.body)}`)
}

const data: CheckinResponsePayload = JSON.parse(response.body)
Expand All @@ -105,8 +102,7 @@ export class CheckinsClient {
}, { check_in: checkin.asRequestPayload() })

if (response.statusCode !== 204) {
this.logger.debug(`Failed to update checkin[${checkin.name}] for project[${checkin.projectId}]: ${response.body}`)
throw new Error(`Failed to update checkin[${checkin.name}] for project[${checkin.projectId}]`)
throw new Error(`Failed to update checkin[${checkin.name}] for project[${checkin.projectId}]: ${this.getErrorMessage(response.body)}`)
}

return checkin
Expand All @@ -125,8 +121,7 @@ export class CheckinsClient {
})

if (response.statusCode !== 204) {
this.logger.debug(`Failed to remove checkin[${checkin.name}] for project[${checkin.projectId}]: ${response.body}`)
throw new Error(`Failed to remove checkin[${checkin.name}] for project[${checkin.projectId}]`)
throw new Error(`Failed to remove checkin[${checkin.name}] for project[${checkin.projectId}]: ${this.getErrorMessage(response.body)}`)
}
}

Expand All @@ -137,4 +132,19 @@ export class CheckinsClient {
}
}

private getErrorMessage(responseBody: string) {
if (!responseBody) {
return ''
}

try {
const jsonBody: { errors: string } = JSON.parse(responseBody)

return jsonBody.errors ?? ''
}
catch (e) {
return responseBody
}
}

}

0 comments on commit 494c309

Please sign in to comment.