Skip to content

Core\Test\Response::error

$response->error(): ?string

In a test, error() returns the error that stopped a request, if your program threw one and did not catch it.

Core\Test::request() sends a request to your own program. If the program throws an error that it does not catch, the request fails. error() then returns the class and the message of that error, as Class: message, for example RuntimeError: the cart is empty. If the request did not fail, the result is null.

A failed request has the status 500 and an empty body, the same response a real visitor gets. From status() and body(), a test only sees that the request failed. error() returns the reason.

Good to know: an error that the program catches does not stop the request, so error() returns null.

The examples below show a request that fails and two requests with no error. The last one is a test that checks the reason for a failure.

Return value

Returns ?string. The class and the message of the error, as Class: message, for example RuntimeError: the cart is empty. If the request did not fail, the result is null.

Changelog

VersionChange
0.0.5Core\Test\Response::error returns the class and message of the error that stopped a request, such as RuntimeError: no stock. It returns null when the request did not fail.

Examples

The error that stopped a request

<?nvs
// Shows how to read the error that stopped a request.
use Core\Http\Method;
use Core\Test;
if (Core\Env::context() == Core\Env::Context::Request) {
// This part answers the request. It throws an error and does not catch it.
throw new RuntimeError('the cart is empty');
} else {
var $response = Test::request(Method::Get, '/checkout');
// The request failed, so the status is 500. This prints `500`.
echo $response->status(), "\n";
// `error()` returns the class and the message of the error.
// This prints `RuntimeError: the cart is empty`.
echo $response->error(), "\n";
}
Output
500
RuntimeError: the cart is empty

Null when the request did not fail

<?nvs
// Shows that `error()` returns `null` when the request did not fail.
use Core\Http\Method;
use Core\Test;
if (Core\Env::context() == Core\Env::Context::Request) {
// This part answers each request. `/caught` throws an error and catches it,
// so the request still works.
if (Core\Request::path() == '/caught') {
try {
throw new RuntimeError('the price list is missing');
} catch (RuntimeError $e) {
echo 'default prices';
}
} else {
echo 'Hello';
}
} else {
// A normal request. This prints `200 null`.
var $plain = Test::request(Method::Get, '/hello');
echo $plain->status(), ' ', $plain->error() ?? 'null', "\n";
// The program caught its error, so the request did not fail.
// This prints `200 null default prices`.
var $caught = Test::request(Method::Get, '/caught');
echo $caught->status(), ' ', $caught->error() ?? 'null', ' ', $caught->body(), "\n";
}
Output
200 null
200 null default prices

Test that a request fails for the right reason

<?nvs
// Shows a test that checks why a request failed, not only that it failed.
use Core\Http\Method;
use Core\Test;
class OutOfStock extends RuntimeError
{
}
if (Core\Env::context() == Core\Env::Context::Request) {
// This part answers each request. An order for item 7 fails,
// because that item is out of stock.
if (Core\Request::path() == '/order/7') {
throw new OutOfStock('item 7 is out of stock');
}
echo 'order placed';
} else {
// This part is the test. Every failed request has the status 500,
// so the test also checks the error.
var $response = Test::request(Method::Post, '/order/7');
Test::assertEquals($response->status(), 500);
Test::assertEquals($response->error(), 'OutOfStock: item 7 is out of stock');
var $other = Test::request(Method::Post, '/order/3');
Test::assertNull($other->error());
// All checks passed, so this prints `the order fails for the right reason`.
echo "the order fails for the right reason\n";
}
Output
the order fails for the right reason