fix: improve Msg91 error handling to extract API error messages#133
fix: improve Msg91 error handling to extract API error messages#133deepshekhardas wants to merge 2 commits into
Conversation
Greptile SummaryThis PR improves error handling in the Msg91 SMS adapter by extracting a structured error message from the API response body (checking
Confidence Score: 4/5The change is safe for the common JSON-response path but silently produces a one-character error message when Msg91 returns a non-JSON (plain-text) body. When request() cannot JSON-decode the response, $result['response'] stays as a raw string. The new code accesses $result['response']['message'] on that string — PHP coerces the key to 0 and returns the first character, so isset() is truthy and $errorMessage becomes a single character instead of the actual error text. This is a present defect on the non-JSON error path. src/Utopia/Messaging/Adapter/SMS/Msg91.php — the response array-type guard before nested key access. Important Files Changed
Prompt To Fix All With AIFix the following 1 code review issue. Work through them one at a time, proposing concise fixes.
---
### Issue 1 of 1
src/Utopia/Messaging/Adapter/SMS/Msg91.php:104-112
**Response array type not guarded before key access**
`request()` declares its `response` field as `array<string, mixed>|string|null`. When Msg91 returns a non-JSON body (e.g. a plain-text "Unauthorized" or an HTML error page), `$result['response']` stays as a raw string. Accessing `$result['response']['message']` on a string in PHP coerces the key `'message'` to integer `0` and returns the first character of the string — so `isset($result['response']['message'])` evaluates to `true` for any non-empty error body, and `$errorMessage` silently becomes a single character (e.g. `"U"` for `"Unauthorized"`). Add an `is_array($result['response'])` guard before the nested key checks to prevent this.
Reviews (3): Last reviewed commit: "fix: guard Msg91 error extraction agains..." | Re-trigger Greptile |
48d9e52 to
a5a921b
Compare
| if (isset($result['response']['message'])) { | ||
| $errorMessage = \is_string($result['response']['message']) | ||
| ? $result['response']['message'] | ||
| : \json_encode($result['response']['message']); | ||
| } elseif (isset($result['response']['error'])) { | ||
| $errorMessage = \is_string($result['response']['error']) | ||
| ? $result['response']['error'] | ||
| : \json_encode($result['response']['error']); | ||
| } |
There was a problem hiding this comment.
Response array type not guarded before key access
request() declares its response field as array<string, mixed>|string|null. When Msg91 returns a non-JSON body (e.g. a plain-text "Unauthorized" or an HTML error page), $result['response'] stays as a raw string. Accessing $result['response']['message'] on a string in PHP coerces the key 'message' to integer 0 and returns the first character of the string — so isset($result['response']['message']) evaluates to true for any non-empty error body, and $errorMessage silently becomes a single character (e.g. "U" for "Unauthorized"). Add an is_array($result['response']) guard before the nested key checks to prevent this.
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/Utopia/Messaging/Adapter/SMS/Msg91.php
Line: 104-112
Comment:
**Response array type not guarded before key access**
`request()` declares its `response` field as `array<string, mixed>|string|null`. When Msg91 returns a non-JSON body (e.g. a plain-text "Unauthorized" or an HTML error page), `$result['response']` stays as a raw string. Accessing `$result['response']['message']` on a string in PHP coerces the key `'message'` to integer `0` and returns the first character of the string — so `isset($result['response']['message'])` evaluates to `true` for any non-empty error body, and `$errorMessage` silently becomes a single character (e.g. `"U"` for `"Unauthorized"`). Add an `is_array($result['response'])` guard before the nested key checks to prevent this.
How can I resolve this? If you propose a fix, please make it concise.|
Following up - Msg91 error handling fix. Let me know if changes needed. |
No description provided.