fix: harden Messenger validation and review feedback#124
Conversation
Greptile SummaryThis PR introduces a new
Confidence Score: 5/5Safe to merge; the core failover logic and validation are correct, and the test coverage is thorough. The new Messenger class has a well-structured constructor guard, proper adapter-compatibility validation, and comprehensive tests. The only fresh finding is a minor exception-type inconsistency in send() that does not affect correctness at runtime. src/Utopia/Messaging/Messenger.php — the exception thrown for an invalid message type in send() could be tightened to \InvalidArgumentException. Important Files Changed
Reviews (4): Last reviewed commit: "feat: add Messenger class with failover ..." | Re-trigger Greptile |
| } catch (\Exception $e) { | ||
| $errors[] = $adapter->getName() | ||
| .' (adapter ' | ||
| .($index + 1) | ||
| .'): ' | ||
| .$e->getMessage(); | ||
| } |
There was a problem hiding this comment.
The catch clause only intercepts
\Exception, so any adapter that throws a subclass of \Error (e.g. \TypeError from a bad API response shape, \ValueError, or an unchecked \AssertionError) will propagate immediately and skip every remaining adapter — silently breaking the failover chain that is the entire purpose of this class. Using \Throwable ensures all adapter failures trigger the fallback path.
| } catch (\Exception $e) { | |
| $errors[] = $adapter->getName() | |
| .' (adapter ' | |
| .($index + 1) | |
| .'): ' | |
| .$e->getMessage(); | |
| } | |
| } catch (\Throwable $e) { | |
| $errors[] = $adapter->getName() | |
| .' (adapter ' | |
| .($index + 1) | |
| .'): ' | |
| .$e->getMessage(); | |
| } |
| .' is ' | ||
| .\get_debug_type($adapter) | ||
| .'.' | ||
| ); |
There was a problem hiding this comment.
Index-base inconsistency in error messages
The constructor loop emits "element N" using the raw 0-based key (e.g. "element 1" for the second item), while validateAdapters emits "adapter N" using a 1-based offset ($index + 1). A caller who encounters both errors will see different numbering conventions for what is logically the same position in the array. Choosing one scheme (consistently 0-based or consistently 1-based) would make the messages unambiguous.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
|
Want your agent to iterate on Greptile's feedback? Try greploops. |
| $this->validateAdapters($adapters); | ||
|
|
||
| $this->adapters = $adapters; |
There was a problem hiding this comment.
The constructor accepts any adapter array, but validateAdapters() reads $adapters[0] directly. PHP arrays preserve caller keys, so a valid list with preserved keys, such as the result of filtering adapters or [1 => $adapter], passes the earlier empty() and instanceof checks and then fails with an undefined key/null dereference instead of constructing a single-adapter messenger. Reindex the array before validation, or explicitly reject non-list arrays, so valid adapter lists do not depend on having key 0.
Harden Messenger validation based on review feedback. Improves input validation and error handling across message processing.