diff --git a/lib/github/index.js b/lib/github/index.js index 5a39efd..a260a43 100644 --- a/lib/github/index.js +++ b/lib/github/index.js @@ -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') @@ -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}`)) } }, @@ -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}`)) } }, diff --git a/lib/github/labels.js b/lib/github/labels.js index c73564d..b4e6272 100644 --- a/lib/github/labels.js +++ b/lib/github/labels.js @@ -1,7 +1,7 @@ 'use strict' const { conf } = require('../config') -const { uniq } = require('../utils') +const { uniq, dressError } = require('../utils') const LABELS = { snyk: { @@ -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) @@ -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}`)) } }) ) diff --git a/lib/snyk.js b/lib/snyk.js index 991467c..9f2adff 100644 --- a/lib/snyk.js +++ b/lib/snyk.js @@ -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' @@ -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}`)) } } @@ -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}`)) } } diff --git a/lib/utils.js b/lib/utils.js index c245224..fdb2c10 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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: { @@ -82,5 +86,6 @@ module.exports = { versionArrays: compareVersionArrays, arrays: compareArrays }, - uniq + uniq, + dressError }