示例#1
0
 /**
  * Create an error message for the given exception.
  *
  * If the exception is a UsageException then
  * UsageException::getMessageArray() will be called to create the message.
  *
  * @param Exception $e
  * @return array ['code' => 'some string', 'info' => 'some other string']
  * @since 1.27
  */
 protected function errorMessageFromException($e)
 {
     if ($e instanceof UsageException) {
         // User entered incorrect parameters - generate error response
         $errMessage = $e->getMessageArray();
     } else {
         $config = $this->getConfig();
         // Something is seriously wrong
         if ($e instanceof DBQueryError && !$config->get('ShowSQLErrors')) {
             $info = 'Database query error';
         } else {
             $info = "Exception Caught: {$e->getMessage()}";
         }
         $errMessage = ['code' => 'internal_api_error_' . get_class($e), 'info' => '[' . WebRequest::getRequestId() . '] ' . $info];
     }
     return $errMessage;
 }
示例#2
0
 /**
  * Replace the result data with the information about an exception.
  * Returns the error code
  * @param Exception $e
  * @return string
  */
 protected function substituteResultWithError($e)
 {
     $result = $this->getResult();
     $config = $this->getConfig();
     if ($e instanceof UsageException) {
         // User entered incorrect parameters - generate error response
         $errMessage = $e->getMessageArray();
         $link = wfExpandUrl(wfScript('api'));
         ApiResult::setContentValue($errMessage, 'docref', "See {$link} for API usage");
     } else {
         // Something is seriously wrong
         if ($e instanceof DBQueryError && !$config->get('ShowSQLErrors')) {
             $info = 'Database query error';
         } else {
             $info = "Exception Caught: {$e->getMessage()}";
         }
         $errMessage = array('code' => 'internal_api_error_' . get_class($e), 'info' => '[' . MWExceptionHandler::getLogId($e) . '] ' . $info);
         if ($config->get('ShowExceptionDetails')) {
             ApiResult::setContentValue($errMessage, 'trace', MWExceptionHandler::getRedactedTraceAsString($e));
         }
     }
     // Remember all the warnings to re-add them later
     $warnings = $result->getResultData(array('warnings'));
     $result->reset();
     // Re-add the id
     $requestid = $this->getParameter('requestid');
     if (!is_null($requestid)) {
         $result->addValue(null, 'requestid', $requestid, ApiResult::NO_SIZE_CHECK);
     }
     if ($config->get('ShowHostnames')) {
         // servedby is especially useful when debugging errors
         $result->addValue(null, 'servedby', wfHostName(), ApiResult::NO_SIZE_CHECK);
     }
     if ($warnings !== null) {
         $result->addValue(null, 'warnings', $warnings, ApiResult::NO_SIZE_CHECK);
     }
     $result->addValue(null, 'error', $errMessage, ApiResult::NO_SIZE_CHECK);
     return $errMessage['code'];
 }
示例#3
0
 /**
  * Replace the result data with the information about an exception.
  * Returns the error code
  * @param Exception $e
  * @return string
  */
 protected function substituteResultWithError($e)
 {
     $result = $this->getResult();
     // Printer may not be initialized if the extractRequestParams() fails for the main module
     if (!isset($this->mPrinter)) {
         // The printer has not been created yet. Try to manually get formatter value.
         $value = $this->getRequest()->getVal('format', self::API_DEFAULT_FORMAT);
         if (!$this->mModuleMgr->isDefined($value, 'format')) {
             $value = self::API_DEFAULT_FORMAT;
         }
         $this->mPrinter = $this->createPrinterByName($value);
     }
     // Printer may not be able to handle errors. This is particularly
     // likely if the module returns something for getCustomPrinter().
     if (!$this->mPrinter->canPrintErrors()) {
         $this->mPrinter->safeProfileOut();
         $this->mPrinter = $this->createPrinterByName(self::API_DEFAULT_FORMAT);
     }
     // Update raw mode flag for the selected printer.
     $result->setRawMode($this->mPrinter->getNeedsRawData());
     $config = $this->getConfig();
     if ($e instanceof UsageException) {
         // User entered incorrect parameters - print usage screen
         $errMessage = $e->getMessageArray();
         // Only print the help message when this is for the developer, not runtime
         if ($this->mPrinter->getWantsHelp() || $this->mAction == 'help') {
             ApiResult::setContent($errMessage, $this->makeHelpMsg());
         }
     } else {
         // Something is seriously wrong
         if ($e instanceof DBQueryError && !$config->get('ShowSQLErrors')) {
             $info = 'Database query error';
         } else {
             $info = "Exception Caught: {$e->getMessage()}";
         }
         $errMessage = array('code' => 'internal_api_error_' . get_class($e), 'info' => $info);
         ApiResult::setContent($errMessage, $config->get('ShowExceptionDetails') ? "\n\n{$e->getTraceAsString()}\n\n" : '');
     }
     // Remember all the warnings to re-add them later
     $oldResult = $result->getData();
     $warnings = isset($oldResult['warnings']) ? $oldResult['warnings'] : null;
     $result->reset();
     // Re-add the id
     $requestid = $this->getParameter('requestid');
     if (!is_null($requestid)) {
         $result->addValue(null, 'requestid', $requestid, ApiResult::NO_SIZE_CHECK);
     }
     if ($config->get('ShowHostnames')) {
         // servedby is especially useful when debugging errors
         $result->addValue(null, 'servedby', wfHostName(), ApiResult::NO_SIZE_CHECK);
     }
     if ($warnings !== null) {
         $result->addValue(null, 'warnings', $warnings, ApiResult::NO_SIZE_CHECK);
     }
     $result->addValue(null, 'error', $errMessage, ApiResult::NO_SIZE_CHECK);
     return $errMessage['code'];
 }