Skip to content

Commit

Permalink
Throw error when PHP run() receives no code to run (#1646)
Browse files Browse the repository at this point in the history
## Motivation for the change, related issues

Prior to this PR, calling PHP#run() without providing an object with a
`code` or `scriptPath` property, the call errors but does not clearly
communicate the issue to the caller:
`Error: PHP.run() failed with exit code 255 and the following output:
PHP Fatal error: Uncaught ValueError: Path cannot be empty in [no active
file]:0`

After this change, the error is:
``TypeError: The request object must have either a `code` or a
`scriptPath` property.``

## Testing Instructions (or ideally a Blueprint)

CI unit tests
  • Loading branch information
brandonpayton authored Jul 24, 2024
1 parent d9d764d commit d5c2ca4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
6 changes: 6 additions & 0 deletions packages/php-wasm/node/src/test/php.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,12 @@ describe.each(SupportedPHPVersions)('PHP %s', (phpVersion) => {
});
});

describe('Interface', () => {
it('run() should throw an error when neither `code` nor `scriptFile` is provided', async () => {
expect(() => php.run({})).rejects.toThrowError(TypeError);
});
});

describe('Startup sequence – basics', () => {
/**
* This test ensures that the PHP runtime can be loaded twice.
Expand Down
7 changes: 6 additions & 1 deletion packages/php-wasm/universal/src/lib/php.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,13 @@ export class PHP implements Disposable {
if (typeof request.code === 'string') {
this.writeFile('/internal/eval.php', request.code);
this.#setScriptPath('/internal/eval.php');
} else {
} else if (typeof request.scriptPath === 'string') {
this.#setScriptPath(request.scriptPath || '');
} else {
throw new TypeError(
'The request object must have either a `code` or a ' +
'`scriptPath` property.'
);
}

const $_SERVER = this.#prepareServerEntries(
Expand Down

0 comments on commit d5c2ca4

Please sign in to comment.