Skip to content

Commit

Permalink
Format errors better, add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonEtco committed Dec 21, 2022
1 parent b5872ea commit 219d2ea
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 3 deletions.
16 changes: 14 additions & 2 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import fm from 'front-matter'
import nunjucks from 'nunjucks'
// @ts-ignore
import dateFilter from 'nunjucks-date-filter'
import { ZodError } from 'zod'
import { FrontMatterAttributes, frontmatterSchema, listToArray, setOutputs } from './helpers'

function logError(tools: Toolkit, template: string, action: 'creating' | 'updating', err: any) {
function logError(tools: Toolkit, template: string, action: 'creating' | 'updating' | 'parsing', err: any) {
// Log the error message
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}!`
tools.log.error(errorMessage)
Expand Down Expand Up @@ -51,7 +52,18 @@ export async function createAnIssue (tools: Toolkit) {

// Grab the front matter as JSON
const { attributes: rawAttributes, body } = fm<FrontMatterAttributes>(file)
const attributes = await frontmatterSchema.parseAsync(rawAttributes)

let attributes: FrontMatterAttributes
try {
attributes = await frontmatterSchema.parseAsync(rawAttributes)
} catch (err) {
if (err instanceof ZodError) {
const formatted = err.format()
return logError(tools, template, 'parsing', formatted)
}
throw err
}

tools.log(`Front matter for ${template} is`, attributes)

const templated = {
Expand Down
2 changes: 1 addition & 1 deletion src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const frontmatterSchema = z.object({
assignees: z.union([z.array(z.string()), z.string()]).optional(),
labels: z.union([z.array(z.string()), z.string()]).optional(),
milestone: z.union([z.string(), z.number()]).optional()
})
}).strict()

export type FrontMatterAttributes = z.infer<typeof frontmatterSchema>

Expand Down
26 changes: 26 additions & 0 deletions tests/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,32 @@ exports[`create-an-issue logs a helpful error if creating an issue throws an err
]
`;

exports[`create-an-issue logs a helpful error if the frontmatter is invalid 1`] = `
[
[
"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!",
],
[
{
"_errors": [
"Unrecognized key(s) in object: 'name', 'not_a_thing'",
],
"labels": {
"_errors": [
"Expected array, received number",
"Expected string, received number",
],
},
"title": {
"_errors": [
"Required",
],
},
},
],
]
`;

exports[`create-an-issue logs a helpful error if updating an issue throws an error with more errors 1`] = `
[
[
Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/.github/invalid-frontmatter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
name: "Not a title"
labels: 123
not_a_thing: "testing"
---
Hi!
9 changes: 9 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,13 @@ describe('create-an-issue', () => {
expect((tools.log.error as any).mock.calls).toMatchSnapshot()
expect(tools.exit.failure).toHaveBeenCalled()
})

it('logs a helpful error if the frontmatter is invalid', async () => {
process.env.INPUT_FILENAME = '.github/invalid-frontmatter.md'

await createAnIssue(tools)
expect(tools.log.error).toHaveBeenCalled()
expect((tools.log.error as any).mock.calls).toMatchSnapshot()
expect(tools.exit.failure).toHaveBeenCalled()
})
})

0 comments on commit 219d2ea

Please sign in to comment.