Skip to content

Commit

Permalink
Further improves error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
jeramysoucy committed Dec 27, 2023
1 parent af7fdc5 commit acd1063
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
5 changes: 3 additions & 2 deletions lib/github/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const chalk = require('chalk')
const getBatchIssue = require('./batch')
const { conf } = require('../config')
const { getProjectName, getGraph } = require('./utils')
const { dressError } = require('../utils')

const { getLabels, ensureLabelsAreCreated } = require('./labels')

Expand All @@ -32,7 +33,7 @@ module.exports = {
])
)
} catch (err) {
throw new Error(`Failed to paginate octakit request for existing issues in repository ${conf.ghRepo}, error: ${err.message}`, { cause: err })
throw new Error(dressError(err, `Failed to paginate octakit request for existing issues in repository ${conf.ghRepo}`))
}
},

Expand All @@ -41,7 +42,7 @@ module.exports = {
try {
return await this.client.issues.create(options)
} catch (err) {
throw new Error(`Failed to ${options.issue_number ? 'update' : 'create'} issue: ${options.issue_number ? options.issue_number : options.title}, error: ${err.message}`, { cause: err })
throw new Error(dressError(err, `Failed to ${options.issue_number ? 'update' : 'create'} issue: ${options.issue_number ? options.issue_number : options.title}`))
}
},

Expand Down
6 changes: 3 additions & 3 deletions lib/github/labels.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const { conf } = require('../config')
const { uniq } = require('../utils')
const { uniq, dressError } = require('../utils')

const LABELS = {
snyk: {
Expand Down Expand Up @@ -62,7 +62,7 @@ const ensureLabelsAreCreated = async (octokit, client, ghOwner, ghRepo, issues)
(response) => response.data
)
} catch (err) {
throw new Error(`Failed to paginate octakit request for labels in repository ${ghRepo}, error: ${err.message}`, { cause: err })
throw new Error(dressError(err, `Failed to paginate octakit request for labels in repository: ${ghRepo}`))
}

const currentLabels = responseData.map((x) => x.name)
Expand All @@ -84,7 +84,7 @@ const ensureLabelsAreCreated = async (octokit, client, ghOwner, ghRepo, issues)
console.log(`Created GitHub label: "${name}"`)
})
} catch (err) {
throw new Error(`Failed to create GitHub label '${name}', error: ${err.message}`, { cause: err })
throw new Error(dressError(err, `Failed to create GitHub label '${name}' in repository: ${ghRepo}`))
}
})
)
Expand Down
5 changes: 3 additions & 2 deletions lib/snyk.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const request = require('request-promise-native')
const { dressError } = require('./utils')

const baseV1Url = 'https://snyk.io/api/v1'
const baseRestUrl = 'https://api.snyk.io/rest'
Expand Down Expand Up @@ -44,7 +45,7 @@ module.exports = class Snyk {

return response.data
} catch (err) {
throw new Error(`Failed to query snyk organization with id ${organizationId}, error: ${err.message}`, { cause: err })
throw new Error(dressError(err, `Failed to query snyk organization with id ${organizationId}`))
}
}

Expand Down Expand Up @@ -138,6 +139,6 @@ async function paginateRestResponseData (url, headers, method = 'get') {

return reponseData
} catch (err) {
throw new Error(`Failed to paginate request for ${method} ${url}, error: ${err.message}`, { cause: err })
throw new Error(dressError(err, `Failed to paginate request for ${method} ${url}`))
}
}
7 changes: 6 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ const capitalize = (s) => {

const uniq = (array) => [...new Set(array)]

const dressError = (err, msg) => {
return `${msg}, error: ${err.status ? err.status + ' - ' : ''}${err.message}, response: ${err.response}`
}

module.exports = {
capitalize,
compare: {
Expand All @@ -82,5 +86,6 @@ module.exports = {
versionArrays: compareVersionArrays,
arrays: compareArrays
},
uniq
uniq,
dressError
}

0 comments on commit acd1063

Please sign in to comment.