Skip to content

Commit 759f977

Browse files
committed
feat: handle new response codes for better error feedback
1 parent 9cfcf9e commit 759f977

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

phpstan-baseline.neon

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
parameters:
22
ignoreErrors:
3+
-
4+
message: "#^Cannot access offset 'error' on mixed\\.$#"
5+
count: 2
6+
path: src/MySqlExplainException.php
7+
8+
-
9+
message: "#^Cannot access offset 'message' on mixed\\.$#"
10+
count: 2
11+
path: src/MySqlExplainException.php
12+
13+
-
14+
message: "#^Part \\$response\\['message'\\] \\(mixed\\) of encapsed string cannot be cast to string\\.$#"
15+
count: 2
16+
path: src/MySqlExplainException.php
17+
318
-
419
message: "#^Parameter \\$explainJson of class Tpetry\\\\MysqlExplain\\\\Values\\\\QueryMetrics constructor expects string, float\\|int\\|string\\|null given\\.$#"
520
count: 1

src/Helpers/ApiHelper.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
namespace Tpetry\MysqlExplain\Helpers;
66

77
use GuzzleHttp\Client;
8-
use GuzzleHttp\Exception\GuzzleException;
8+
use GuzzleHttp\Exception\BadResponseException;
9+
use Throwable;
910
use Tpetry\MysqlExplain\MysqlExplain;
1011
use Tpetry\MysqlExplain\MySqlExplainException;
1112
use Tpetry\MysqlExplain\Values\QueryMetrics;
@@ -46,8 +47,10 @@ public function submitPlan(QueryMetrics $metrics): string
4647
$json = json_decode($response->getBody()->getContents(), true);
4748

4849
return $json['url'];
49-
} catch (GuzzleException $e) {
50-
throw MySqlExplainException::fromException($e);
50+
} catch (BadResponseException $e) {
51+
throw MySqlExplainException::fromBadResponseException($e);
52+
} catch (Throwable $t) {
53+
throw MySqlExplainException::fromThrowable($t);
5154
}
5255
}
5356
}

src/MySqlExplainException.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,28 @@
55
namespace Tpetry\MysqlExplain;
66

77
use Exception;
8+
use GuzzleHttp\Exception\BadResponseException;
89
use Throwable;
910

1011
class MySqlExplainException extends Exception
1112
{
12-
public static function fromException(Throwable $exception): self
13+
public static function fromBadResponseException(BadResponseException $e): self
1314
{
14-
return new self('Submitting query to explainmysql.com failed.', 0, $exception);
15+
$response = json_decode($e->getResponse()->getBody()->getContents(), true);
16+
if ($response) {
17+
if (isset($response['error'], $response['message']) && $response['error'] === 'unsupported_database') {
18+
return new self("Submitting EXPLAIN failed (Unsupported Datatabase: {$response['message']}).", 0, $e);
19+
}
20+
if (isset($response['error'], $response['message']) && $response['error'] === 'unsupported_query') {
21+
return new self("Submitting EXPLAIN failed (Unsupported Query: {$response['message']}).", 0, $e);
22+
}
23+
}
24+
25+
return self::fromThrowable($e);
26+
}
27+
28+
public static function fromThrowable(Throwable $t): self
29+
{
30+
return new self("Submitting EXPLAIN failed ({$t->getMessage()}).", 0, $t);
1531
}
1632
}

0 commit comments

Comments
 (0)