"username_taken"]`. * Branch on the codes, the messages are for people and may change. */ class ValidationFailedException extends ApiException { /** * @return array field => code; empty when the endpoint sends none */ public function errorCodes(): array { $codes = $this->body()->codes ?? null; return is_object($codes) || is_array($codes) ? array_map('strval', (array) $codes) : []; } public function errorCodeFor(string $field): ?string { return $this->errorCodes()[$field] ?? null; } /** * Whether any field failed with $code, or the response's `error` is $code. */ public function hasErrorCode(string $code): bool { return $this->error === $code || in_array($code, $this->errorCodes(), true); } /** * The messages per field (`errors`), for showing them to people. * * @return array> */ public function errors(): array { $errors = $this->body()->errors ?? null; return is_object($errors) || is_array($errors) ? array_map(fn ($messages) => array_values((array) $messages), (array) $errors) : []; } }