Skip to content

Commit 306d0d8

Browse files
authored
Merge pull request #13 from JasonEtco/helpful-error
Throw helpful error when issue creation fails
2 parents 5c1234b + fed92b6 commit 306d0d8

File tree

3 files changed

+79
-10
lines changed

3 files changed

+79
-10
lines changed

index.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,27 @@ Toolkit.run(async tools => {
3535
tools.log.info(`Creating new issue ${templated.title}`)
3636

3737
// Create the new issue
38-
const issue = await tools.github.issues.create({
39-
...tools.context.repo,
40-
...templated,
41-
assignees: listToArray(attributes.assignees),
42-
labels: listToArray(attributes.labels),
43-
milestone: attributes.milestone
44-
})
45-
46-
tools.log.success(`Created issue ${issue.data.title}#${issue.data.number}: ${issue.data.html_url}`)
38+
try {
39+
const issue = await tools.github.issues.create({
40+
...tools.context.repo,
41+
...templated,
42+
assignees: listToArray(attributes.assignees),
43+
labels: listToArray(attributes.labels),
44+
milestone: attributes.milestone
45+
})
46+
47+
tools.log.success(`Created issue ${issue.data.title}#${issue.data.number}: ${issue.data.html_url}`)
48+
} catch (err) {
49+
// Log the error message
50+
tools.log.error(`An error occurred while creating the issue. This might be caused by a malformed issue title, or a typo in the labels or assignees. Check ${template}!`)
51+
tools.log.error(err)
52+
53+
// The error might have more details
54+
if (err.errors) tools.log.error(err.errors)
55+
56+
// Exit with a failing status
57+
tools.exit.failure()
58+
}
4759
}, {
4860
secrets: ['GITHUB_TOKEN']
4961
})

tests/__snapshots__/index.test.js.snap

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,32 @@ Array [
8686
],
8787
]
8888
`;
89+
90+
exports[`create-an-issue logs a helpful error if creating an issue throws an error 1`] = `
91+
Array [
92+
Array [
93+
"An error occurred while creating the issue. This might be caused by a malformed issue title, or a typo in the labels or assignees. Check .github/ISSUE_TEMPLATE.md!",
94+
],
95+
Array [
96+
[HttpError: Validation error],
97+
],
98+
]
99+
`;
100+
101+
exports[`create-an-issue logs a helpful error if creating an issue throws an error with more errors 1`] = `
102+
Array [
103+
Array [
104+
"An error occurred while creating the issue. This might be caused by a malformed issue title, or a typo in the labels or assignees. Check .github/ISSUE_TEMPLATE.md!",
105+
],
106+
Array [
107+
[HttpError: Validation error],
108+
],
109+
Array [
110+
Array [
111+
Object {
112+
"foo": true,
113+
},
114+
],
115+
],
116+
]
117+
`;

tests/index.test.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ describe('create-an-issue', () => {
2323
info: jest.fn(),
2424
success: jest.fn(),
2525
warn: jest.fn(),
26-
debug: jest.fn()
26+
debug: jest.fn(),
27+
error: jest.fn()
2728
}
2829
})
2930

@@ -70,4 +71,31 @@ describe('create-an-issue', () => {
7071
expect(tools.log.success).toHaveBeenCalled()
7172
expect(tools.log.success.mock.calls).toMatchSnapshot()
7273
})
74+
75+
it('logs a helpful error if creating an issue throws an error', async () => {
76+
nock.cleanAll()
77+
nock('https://api.github.com')
78+
.post(/\/repos\/.*\/.*\/issues/).reply(500, {
79+
message: 'Validation error'
80+
})
81+
82+
await actionFn(tools)
83+
expect(tools.log.error).toHaveBeenCalled()
84+
expect(tools.log.error.mock.calls).toMatchSnapshot()
85+
expect(tools.exit.failure).toHaveBeenCalled()
86+
})
87+
88+
it('logs a helpful error if creating an issue throws an error with more errors', async () => {
89+
nock.cleanAll()
90+
nock('https://api.github.com')
91+
.post(/\/repos\/.*\/.*\/issues/).reply(500, {
92+
message: 'Validation error',
93+
errors: [{ foo: true }]
94+
})
95+
96+
await actionFn(tools)
97+
expect(tools.log.error).toHaveBeenCalled()
98+
expect(tools.log.error.mock.calls).toMatchSnapshot()
99+
expect(tools.exit.failure).toHaveBeenCalled()
100+
})
73101
})

0 commit comments

Comments
 (0)