Handle browser service error responses in the screenshots client

The browser service returns raw image bytes on success and a 4xx JSON
{"error": message} on failure (per appwrite/docker-browser 0.3.2). The
client now surfaces that message through a typed Screenshots\Exception
carrying the status code, and rejects non-image success responses
instead of saving them as PNG files. Covered by unit tests with a
stubbed PSR-18 client.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
loks0nandClaude Fable 5 committed 2026-07-10 20:25:03 +01:00
1 parent a9a7a1eba3
commit b10a2bdcb4
3 files changed
+117 -4

No files matched your search

+16 -4
View File
@@ -22,8 +22,11 @@ class Client
/**
* Capture a screenshot of a page and return the PNG bytes.
*
* The browser service responds with raw image bytes on success and
* `{"error": message}` with a 4xx status on failure.
*
* @param array<string, string> $headers
* @throws \Exception on an error response
* @throws Exception on an error response
* @throws ClientExceptionInterface on transport failure
*/
public function create(string $url, string $theme, array $headers = [], int $sleep = 3000): string
@@ -35,10 +38,19 @@ class Client
'sleep' => $sleep,
]));
if ($response->getStatusCode() >= 400) {
throw new \Exception((string)$response->getBody());
$status = $response->getStatusCode();
$body = (string)$response->getBody();
if ($status >= 400) {
$error = \json_decode($body, true)['error'] ?? null;
throw new Exception(\is_string($error) ? $error : 'Screenshot failed with status ' . $status, $status);
}
return (string)$response->getBody();
$contentType = $response->getHeaderLine('Content-Type');
if (!\str_starts_with($contentType, 'image/')) {
throw new Exception('Expected an image response, got ' . ($contentType === '' ? 'no content type' : $contentType), $status);
}
return $body;
}
}
+7
View File
@@ -0,0 +1,7 @@
<?php
namespace Appwrite\Screenshots;
class Exception extends \Exception
{
}
+94
View File
@@ -0,0 +1,94 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Screenshots;
use Appwrite\Screenshots\Client;
use Appwrite\Screenshots\Exception;
use PHPUnit\Framework\TestCase;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Utopia\Psr7\Response;
use Utopia\Psr7\Stream;
final class ClientTest extends TestCase
{
private ?RequestInterface $request = null;
private function client(ResponseInterface $response): Client
{
$test = $this;
return new Client(new class ($response, $test) implements ClientInterface {
public function __construct(private ResponseInterface $response, private ClientTest $test)
{
}
public function sendRequest(RequestInterface $request): ResponseInterface
{
$this->test->setRequest($request);
return $this->response;
}
});
}
public function setRequest(RequestInterface $request): void
{
$this->request = $request;
}
public function testCreate(): void
{
$response = (new Response(200, body: new Stream('png-bytes')))
->withHeader('Content-Type', 'image/png');
$screenshot = $this->client($response)->create(
url: 'http://appwrite/',
theme: 'dark',
headers: ['x-appwrite-hostname' => 'example.com'],
sleep: 500,
);
$this->assertSame('png-bytes', $screenshot);
$this->assertSame('POST', $this->request->getMethod());
$this->assertSame('screenshots', (string)$this->request->getUri());
$this->assertEquals([
'url' => 'http://appwrite/',
'theme' => 'dark',
'headers' => ['x-appwrite-hostname' => 'example.com'],
'sleep' => 500,
], \json_decode((string)$this->request->getBody(), true));
}
public function testCreateError(): void
{
$response = (new Response(400, body: new Stream('{"error":"Timeout exceeded"}')))
->withHeader('Content-Type', 'application/json');
$this->expectException(Exception::class);
$this->expectExceptionMessage('Timeout exceeded');
$this->expectExceptionCode(400);
$this->client($response)->create('http://appwrite/', 'light');
}
public function testCreateErrorWithoutBody(): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('Screenshot failed with status 502');
$this->client(new Response(502))->create('http://appwrite/', 'light');
}
public function testCreateUnexpectedContentType(): void
{
$response = (new Response(200, body: new Stream('<html>')))
->withHeader('Content-Type', 'text/html');
$this->expectException(Exception::class);
$this->expectExceptionMessage('Expected an image response, got text/html');
$this->client($response)->create('http://appwrite/', 'light');
}
}