From b10a2bdcb461882d55545c8c459d2a72d7937457 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 10 Jul 2026 20:25:03 +0100 Subject: [PATCH] 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 --- src/Appwrite/Screenshots/Client.php | 20 ++++-- src/Appwrite/Screenshots/Exception.php | 7 ++ tests/unit/Screenshots/ClientTest.php | 94 ++++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 4 deletions(-) create mode 100644 src/Appwrite/Screenshots/Exception.php create mode 100644 tests/unit/Screenshots/ClientTest.php diff --git a/src/Appwrite/Screenshots/Client.php b/src/Appwrite/Screenshots/Client.php index ad5b78a8f6..74fe9fb98c 100644 --- a/src/Appwrite/Screenshots/Client.php +++ b/src/Appwrite/Screenshots/Client.php @@ -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 $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; } } diff --git a/src/Appwrite/Screenshots/Exception.php b/src/Appwrite/Screenshots/Exception.php new file mode 100644 index 0000000000..4f6b17a39b --- /dev/null +++ b/src/Appwrite/Screenshots/Exception.php @@ -0,0 +1,7 @@ +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(''))) + ->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'); + } +}