Skip to content

fix: improve Msg91 error handling to extract API error messages#133

Open
deepshekhardas wants to merge 2 commits into
utopia-php:mainfrom
deepshekhardas:fix/msg91-error-handling
Open

fix: improve Msg91 error handling to extract API error messages#133
deepshekhardas wants to merge 2 commits into
utopia-php:mainfrom
deepshekhardas:fix/msg91-error-handling

Conversation

@deepshekhardas

Copy link
Copy Markdown

No description provided.

@greptile-apps

greptile-apps Bot commented Jun 17, 2026

Copy link
Copy Markdown

Greptile Summary

This PR improves error handling in the Msg91 SMS adapter by extracting a structured error message from the API response body (checking response.message then response.error) instead of always returning the generic 'Unknown error' string. Non-string values are handled via json_encode as a fallback.

  • The error extraction logic correctly handles both string and non-string values within the nested fields, avoiding a TypeError at addResult().
  • However, $result['response'] itself can be a raw string (when the API returns a non-JSON body) or null, and the nested key access is not guarded against those types — PHP will silently coerce a string key on a string value to index 0, returning just the first character as the error message.

Confidence Score: 4/5

The 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

Filename Overview
src/Utopia/Messaging/Adapter/SMS/Msg91.php Error handling improved to extract API error messages, but the nested key access on $result['response'] is not guarded against non-array values (string/null), causing silent truncation to a single character when the response body is plain text.

Fix All in Claude Code Fix All in Codex

Prompt To Fix All With AI
Fix 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

Comment thread src/Utopia/Messaging/Adapter/SMS/Msg91.php
@deepshekhardas deepshekhardas force-pushed the fix/msg91-error-handling branch from 48d9e52 to a5a921b Compare July 10, 2026 05:01
Comment on lines +104 to +112
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']);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 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.

Fix in Claude Code Fix in Codex

@deepshekhardas

Copy link
Copy Markdown
Author

Following up - Msg91 error handling fix. Let me know if changes needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant