ApiUsageException.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. */
  20. /**
  21. * Exception used to abort API execution with an error
  22. *
  23. * If possible, use ApiBase::dieWithError() instead of throwing this directly.
  24. *
  25. * @ingroup API
  26. */
  27. class ApiUsageException extends MWException implements ILocalizedException {
  28. protected $modulePath;
  29. protected $status;
  30. /**
  31. * @param ApiBase|null $module API module responsible for the error, if known
  32. * @param StatusValue $status Status holding errors
  33. * @param int $httpCode HTTP error code to use
  34. */
  35. public function __construct(
  36. ApiBase $module = null, StatusValue $status, $httpCode = 0
  37. ) {
  38. if ( $status->isOK() ) {
  39. throw new InvalidArgumentException( __METHOD__ . ' requires a fatal Status' );
  40. }
  41. $this->modulePath = $module ? $module->getModulePath() : null;
  42. $this->status = $status;
  43. // Bug T46111: Messages in the log files should be in English and not
  44. // customized by the local wiki.
  45. $enMsg = clone $this->getApiMessage();
  46. $enMsg->inLanguage( 'en' )->useDatabase( false );
  47. parent::__construct( ApiErrorFormatter::stripMarkup( $enMsg->text() ), $httpCode );
  48. }
  49. /**
  50. * @param ApiBase|null $module API module responsible for the error, if known
  51. * @param string|array|Message $msg See ApiMessage::create()
  52. * @param string|null $code See ApiMessage::create()
  53. * @param array|null $data See ApiMessage::create()
  54. * @param int $httpCode HTTP error code to use
  55. * @return static
  56. */
  57. public static function newWithMessage(
  58. ApiBase $module = null, $msg, $code = null, $data = null, $httpCode = 0
  59. ) {
  60. return new static(
  61. $module,
  62. StatusValue::newFatal( ApiMessage::create( $msg, $code, $data ) ),
  63. $httpCode
  64. );
  65. }
  66. /**
  67. * @return ApiMessage
  68. */
  69. private function getApiMessage() {
  70. $errors = $this->status->getErrorsByType( 'error' );
  71. if ( !$errors ) {
  72. $errors = $this->status->getErrors();
  73. }
  74. if ( !$errors ) {
  75. $msg = new ApiMessage( 'apierror-unknownerror-nocode', 'unknownerror' );
  76. } else {
  77. $msg = ApiMessage::create( $errors[0] );
  78. }
  79. return $msg;
  80. }
  81. /**
  82. * Fetch the responsible module name
  83. * @return string|null
  84. */
  85. public function getModulePath() {
  86. return $this->modulePath;
  87. }
  88. /**
  89. * Fetch the error status
  90. * @return StatusValue
  91. */
  92. public function getStatusValue() {
  93. return $this->status;
  94. }
  95. /**
  96. * @inheritDoc
  97. */
  98. public function getMessageObject() {
  99. return Status::wrap( $this->status )->getMessage();
  100. }
  101. /**
  102. * @return string
  103. */
  104. public function __toString() {
  105. $enMsg = clone $this->getApiMessage();
  106. $enMsg->inLanguage( 'en' )->useDatabase( false );
  107. $text = ApiErrorFormatter::stripMarkup( $enMsg->text() );
  108. return get_class( $this ) . ": {$enMsg->getApiCode()}: {$text} "
  109. . "in {$this->getFile()}:{$this->getLine()}\n"
  110. . "Stack trace:\n{$this->getTraceAsString()}";
  111. }
  112. }