Skip to content

Commit

Permalink
feat(Dangerfile): improve error message output
Browse files Browse the repository at this point in the history
  • Loading branch information
macklinu authored and orta committed Aug 15, 2017
1 parent 515537a commit f664ee5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
25 changes: 18 additions & 7 deletions source/runner/DangerfileRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,28 @@ export async function runDangerfileEnvironment(
messages: results.messages,
markdowns: results.markdowns,
}
} catch (e) {
} catch (error) {
console.error("Unable to evaluate the Dangerfile")
return {
fails: [{ message: `\`\`\`\n${e.stack}\n\`\`\`` }],
warnings: [],
messages: [],
markdowns: [],
}
return resultsForCaughtError(filename, content, error)
}
}

/** Returns Markdown results to post if an exception is raised during the danger run */
const resultsForCaughtError = (file: string, contents: string, error: Error): DangerResults => {
const failure = `Danger failed to run \`${file}\`.`
const errorMD = `## Error ${error.name}
\`\`\`
${error.message}
${error.stack}
\`\`\`
### Dangerfile
\`\`\`
${contents}
\`\`\`
`
return { fails: [{ message: failure }], warnings: [], markdowns: [errorMD], messages: [] }
}

// https://regex101.com/r/dUq4yB/1
const requirePattern = /^.* require\(('|")danger('|")\);?$/gm
// https://regex101.com/r/dUq4yB/2
Expand Down
6 changes: 4 additions & 2 deletions source/runner/_tests/_danger_runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ describe("with fixtures", () => {
const runtime = await createDangerfileRuntimeEnvironment(context)
const results = await runDangerfileEnvironment(resolve(fixtures, "__DangerfileBadSyntax.js"), undefined, runtime)

expect(results.fails[0].message).toContain("hello is not defined")
expect(results.fails[0].message).toContain("Danger failed to run")
expect(results.markdowns[0]).toContain("hello is not defined")
})

it.skip("handles relative imports correctly in Babel", async () => {
Expand Down Expand Up @@ -179,7 +180,8 @@ describe("with fixtures", () => {
const runtime = await createDangerfileRuntimeEnvironment(context)
const results = await runDangerfileEnvironment(resolve(fixtures, "__DangerfileThrows.js"), undefined, runtime)

expect(results.fails[0].message).toContain("Error: failure")
expect(results.fails[0].message).toContain("Danger failed to run")
expect(results.markdowns[0]).toContain("Error: failure")
})
})

Expand Down

0 comments on commit f664ee5

Please sign in to comment.