/** * Get error message key and parameters from status * * @param \Status $status * @return array of error message key, and message parameters as an array * @throws Exception if called on an "OK" Status */ public function getStatusError(\Status $status) { if ($status->isOK()) { throw new \Exception('Status contains no errors'); } $errors = $status->getErrorsByType('error'); if (!empty($errors[0]['message'])) { $message = $errors[0]['message']; $params = $errors[0]['params']; } else { $message = 'fbconnect-error'; $params = []; } return [$message, $params]; }
/** * @covers Status::getErrorsByType */ public function testGetErrorsByType() { $status = new Status(); $warning = new Message('warning111'); $error = new Message('error111'); $status->warning($warning); $status->error($error); $warnings = $status->getErrorsByType('warning'); $errors = $status->getErrorsByType('error'); $this->assertCount(1, $warnings); $this->assertCount(1, $errors); $this->assertEquals($warning, $warnings[0]['message']); $this->assertEquals($error, $errors[0]['message']); }
/** * Converts a Status object to an array suitable for addValue * @param Status $status * @param string $errorType * @return array */ public function convertStatusToArray($status, $errorType = 'error') { if ($status->isGood()) { return array(); } $result = array(); foreach ($status->getErrorsByType($errorType) as $error) { $this->setIndexedTagName($error['params'], 'param'); $result[] = $error; } $this->setIndexedTagName($result, $errorType); return $result; }
public function arrayFromStatus(Status $status, $type = 'error', $format = null) { if ($status->isGood() || !$status->errors) { return []; } $result = []; foreach ($status->getErrorsByType($type) as $error) { if ($error['message'] instanceof Message) { $error = ['message' => $error['message']->getKey(), 'params' => $error['message']->getParams()] + $error; } ApiResult::setIndexedTagName($error['params'], 'param'); $result[] = $error; } ApiResult::setIndexedTagName($result, $type); return $result; }
/** * @param string|array|Status $err * @return string */ function getErrors($err) { if (!$err) { $errors = array(); } elseif ($err instanceof Status) { if ($err->isOK()) { $errors = array(); } else { $errors = $err->getErrorsByType('error'); foreach ($errors as &$error) { // Input: array( 'message' => 'foo', 'errors' => array( 'a', 'b', 'c' ) ) // Output: array( 'foo', 'a', 'b', 'c' ) $error = array_merge(array($error['message']), $error['params']); } } } else { $errors = $err; if (!is_array($errors)) { $errors = array($errors); } } foreach ($errors as &$error) { if (is_array($error)) { $msg = array_shift($error); } else { $msg = $error; $error = array(); } // if the error is already a message object, don't use it as a message key if (!$msg instanceof Message) { $error = $this->msg($msg, $error)->parse(); } else { $error = $msg->parse(); } $error = new OOUI\HtmlSnippet($error); } // Used in getBody() $this->oouiErrors = $errors; return ''; }
/** * Get error (as code, string) from a Status object. * * @since 1.23 * @param Status $status * @param array|null &$extraData Set if extra data from IApiMessage is available (since 1.27) * @return array Array of code and error string * @throws MWException */ public function getErrorFromStatus($status, &$extraData = null) { if ($status->isGood()) { throw new MWException('Successful status passed to ApiBase::dieStatus'); } $errors = $status->getErrorsByType('error'); if (!$errors) { // No errors? Assume the warnings should be treated as errors $errors = $status->getErrorsByType('warning'); } if (!$errors) { // Still no errors? Punt $errors = [['message' => 'unknownerror-nocode', 'params' => []]]; } // Cannot use dieUsageMsg() because extensions might return custom // error messages. if ($errors[0]['message'] instanceof Message) { $msg = $errors[0]['message']; if ($msg instanceof IApiMessage) { $extraData = $msg->getApiData(); $code = $msg->getApiCode(); } else { $code = $msg->getKey(); } } else { $code = $errors[0]['message']; $msg = wfMessage($code, $errors[0]['params']); } if (isset(ApiBase::$messageMap[$code])) { // Translate message to code, for backwards compatibility $code = ApiBase::$messageMap[$code]['code']; } return [$code, $msg->inLanguage('en')->useDatabase(false)->plain()]; }
/** * @param string|array|Status $elements * @param string $elementsType * @return string */ function getErrorsOrWarnings($elements, $elementsType) { if (!in_array($elementsType, ['error', 'warning'])) { throw new DomainException($elementsType . ' is not a valid type.'); } if (!$elements) { $errors = []; } elseif ($elements instanceof Status) { if ($elements->isGood()) { $errors = []; } else { $errors = $elements->getErrorsByType($elementsType); foreach ($errors as &$error) { // Input: [ 'message' => 'foo', 'errors' => [ 'a', 'b', 'c' ] ] // Output: [ 'foo', 'a', 'b', 'c' ] $error = array_merge([$error['message']], $error['params']); } } } elseif ($elementsType === 'errors') { $errors = $elements; if (!is_array($errors)) { $errors = [$errors]; } } else { $errors = []; } foreach ($errors as &$error) { $error = $this->getMessage($error)->parse(); $error = new OOUI\HtmlSnippet($error); } // Used in getBody() if ($elementsType === 'error') { $this->oouiErrors = $errors; } else { $this->oouiWarnings = $errors; } return ''; }
/** * @param string|array|Status $err * @return string */ function getErrors($err) { if (!$err) { $errors = []; } elseif ($err instanceof Status) { if ($err->isOK()) { $errors = []; } else { $errors = $err->getErrorsByType('error'); foreach ($errors as &$error) { // Input: array( 'message' => 'foo', 'errors' => array( 'a', 'b', 'c' ) ) // Output: array( 'foo', 'a', 'b', 'c' ) $error = array_merge([$error['message']], $error['params']); } } } else { $errors = $err; if (!is_array($errors)) { $errors = [$errors]; } } foreach ($errors as &$error) { $error = $this->getMessage($error)->parse(); $error = new OOUI\HtmlSnippet($error); } // Used in getBody() $this->oouiErrors = $errors; return ''; }