Skip to content

Commit

Permalink
Improve error message when unable to resolve GITHUB_EVENT_PATH
Browse files Browse the repository at this point in the history
The error message here was a bit cryptic:

```
Error: Cannot find module '/path/to/some/file.json'
Require stack:
- /e2e/node_modules/happo-e2e/src/resolveEnvironment.js
...
```

I want to make it easier for people to troubleshoot this on their own. I
think we can try/catch the require and provide a clearer error message
that helps people understand that something may be up with the
GITHUB_EVENT_PATH environment variable.
  • Loading branch information
lencioni committed Jan 10, 2025
1 parent 5903e00 commit ba0fa94
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/resolveEnvironment.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ const envKeys = [
'SYSTEM_PULLREQUEST_SOURCEREPOSITORYURI',
];

function resolveGithubEvent(GITHUB_EVENT_PATH) {
try {
return require(GITHUB_EVENT_PATH);
} catch (e) {
throw new Error(
`Failed to load GitHub event from the GITHUB_EVENT_PATH environment variable: "${GITHUB_EVENT_PATH}"`,
{ cause: e },
);
}
}

function resolveLink(env) {
const {
CHANGE_URL,
Expand Down Expand Up @@ -67,7 +78,8 @@ function resolveLink(env) {
}

if (GITHUB_EVENT_PATH) {
const ghEvent = require(GITHUB_EVENT_PATH);
const ghEvent = resolveGithubEvent(GITHUB_EVENT_PATH);

if (ghEvent.pull_request) {
return ghEvent.pull_request.html_url;
}
Expand Down Expand Up @@ -120,7 +132,7 @@ function resolveMessage(env) {
return HAPPO_MESSAGE;
}
if (GITHUB_EVENT_PATH) {
const ghEvent = require(GITHUB_EVENT_PATH);
const ghEvent = resolveGithubEvent(GITHUB_EVENT_PATH);
if (ghEvent.pull_request) {
return ghEvent.pull_request.title;
}
Expand Down Expand Up @@ -202,7 +214,7 @@ function resolveBeforeSha(env, afterSha) {
}

if (GITHUB_EVENT_PATH) {
const ghEvent = require(GITHUB_EVENT_PATH);
const ghEvent = resolveGithubEvent(GITHUB_EVENT_PATH);
if (ghEvent.pull_request) {
return ghEvent.pull_request.base.sha;
}
Expand Down Expand Up @@ -273,7 +285,7 @@ function resolveAfterSha(env) {
return BUILD_SOURCEVERSION;
}
if (GITHUB_EVENT_PATH) {
const ghEvent = require(GITHUB_EVENT_PATH);
const ghEvent = resolveGithubEvent(GITHUB_EVENT_PATH);
if (ghEvent.pull_request) {
return ghEvent.pull_request.head.sha;
}
Expand Down
16 changes: 16 additions & 0 deletions test/resolveEnvironment-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,22 @@ function testGithubActionsEnvironment() {
'https://github.com/octo-org/octo-repo/commit/ccddffddccffdd',
);
assert.ok(result.message !== undefined);

// Try with a non-existing event path
let caughtError;
try {
resolveEnvironment({
...githubEnv,
GITHUB_EVENT_PATH: 'non-existing-path',
});
} catch (e) {
caughtError = e;
}
assert.ok(caughtError);
assert.equal(
caughtError.message,
'Failed to load GitHub event from the GITHUB_EVENT_PATH environment variable: "non-existing-path"',
);
}

function testGithubMergeGroupEnvironment() {
Expand Down

0 comments on commit ba0fa94

Please sign in to comment.