Skip to content

Commit 9a15406

Browse files
committed
🥅 Handle functional errors
1 parent 4e22239 commit 9a15406

4 files changed

Lines changed: 69 additions & 34 deletions

File tree

packages/gitmoji-changelog-cli/src/cli.e2e.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ expect.extend({
1818
})
1919

2020
expect.extend({
21-
toDisplayError(str) {
22-
const pass = str.includes('Error')
21+
toDisplayError(str, message = '') {
22+
const pass = str.includes('Error') && str.includes(message)
2323
return {
2424
pass,
25-
message: () => pass ? 'It passes' : `Expected ${str} to includes Error`,
25+
message: () => pass ? 'It passes' : `Expected ${str} to includes Error and "${message}"`,
2626
}
2727
},
2828
})
@@ -336,7 +336,7 @@ describe('generate changelog', () => {
336336
const output = gitmojiChangelog()
337337

338338
expect(getChangelog()).includes(['1.0.0'])
339-
expect(output.toString('utf8')).toDisplayError()
339+
expect(output.toString('utf8')).toDisplayError('No changes found.')
340340
})
341341

342342
it('should get two versions 1.0.0 and next after two generation while updating changelog by calling cli without arguments', async () => {

packages/gitmoji-changelog-cli/src/cli.js

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -100,26 +100,30 @@ async function main(options = {}) {
100100
}
101101
logger.success(`changelog updated into ${options.output}`)
102102
} catch (e) {
103-
const repository = await getRepositoryInfo()
104-
await issueReporter({
105-
error: e,
106-
user: 'frinyvonnick',
107-
repo: 'gitmoji-changelog',
108-
sections: [
109-
{
110-
title: 'CLI options',
111-
content: options,
112-
},
113-
{
114-
title: 'Project info',
115-
content: projectInfo,
116-
},
117-
{
118-
title: 'Repository info',
119-
content: repository,
120-
},
121-
],
122-
})
103+
if (e.name !== 'FunctionalError') {
104+
const repository = await getRepositoryInfo()
105+
await issueReporter({
106+
error: e,
107+
user: 'frinyvonnick',
108+
repo: 'gitmoji-changelog',
109+
sections: [
110+
{
111+
title: 'CLI options',
112+
content: options,
113+
},
114+
{
115+
title: 'Project info',
116+
content: projectInfo,
117+
},
118+
{
119+
title: 'Repository info',
120+
content: repository,
121+
},
122+
],
123+
})
124+
} else {
125+
logger.error(e)
126+
}
123127
}
124128

125129
// force quit (if the latest version request is pending, we don't wait for it)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class FunctionalError extends Error {
2+
constructor(message) {
3+
super(message)
4+
this.name = 'FunctionalError'
5+
}
6+
}
7+
8+
module.exports = {
9+
FunctionalError,
10+
}

packages/gitmoji-changelog-core/src/index.js

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const { isEmpty } = require('lodash')
66
const { parseCommit, getMergedGroupMapping } = require('./parser')
77
const logger = require('./logger')
88
const { groupSentencesByDistance } = require('./utils')
9+
const { FunctionalError } = require('./errors')
910
const fromGitFileClient = require('./fromGitFile')
1011

1112
const HEAD = ''
@@ -31,10 +32,17 @@ function makeGroups(commits) {
3132
}
3233

3334
function sanitizeVersion(version) {
34-
return semver.valid(version, {
35-
loose: false,
36-
includePrerelease: true,
37-
})
35+
try {
36+
return semver.valid(version, {
37+
loose: false,
38+
includePrerelease: true,
39+
})
40+
} catch (e) {
41+
if (e.name === 'TypeError') {
42+
throw FunctionalError(e.message)
43+
}
44+
return null
45+
}
3846
}
3947

4048
function filterCommits(commits) {
@@ -84,7 +92,14 @@ function sortVersions(c1, c2) {
8492

8593
function hasNextVersion(tags, release) {
8694
if (!release || release === 'next') return true
87-
return tags.some(tag => semver.eq(tag, release))
95+
try {
96+
return tags.some(tag => semver.eq(tag, release))
97+
} catch (e) {
98+
if (e.name === 'TypeError') {
99+
throw FunctionalError(e.message)
100+
}
101+
return null
102+
}
88103
}
89104

90105
async function generateVersions({
@@ -120,11 +135,17 @@ async function generateChangelog(from, to, {
120135
if (from === TAIL) {
121136
tagsToProcess = [...tagsToProcess, TAIL]
122137
} else {
123-
const fromIndex = tagsToProcess.findIndex(tag => semver.eq(tag, from))
124-
tagsToProcess.splice(fromIndex + 1)
138+
try {
139+
const fromIndex = tagsToProcess.findIndex(tag => semver.eq(tag, from))
140+
tagsToProcess.splice(fromIndex + 1)
125141

126-
if (hasNext && isEmpty(tagsToProcess)) {
127-
tagsToProcess.push(HEAD)
142+
if (hasNext && isEmpty(tagsToProcess)) {
143+
tagsToProcess.push(HEAD)
144+
}
145+
} catch (e) {
146+
if (e.name === 'TypeError') {
147+
throw FunctionalError(e.message)
148+
}
128149
}
129150
}
130151

@@ -137,7 +158,7 @@ async function generateChangelog(from, to, {
137158
})
138159

139160
if (from !== TAIL && changes.length === 1 && isEmpty(changes[0].groups)) {
140-
throw new Error('No changes found. You may need to fetch or pull the last changes.')
161+
throw new FunctionalError('No changes found. You may need to fetch or pull the last changes.')
141162
}
142163

143164
return {

0 commit comments

Comments
 (0)