SimpleSAML_Error_Error::__construct PHP Method

__construct() public method

The error can either be given as a string, or as an array. If it is an array, the first element in the array (with index 0), is the error code, while the other elements are replacements for the error text.
public __construct ( mixed $errorCode, Exception $cause = null, integer | null $httpCode = null )
$errorCode mixed One of the error codes defined in the errors dictionary.
$cause Exception The exception which caused this fatal error (if any). Optional.
$httpCode integer | null The HTTP response code to use. Optional.
    public function __construct($errorCode, Exception $cause = null, $httpCode = null)
    {
        assert('is_string($errorCode) || is_array($errorCode)');
        if (is_array($errorCode)) {
            $this->parameters = $errorCode;
            unset($this->parameters[0]);
            $this->errorCode = $errorCode[0];
        } else {
            $this->parameters = array();
            $this->errorCode = $errorCode;
        }
        if (isset($httpCode)) {
            $this->httpCode = $httpCode;
        }
        $moduleCode = explode(':', $this->errorCode, 2);
        if (count($moduleCode) === 2) {
            $this->module = $moduleCode[0];
            $this->dictTitle = '{' . $this->module . ':errors:title_' . $moduleCode[1] . '}';
            $this->dictDescr = '{' . $this->module . ':errors:descr_' . $moduleCode[1] . '}';
        } else {
            $this->dictTitle = SimpleSAML\Error\ErrorCodes::getErrorCodeTitle($this->errorCode);
            $this->dictDescr = SimpleSAML\Error\ErrorCodes::getErrorCodeDescription($this->errorCode);
        }
        if (!empty($this->parameters)) {
            $msg = $this->errorCode . '(';
            foreach ($this->parameters as $k => $v) {
                if ($k === 0) {
                    continue;
                }
                $msg .= var_export($k, true) . ' => ' . var_export($v, true) . ', ';
            }
            $msg = substr($msg, 0, -2) . ')';
        } else {
            $msg = $this->errorCode;
        }
        parent::__construct($msg, -1, $cause);
    }

Usage Example

Example #1
0
 /**
  * Create a new BadRequest error.
  *
  * @param string $reason  Description of why the request was unacceptable.
  */
 public function __construct($reason)
 {
     assert('is_string($reason)');
     $this->reason = $reason;
     parent::__construct(array('BADREQUEST', '%REASON%' => $this->reason));
     $this->httpCode = 400;
 }
All Usage Examples Of SimpleSAML_Error_Error::__construct