Skip to content

Commit c16610b

Browse files
author
Jacob Middag
committed
Better error message for cURL and JSON
1 parent 57e77d6 commit c16610b

4 files changed

Lines changed: 36 additions & 3 deletions

File tree

src/class-tiny-compress-curl.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ protected function shrink($input) {
4747

4848
$output_url = null;
4949
$response = curl_exec($request);
50-
if ($response === false) {
50+
if ($response === false || $response === null) {
5151
return array(array(
5252
'error' => 'CurlError',
53-
'message' => sprintf("%s [%d]", curl_error($request), curl_errno($request))
53+
'message' => sprintf("cURL: %s [%d]", curl_error($request), curl_errno($request))
5454
), null
5555
);
5656
}

src/class-tiny-compress.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,10 @@ protected static function parse_headers($headers) {
102102
protected static function decode($text) {
103103
$result = json_decode($text, true);
104104
if ($result === null) {
105-
throw new Tiny_Exception('Could not decode JSON', 'JsonError');
105+
throw new Tiny_Exception(sprintf('JSON: %s [%d]',
106+
PHP_VERSION_ID >= 50500 ? json_last_error_msg() : 'Error',
107+
json_last_error()),
108+
'JsonError');
106109
}
107110
return $result;
108111
}

test/integration/CompressIntegrationTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,18 @@ public function testLimitReachedDismisses() {
6767
self::$driver->get(wordpress('/wp-admin/options-media.php'));
6868
$this->assertEquals(0, count(self::$driver->findElements(WebDriverBy::cssSelector('div.error p'))));
6969
}
70+
71+
public function testIncorrectJsonButton() {
72+
$this->enable_compression_sizes(array());
73+
$this->upload_image(dirname(__FILE__) . '/../fixtures/input-example.png');
74+
$this->enable_compression_sizes(array('medium', 'large'));
75+
76+
$this->set_api_key('JSON1234');
77+
self::$driver->get(wordpress('/wp-admin/upload.php'));
78+
79+
self::$driver->findElement(WebDriverBy::cssSelector('td.tiny-compress-images button'))->click();
80+
self::$driver->wait(2)->until(WebDriverExpectedCondition::textToBePresentInElement(
81+
WebDriverBy::cssSelector('td.tiny-compress-images'), 'JSON: Syntax error [4]'));
82+
}
83+
7084
}

test/mock-tinypng-webservice/shrink.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ function mock_limit_reached_response() {
9494
return json_encode($response);
9595
}
9696

97+
function mock_invalid_json_response() {
98+
global $session;
99+
100+
$session['Compression-Count'] += 1;
101+
header('HTTP/1.1 201 Created');
102+
header("Location: http://webservice/output/example.png");
103+
header("Compression-Count: {$session['Compression-Count']}");
104+
return '{invalid: json}';
105+
}
106+
97107
$request_headers = apache_request_headers();
98108
$basic_auth = base64_decode(str_replace('Basic ', '', $request_headers['Authorization']));
99109
$api_key_elements = explode(':', $basic_auth);
@@ -113,6 +123,12 @@ function mock_limit_reached_response() {
113123
} else {
114124
echo mock_jpg_response();
115125
}
126+
} else if ($api_key == 'JSON1234') {
127+
if (intval($_SERVER['CONTENT_LENGTH']) == 0) {
128+
echo mock_empty_response();
129+
} else {
130+
echo mock_invald_json_response();
131+
}
116132
} else if ($api_key == 'LIMIT123') {
117133
echo mock_limit_reached_response();
118134
} else {

0 commit comments

Comments
 (0)