Added 2 steps to clear and debug API responses.#59
Conversation
📝 WalkthroughWalkthroughTwo new Behat step definitions were added to ApiServerContext: Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Comment |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #59 +/- ##
==========================================
+ Coverage 75.34% 75.45% +0.10%
==========================================
Files 3 3
Lines 430 440 +10
==========================================
+ Hits 324 332 +8
- Misses 106 108 +2 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@src/DrevOps/BehatPhpServer/ApiServerContext.php`:
- Around line 157-168: The debugApiRequests method currently assumes the
response body is valid JSON; change debugApiRequests to validate the json_decode
result (call json_decode($body, TRUE) into a variable, then check
json_last_error() or that the result is not null) and when decoding fails write
a clear message and the raw response body to STDOUT instead of encoding
null—update references to $response->getBody(), the json_decode call, and the
$message construction so invalid JSON is reported gracefully while valid JSON
continues to be pretty-printed.
| public function debugApiRequests(): void { | ||
| $response = $this->client->request('GET', '/admin/requests'); | ||
|
|
||
| if ($response->getStatusCode() !== 200) { | ||
| throw new \RuntimeException('Failed to fetch the API requests.'); | ||
| } | ||
|
|
||
| $body = (string) $response->getBody(); | ||
|
|
||
| $message = json_encode(json_decode($body, TRUE), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); | ||
| fwrite(STDOUT, "\nAPI Requests Debug Info:\n" . $message . "\n"); | ||
| } |
There was a problem hiding this comment.
Handle invalid JSON gracefully in debug output.
If the response body is not valid JSON, json_decode($body, TRUE) returns null, and json_encode(null, ...) will output the string "null", which could be confusing during debugging.
Proposed fix
public function debugApiRequests(): void {
$response = $this->client->request('GET', '/admin/requests');
if ($response->getStatusCode() !== 200) {
throw new \RuntimeException('Failed to fetch the API requests.');
}
$body = (string) $response->getBody();
- $message = json_encode(json_decode($body, TRUE), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
+ $decoded = json_decode($body, TRUE);
+ $message = $decoded !== null
+ ? json_encode($decoded, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
+ : $body;
fwrite(STDOUT, "\nAPI Requests Debug Info:\n" . $message . "\n");
}🧰 Tools
🪛 PHPMD (2.15.0)
161-161: Missing class import via use statement (line '161', column '17'). (undefined)
(MissingImport)
🤖 Prompt for AI Agents
In `@src/DrevOps/BehatPhpServer/ApiServerContext.php` around lines 157 - 168, The
debugApiRequests method currently assumes the response body is valid JSON;
change debugApiRequests to validate the json_decode result (call
json_decode($body, TRUE) into a variable, then check json_last_error() or that
the result is not null) and when decoding fails write a clear message and the
raw response body to STDOUT instead of encoding null—update references to
$response->getBody(), the json_decode call, and the $message construction so
invalid JSON is reported gracefully while valid JSON continues to be
pretty-printed.
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.