moodle/moodle · error · InvalidArgumentException
Expected an Aws\ResultInterface or Exception.
Error message
Expected an Aws\ResultInterface or Exception.
What it means
MockHandler::append() only accepts values that are AWS ResultInterface objects, Exception/Throwable instances, or callables, since those are the only things it can return when the handler is invoked. Any other queued value (e.g. a raw array, a string, or an SDK Model object created before the result exists) is rejected with InvalidArgumentException at queue-append time rather than failing later during the mocked send.
Solutions
- Wrap plain response data in Aws\Result before appending: $mock->append(new Result(['Foo' => 'bar'])).
- If you intend an error, append an Aws\Exception or Aws\Result implementing object instead of a string/array.
- For dynamic behavior, append a callable: $mock->append(function (CommandInterface $cmd, RequestInterface $req) { return new Result([...]); }).
- Check that the value class actually implements Aws\ResultInterface (e.g. it is an Aws\Result, not an array or Guzzle response).
Example fix
// before $mock->append(['Products' => []]); // after $mock->append(new \Aws\Result(['Products' => []]));
Defensive patterns
Strategy: validation
Validate before calling
if (!$value instanceof \Aws\ResultInterface && !$value instanceof \Exception && !is_callable($value)) { throw new \InvalidArgumentException('append() requires ResultInterface, Exception, or callable'); } Type guard
function isQueueableMockOutcome($v): bool { return $v instanceof \Aws\ResultInterface || $v instanceof \Exception || is_callable($v); } Try / catch
try { $mock->append($value); } catch (\InvalidArgumentException $e) { $mock->append(new \Aws\Result((array) $value)); } Prevention
- Always wrap raw response arrays in new Aws\Result([...]) before appending
- Remember AWS MockHandler does not accept Guzzle responses or arrays, unlike Guzzle's MockHandler
- Add a small test helper that validates queued values before appending
When it happens
Trigger: Calling $mock->append(['key' => 'value']) with a plain array, appending a string, null, or an arbitrary object, or appending a value that implements neither ResultInterface, Exception, nor __invoke while configuring a mocked AWS client.
Common situations: Test setups where developers copy examples that queue arrays; hand-built fake responses not wrapped in Result::fromArray; migrating test code from other HTTP mocking libraries (Guzzle MockHandler accepts arrays/responses, AWS MockHandler does not).
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Expected an \Exception or \Throwable.
- Mock queue is empty. Trying to send a
- batch_size is not > 0
- before must be callable
- Mock queue is empty
AI-assisted analysis of moodle/moodle@48ea8c3322 (2026-09-14).
Data as JSON: /api/errors/b380a97e6ce61951.
Report an issue: GitHub.
Appendix: source
Thrown at public/lib/aws-sdk/src/MockHandler.php:58
if ($resultOrQueue) {
call_user_func_array([$this, 'append'], array_values($resultOrQueue));
}
}
/**
* Adds one or more variadic ResultInterface or AwsException objects to the
* queue.
*/
public function append()
{
foreach (func_get_args() as $value) {
if ($value instanceof ResultInterface
|| $value instanceof Exception
|| is_callable($value)
) {
$this->queue[] = $value;
} else {
throw new \InvalidArgumentException('Expected an Aws\ResultInterface or Exception.');
}
}
}
/**
* Adds one or more \Exception or \Throwable to the queue
*/
public function appendException()
{
foreach (func_get_args() as $value) {
if ($value instanceof \Exception || $value instanceof \Throwable) {
$this->queue[] = $value;
} else {
throw new \InvalidArgumentException('Expected an \Exception or \Throwable.');
}
}
}
View on GitHub (pinned to 48ea8c3322)