Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP: Handle request errors in PHPRequestHandler, return response code 500 #1249

Merged
merged 10 commits into from
Apr 17, 2024
60 changes: 60 additions & 0 deletions packages/php-wasm/node/src/test/php-request-handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,66 @@ describe.each(SupportedPHPVersions)(
});
});

it('should return httpStatus 500 if exit code is not 0', async () => {
php.writeFile(
'/index.php',
`<?php
echo 'Hello World';
`
);
const response1Result = await handler.request({
url: '/index.php',
});
php.writeFile(
'/index.php',
`<?php
die('Hello World')
adamziel marked this conversation as resolved.
Show resolved Hide resolved
`
);
const response2Result = await handler.request({
url: '/index.php',
});
php.writeFile(
'/index.php',
`<?php
echo 'Hello World!';
`
);
const response3Result = await handler.request({
url: '/index.php',
});
expect(response1Result).toEqual({
httpStatusCode: 200,
headers: {
'content-type': ['text/html; charset=UTF-8'],
'x-powered-by': [expect.any(String)],
},
bytes: new TextEncoder().encode('Hello World'),
errors: '',
exitCode: 0,
});
expect(response2Result).toEqual({
httpStatusCode: 500,
headers: {
'content-type': ['text/html; charset=UTF-8'],
'x-powered-by': [expect.any(String)],
},
bytes: expect.any(Uint8Array),
errors: expect.any(String),
exitCode: 255,
});
expect(response3Result).toEqual({
httpStatusCode: 200,
headers: {
'content-type': ['text/html; charset=UTF-8'],
'x-powered-by': [expect.any(String)],
},
bytes: new TextEncoder().encode('Hello World!'),
errors: '',
exitCode: 0,
});
});

it('Should accept `body` as a JavaScript object', async () => {
/**
* Tests against calling phpwasm_init_uploaded_files_hash() when
Expand Down
8 changes: 7 additions & 1 deletion packages/php-wasm/universal/src/lib/base-php.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ const STRING = 'string';
const NUMBER = 'number';

export const __private__dont__use = Symbol('__private__dont__use');

export interface PHPExecutionFailureError extends Error {
response: PHPResponse;
source: 'request' | 'php-wasm';
}

/**
* An environment-agnostic wrapper around the Emscripten PHP runtime
* that universals the super low-level API and provides a more convenient
Expand Down Expand Up @@ -661,7 +667,7 @@ export abstract class BasePHP implements IsomorphicLocalPHP {

const { headers, httpStatusCode } = this.#getResponseHeaders();
return new PHPResponse(
httpStatusCode,
exitCode === 0 ? httpStatusCode : 500,
headers,
this.readFileAsBuffer('/internal/stdout'),
this.readFileAsText('/internal/stderr'),
Expand Down
36 changes: 24 additions & 12 deletions packages/php-wasm/universal/src/lib/php-request-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import {
removePathPrefix,
DEFAULT_BASE_URL,
} from './urls';
import { BasePHP, normalizeHeaders } from './base-php';
import {
BasePHP,
PHPExecutionFailureError,
normalizeHeaders,
} from './base-php';
import { PHPResponse } from './php-response';
import { PHPRequest, PHPRunOptions, RequestHandler } from './universal-php';
import { encodeAsMultipart } from './encode-as-multipart';
Expand Down Expand Up @@ -242,17 +246,25 @@ export class PHPRequestHandler implements RequestHandler {
);
}

return await this.php.run({
relativeUri: ensurePathPrefix(
toRelativeUrl(requestedUrl),
this.#PATHNAME
),
protocol: this.#PROTOCOL,
method: request.method || preferredMethod,
body,
scriptPath,
headers,
});
try {
return await this.php.run({
relativeUri: ensurePathPrefix(
toRelativeUrl(requestedUrl),
this.#PATHNAME
),
protocol: this.#PROTOCOL,
method: request.method || preferredMethod,
body,
scriptPath,
headers,
});
} catch (error) {
const executionError = error as PHPExecutionFailureError;
if (executionError?.response) {
return executionError.response;
}
throw error;
}
} finally {
release();
}
Expand Down
Loading