X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Fapi%2FApiErrorFormatter.php;h=5484a78efe0359840c906b504fa27b2a3f46ee04;hb=a8379682a46a428320c88702c800a6107c015137;hp=f246203a3a9d4fedbac7afba76e963b21d09317c;hpb=f9e21f91e459c40fed6d8f2e76cccbce07e2ba1b;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/api/ApiErrorFormatter.php b/includes/api/ApiErrorFormatter.php index f246203a3a..183f7e2bb3 100644 --- a/includes/api/ApiErrorFormatter.php +++ b/includes/api/ApiErrorFormatter.php @@ -148,10 +148,11 @@ class ApiErrorFormatter { * @param Exception|Throwable $exception * @param array $options * - wrap: (string|array|MessageSpecifier) Used to wrap the exception's - * message. The exception's message will be added as the final parameter. + * message if it's not an ILocalizedException. The exception's message + * will be added as the final parameter. * - code: (string) Default code - * - data: (array) Extra data - * @return ApiMessage + * - data: (array) Default extra data + * @return IApiMessage */ public function getMessageFromException( $exception, array $options = [] ) { $options += [ 'code' => null, 'data' => [] ]; @@ -159,15 +160,18 @@ class ApiErrorFormatter { if ( $exception instanceof ILocalizedException ) { $msg = $exception->getMessageObject(); $params = []; + } elseif ( $exception instanceof MessageSpecifier ) { + $msg = Message::newFromSpecifier( $exception ); + $params = []; } else { // Extract code and data from the exception, if applicable if ( $exception instanceof UsageException ) { $data = $exception->getMessageArray(); - if ( !isset( $options['code'] ) ) { + if ( !$options['code'] ) { $options['code'] = $data['code']; } unset( $data['code'], $data['info'] ); - $options['data'] = array_merge( $data['code'], $options['data'] ); + $options['data'] = array_merge( $data, $options['data'] ); } if ( isset( $options['wrap'] ) ) { @@ -175,7 +179,8 @@ class ApiErrorFormatter { } else { $msg = new RawMessage( '$1' ); if ( !isset( $options['code'] ) ) { - $options['code'] = 'internal_api_error_' . get_class( $exception ); + $class = preg_replace( '#^Wikimedia\\\Rdbms\\\#', '', get_class( $exception ) ); + $options['code'] = 'internal_api_error_' . $class; } } $params = [ wfEscapeWikiText( $exception->getMessage() ) ]; @@ -252,7 +257,7 @@ class ApiErrorFormatter { $ret = preg_replace( '!!', '"', $text ); // Strip tags and decode. - $ret = html_entity_decode( strip_tags( $ret ), ENT_QUOTES | ENT_HTML5 ); + $ret = Sanitizer::stripAllTags( $ret ); return $ret; } @@ -413,12 +418,17 @@ class ApiErrorFormatter_BackCompat extends ApiErrorFormatter { if ( $tag === 'error' ) { // In BC mode, only one error - $value = [ - 'code' => $msg->getApiCode(), - 'info' => $value, - ] + $msg->getApiData(); - $this->result->addValue( null, 'error', $value, - ApiResult::OVERRIDE | ApiResult::ADD_ON_TOP | ApiResult::NO_SIZE_CHECK ); + $existingError = $this->result->getResultData( [ 'error' ] ); + if ( !is_array( $existingError ) || + !isset( $existingError['code'] ) || !isset( $existingError['info'] ) + ) { + $value = [ + 'code' => $msg->getApiCode(), + 'info' => $value, + ] + $msg->getApiData(); + $this->result->addValue( null, 'error', $value, + ApiResult::OVERRIDE | ApiResult::ADD_ON_TOP | ApiResult::NO_SIZE_CHECK ); + } } else { if ( $modulePath === null ) { $moduleName = 'unknown';