Skip to content

Commit 219d2ea

Browse files
committed
Format errors better, add a test
1 parent b5872ea commit 219d2ea

File tree

5 files changed

+56
-3
lines changed

5 files changed

+56
-3
lines changed

src/action.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import fm from 'front-matter'
44
import nunjucks from 'nunjucks'
55
// @ts-ignore
66
import dateFilter from 'nunjucks-date-filter'
7+
import { ZodError } from 'zod'
78
import { FrontMatterAttributes, frontmatterSchema, listToArray, setOutputs } from './helpers'
89

9-
function logError(tools: Toolkit, template: string, action: 'creating' | 'updating', err: any) {
10+
function logError(tools: Toolkit, template: string, action: 'creating' | 'updating' | 'parsing', err: any) {
1011
// Log the error message
1112
const errorMessage = `An error occurred while ${action} the issue. This might be caused by a malformed issue title, or a typo in the labels or assignees. Check ${template}!`
1213
tools.log.error(errorMessage)
@@ -51,7 +52,18 @@ export async function createAnIssue (tools: Toolkit) {
5152

5253
// Grab the front matter as JSON
5354
const { attributes: rawAttributes, body } = fm<FrontMatterAttributes>(file)
54-
const attributes = await frontmatterSchema.parseAsync(rawAttributes)
55+
56+
let attributes: FrontMatterAttributes
57+
try {
58+
attributes = await frontmatterSchema.parseAsync(rawAttributes)
59+
} catch (err) {
60+
if (err instanceof ZodError) {
61+
const formatted = err.format()
62+
return logError(tools, template, 'parsing', formatted)
63+
}
64+
throw err
65+
}
66+
5567
tools.log(`Front matter for ${template} is`, attributes)
5668

5769
const templated = {

src/helpers.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const frontmatterSchema = z.object({
66
assignees: z.union([z.array(z.string()), z.string()]).optional(),
77
labels: z.union([z.array(z.string()), z.string()]).optional(),
88
milestone: z.union([z.string(), z.number()]).optional()
9-
})
9+
}).strict()
1010

1111
export type FrontMatterAttributes = z.infer<typeof frontmatterSchema>
1212

tests/__snapshots__/index.test.ts.snap

+26
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,32 @@ exports[`create-an-issue logs a helpful error if creating an issue throws an err
232232
]
233233
`;
234234

235+
exports[`create-an-issue logs a helpful error if the frontmatter is invalid 1`] = `
236+
[
237+
[
238+
"An error occurred while parsing the issue. This might be caused by a malformed issue title, or a typo in the labels or assignees. Check .github/invalid-frontmatter.md!",
239+
],
240+
[
241+
{
242+
"_errors": [
243+
"Unrecognized key(s) in object: 'name', 'not_a_thing'",
244+
],
245+
"labels": {
246+
"_errors": [
247+
"Expected array, received number",
248+
"Expected string, received number",
249+
],
250+
},
251+
"title": {
252+
"_errors": [
253+
"Required",
254+
],
255+
},
256+
},
257+
],
258+
]
259+
`;
260+
235261
exports[`create-an-issue logs a helpful error if updating an issue throws an error with more errors 1`] = `
236262
[
237263
[
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
name: "Not a title"
3+
labels: 123
4+
not_a_thing: "testing"
5+
---
6+
Hi!

tests/index.test.ts

+9
Original file line numberDiff line numberDiff line change
@@ -302,4 +302,13 @@ describe('create-an-issue', () => {
302302
expect((tools.log.error as any).mock.calls).toMatchSnapshot()
303303
expect(tools.exit.failure).toHaveBeenCalled()
304304
})
305+
306+
it('logs a helpful error if the frontmatter is invalid', async () => {
307+
process.env.INPUT_FILENAME = '.github/invalid-frontmatter.md'
308+
309+
await createAnIssue(tools)
310+
expect(tools.log.error).toHaveBeenCalled()
311+
expect((tools.log.error as any).mock.calls).toMatchSnapshot()
312+
expect(tools.exit.failure).toHaveBeenCalled()
313+
})
305314
})

0 commit comments

Comments
 (0)