UnexpectedValueException
PHPERRORCommonValidation

Value does not match expected set

Quick Answer

Throw UnexpectedValueException when a value returned from a callback or external source does not match the expected format or type.

What this means

Thrown when a value does not match what was expected — e.g., the return value of a callback does not match a required interface or format.

Why it happens
  1. 1External API returns unexpected JSON structure
  2. 2Callback returns wrong type

Fix

Validate external data

Validate external data
$payload = json_decode($body, true);
if (!isset($payload['id'], $payload['status'])) {
    throw new \UnexpectedValueException("Malformed webhook payload: " . $body);
}

Why this works

Validating external data at the boundary gives a clear error with the offending data in the message.

Code examples
Callback return checkphp
$result = $transformer($item);
if (!is_array($result)) {
    throw new \UnexpectedValueException("Transformer must return array, got " . gettype($result));
}

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All PHP errors