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'); + } +}