wiki.techinc.nl/includes/libs/ParamValidator/ValidationException.php
Ricordisamoa 1b3bc281ac Clean up redundant Exception|Throwable union type
PHP 7.0 makes many error conditions throw instances of the new Error class
which does not extend the known Exception.
The Throwable interface provides a concise and type-safe way of handling
either, e.g. for logging purposes, but HHVM did not support it, requiring
tedious fallback checks.

This commit replaces occurrences of Exception in code paths equally
covered by Throwable, like Exception|Throwable parameter and return types
(also nullable), instanceof guards, duplicated `catch` blocks, as well as
related comments and documentation blocks, with the exception of $previous
parameter descriptions consistent with the manual at
https://www.php.net/manual/en/exception.construct.php

Proper type declarations have been added or reinstated where possible.

Change-Id: I5d3920d3cc66936a350314e2f19c4f6faeffd7c0
2020-02-12 20:28:40 +00:00

89 lines
2.1 KiB
PHP

<?php
namespace Wikimedia\ParamValidator;
use Throwable;
use UnexpectedValueException;
use Wikimedia\Message\DataMessageValue;
/**
* Error reporting for ParamValidator
*
* @since 1.34
* @unstable
*/
class ValidationException extends UnexpectedValueException {
/** @var DataMessageValue */
protected $failureMessage;
/** @var string */
protected $paramName;
/** @var mixed */
protected $paramValue;
/** @var array */
protected $settings;
/**
* @param DataMessageValue $failureMessage Failure message.
* @param string $name Parameter name being validated
* @param mixed $value Value of the parameter
* @param array $settings Settings array being used for validation
* @param Throwable|null $previous Previous throwable causing this failure
*/
public function __construct(
DataMessageValue $failureMessage, $name, $value, $settings, Throwable $previous = null
) {
$this->failureMessage = $failureMessage;
$this->paramName = $name;
$this->paramValue = $value;
$this->settings = $settings;
// Parent class needs some static English message.
$msg = "Validation of `$name` failed: " . $failureMessage->getCode();
$data = $failureMessage->getData();
if ( $data ) {
$msg .= ' ' . json_encode( $data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
}
parent::__construct( $msg, 0, $previous );
}
/**
* Fetch the validation failure message
*
* Users are encouraged to use this with an appropriate message formatter rather
* than relying on the limited English text returned by getMessage().
*
* @return DataMessageValue
*/
public function getFailureMessage() {
return $this->failureMessage;
}
/**
* Fetch the parameter name that failed validation
* @return string
*/
public function getParamName() {
return $this->paramName;
}
/**
* Fetch the parameter value that failed validation
* @return mixed
*/
public function getParamValue() {
return $this->paramValue;
}
/**
* Fetch the settings array that failed validation
* @return array
*/
public function getSettings() {
return $this->settings;
}
}