Skip to content

Commit 89e6d60

Browse files
refactor: log all thrown errors
1 parent a610e0f commit 89e6d60

File tree

8 files changed

+51
-38
lines changed

8 files changed

+51
-38
lines changed

src/ci_providers/provider_local.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const childprocess = require('child_process')
22
const { parseSlug } = require('../helpers/git')
3+
const { logAndThrow } = require('../helpers/util')
34

45
function detect(envs) {
56
return !envs.CI
@@ -24,9 +25,7 @@ function _getBranch(inputs) {
2425
.trimRight()
2526
return args.branch || branchName
2627
} catch (error) {
27-
throw new Error(
28-
`There was an error getting the branch name from git: ${error}`,
29-
)
28+
logAndThrow(`There was an error getting the branch name from git: ${error}`)
3029
}
3130
}
3231

@@ -60,9 +59,7 @@ function _getSHA(inputs) {
6059
.trimRight()
6160
return args.sha || sha
6261
} catch (error) {
63-
throw new Error(
64-
`There was an error getting the commit SHA from git: ${error}`,
65-
)
62+
logAndThrow(`There was an error getting the commit SHA from git: ${error}`)
6663
}
6764
}
6865

@@ -75,7 +72,7 @@ function _getSlug(inputs) {
7572
.trimRight()
7673
return args.slug || parseSlug(slug)
7774
} catch (error) {
78-
throw new Error(`There was an error getting the slug from git: ${error}`)
75+
logAndThrow(`There was an error getting the slug from git: ${error}`)
7976
}
8077
}
8178

src/ci_providers/provider_template.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
* @param {*} envs an object of enviromental variable key/value pairs
55
* @returns boolean
66
*/
7+
8+
const { logAndThrow } = require('../helpers/util')
9+
710
// eslint-disable-next-line no-unused-vars
811
function detect(envs) {
912
return false
@@ -42,9 +45,7 @@ function _getBranch(inputs) {
4245
try {
4346
return args.branch || ''
4447
} catch (error) {
45-
throw new Error(
46-
`There was an error getting the branch name from git: ${error}`,
47-
)
48+
logAndThrow(`There was an error getting the branch name from git: ${error}`)
4849
}
4950
}
5051

@@ -71,9 +72,7 @@ function _getPR(inputs) {
7172
try {
7273
return args.pr || ''
7374
} catch (error) {
74-
throw new Error(
75-
`There was an error getting the branch name from git: ${error}`,
76-
)
75+
logAndThrow(`There was an error getting the pr number: ${error}`)
7776
}
7877
}
7978

@@ -105,9 +104,7 @@ function _getSHA(inputs) {
105104
try {
106105
return args.sha || ''
107106
} catch (error) {
108-
throw new Error(
109-
`There was an error getting the commit SHA from git: ${error}`,
110-
)
107+
logAndThrow(`There was an error getting the commit SHA from git: ${error}`)
111108
}
112109
}
113110
/**
@@ -121,7 +118,7 @@ function _getSlug(inputs) {
121118
try {
122119
return args.slug || ''
123120
} catch (error) {
124-
throw new Error(`There was an error getting the slug from git: ${error}`)
121+
logAndThrow(`There was an error getting the slug from git: ${error}`)
125122
}
126123
}
127124
/**

src/helpers/files.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const childProcess = require('child_process')
33
const fs = require('fs')
44
const glob = require('glob')
55
const path = require('path').posix
6-
6+
const { logAndThrow } = require('./util')
77
const { error, info, verbose } = require('./logger')
88

99
/**
@@ -195,7 +195,7 @@ function fetchGitRoot() {
195195
process.cwd()
196196
).trimRight()
197197
} catch (error) {
198-
throw new Error('Error fetching git root. Please try using the -R flag.')
198+
logAndThrow('Error fetching git root. Please try using the -R flag.')
199199
}
200200
}
201201

@@ -210,7 +210,7 @@ function parseGitIgnore(projectRoot) {
210210
try {
211211
lines = readAllLines(gitIgnorePath) || []
212212
} catch (error) {
213-
throw new Error(`Unable to open ${gitIgnorePath}: ${error}`)
213+
logAndThrow(`Unable to open ${gitIgnorePath}: ${error}`)
214214
}
215215

216216
return lines.filter(line => {
@@ -278,7 +278,7 @@ function readCoverageFile(projectRoot, filePath) {
278278
encoding: 'utf-8',
279279
})
280280
} catch (error) {
281-
throw new Error(`There was an error reading the coverage file: ${error}`)
281+
logAndThrow(`There was an error reading the coverage file: ${error}`)
282282
}
283283
}
284284

src/helpers/git.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const childProcess = require('child_process')
2+
const { logAndThrow } = require('./util')
23

34
function parseSlug(slug) {
45
// origin https://github.com/torvalds/linux.git (fetch)
@@ -18,18 +19,16 @@ function parseSlug(slug) {
1819
const cleanSlug = slug.split(':')[1].replace('.git', '')
1920
return cleanSlug
2021
}
21-
throw new Error(`Unable to parse slug URL: ${slug}`)
22+
logAndThrow(`Unable to parse slug URL: ${slug}`)
2223
}
2324

2425
function parseSlugFromRemoteAddr(remoteAddr) {
2526
let slug = ''
2627
if (!remoteAddr) {
27-
remoteAddr = (childProcess
28-
.spawnSync('git', [
29-
'config',
30-
'--get',
31-
'remote.origin.url',
32-
]).stdout || '')
28+
remoteAddr = (
29+
childProcess.spawnSync('git', ['config', '--get', 'remote.origin.url'])
30+
.stdout || ''
31+
)
3332
.toString()
3433
.trimRight()
3534
}

src/helpers/util.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const { error } = require('./logger')
2+
3+
/**
4+
* Log the error and throw it
5+
* @param {string} message
6+
* @throws Error
7+
*/
8+
function logAndThrow(message) {
9+
error(message)
10+
throw new Error(message)
11+
}
12+
13+
module.exports = {
14+
logAndThrow,
15+
}

src/helpers/web.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const superagent = require('superagent')
22
const { version } = require('../../package.json')
33
const validateHelpers = require('./validate')
44
const { error, info, verbose } = require('./logger')
5+
const { logAndThrow } = require('./util')
56

67
/**
78
*
@@ -59,9 +60,11 @@ async function uploadToCodecovPUT(uploadURL, uploadFile) {
5960
if (result.status === 200) {
6061
return { status: 'success', resultURL: codecovResultURL }
6162
}
62-
throw new Error(`Error uploading: ${result.status}, ${result.body}`)
63+
logAndThrow(
64+
`Error uploading during PUT (inner): ${result.status}, ${result.body}`,
65+
)
6366
} catch (error) {
64-
throw new Error(`Error uploading: ${error}`)
67+
logAndThrow(`Error uploading during PUT (outer): ${error}`)
6568
}
6669
}
6770

@@ -91,7 +94,7 @@ async function uploadToCodecov(uploadURL, token, query, uploadFile, source) {
9194

9295
return result.res.text
9396
} catch (error) {
94-
throw new Error(`Error uploading to Codecov: ${error}`)
97+
logAndThrow(`Error uploading to Codecov when fetching PUT: ${error}`)
9598
}
9699
}
97100

src/index.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const tokenHelpers = require('./helpers/token')
77
const webHelpers = require('./helpers/web')
88
const { error, info, verbose } = require('./helpers/logger')
99
const providers = require('./ci_providers')
10+
const { logAndThrow } = require('./helpers/util')
1011

1112
/**
1213
*
@@ -96,7 +97,7 @@ async function main(args) {
9697
try {
9798
fileListing = await fileHelpers.getFileListing(projectRoot, args)
9899
} catch (error) {
99-
throw new Error(`Error getting file listing: ${error}`)
100+
logAndThrow(`Error getting file listing: ${error}`)
100101
}
101102

102103
uploadFile = uploadFile
@@ -119,7 +120,7 @@ async function main(args) {
119120
info(`=> Found ${coverageFilePaths.length} possible coverage files:`)
120121
info(coverageFilePaths.join('\n'))
121122
} else {
122-
throw new Error(
123+
logAndThrow(
123124
'No coverage files located, please try use `-f`, or change the project root with `-R`',
124125
)
125126
}
@@ -134,7 +135,7 @@ async function main(args) {
134135
return validateHelpers.validateFileNamePath(file)
135136
})
136137
if (coverageFilePaths.length === 0) {
137-
throw new Error('No coverage files found, exiting.')
138+
logAndThrow('No coverage files found, exiting.')
138139
}
139140
}
140141
verbose('End of network processing', args.verbose)
@@ -197,7 +198,7 @@ async function main(args) {
197198
}
198199

199200
if (serviceParams === undefined) {
200-
throw new Error('Unable to detect service, please specify manually.')
201+
logAndThrow('Unable to detect service, please specify manually.')
201202
}
202203

203204
// == Step 8: either upload or dry-run
@@ -247,7 +248,7 @@ async function main(args) {
247248
info(JSON.stringify(result))
248249
return result
249250
} catch (error) {
250-
throw new Error(`Error uploading to ${uploadHost}: ${error}`)
251+
logAndThrow(`Error uploading to ${uploadHost}: ${error}`)
251252
}
252253
}
253254

test/test_helpers.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const td = require('testdouble')
22
const childProcess = require('child_process')
33
const { beforeAll, afterEach, afterAll, expect } = require('@jest/globals')
4+
const { logAndThrow } = require('../src/helpers/util')
45

56
// eslint-disable-next-line no-undef
67
require('testdouble-jest')(td, jest)
@@ -10,12 +11,12 @@ let exec
1011

1112
beforeAll(() => {
1213
execSync = jest.spyOn(childProcess, 'execSync').mockImplementation(() => {
13-
throw new Error(
14+
logAndThrow(
1415
`Security alert! Do not use execSync(), use spawnSync() instead`,
1516
)
1617
})
1718
exec = jest.spyOn(childProcess, 'exec').mockImplementation(() => {
18-
throw new Error(`Security alert! Do not use exec(), use spawn() instead`)
19+
logAndThrow(`Security alert! Do not use exec(), use spawn() instead`)
1920
})
2021
})
2122

0 commit comments

Comments
 (0)