wiki.techinc.nl/includes/libs/ParamValidator/ValidationException.php
Brad Jorsch aa0720d37c ParamValidator: Use MessageValue!
Trying to get away with returning a single code and parameter-list that
was supposed to represent both human-readable and machine-readable data
was a mistake.

This patch converts it to use DataMessageValue, which represents the two
separately and also provides guidance for supplying translations of all
the error codes.

This also eliminates the "describeSettings()" method that was trying to
serve multiple use cases (in terms of the Action API, action=paraminfo
and action=help). It's replaced by two methods that each serve one of
the use cases. Also some of the functionality was moved out of the
TypeDef base class into ParamValidator, to better match where the
constants themselves live.

Also I wound up creating a NumericDef base class so FloatDef can share
the same range-checking logic that IntegerDef has. I probably should
have done that as a separate patch, but untangling it now would be too
much work.

Bug: T235801
Change-Id: Iea6d4a1d05bb4b92d60415b0f03ff9d3dc99a80b
2019-11-01 15:49:31 -04:00

90 lines
2.1 KiB
PHP

<?php
namespace Wikimedia\ParamValidator;
use Exception;
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|Exception|null $previous Previous exception causing this failure
*/
public function __construct(
DataMessageValue $failureMessage, $name, $value, $settings, $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;
}
}