6use InvalidArgumentException;
18 private const CT_HTML =
'text/html; charset=utf-8';
19 private const CT_JSON =
'application/json';
22 private $textFormatters;
25 private $showExceptionDetails =
false;
31 $this->textFormatters = $textFormatters;
42 $this->showExceptionDetails = $showExceptionDetails;
53 $json = json_encode( $value,
54 JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE );
55 if ( $json ===
false ) {
77 public function createJson( $value, $contentType =
null ) {
78 $contentType ??= self::CT_JSON;
79 $response =
new Response( $this->encodeJson( $value ) );
80 $response->setHeader(
'Content-Type', $contentType );
96 $response->setStatus( 204 );
110 $response = $this->createRedirectBase( $target );
111 $response->setStatus( 301 );
126 $response = $this->createRedirectBase( $target );
127 $response->setStatus( 302 );
140 $response = $this->createRedirectBase( $target );
141 $response->setStatus( 307 );
154 $response = $this->createRedirectBase( $target );
155 $response->setStatus( 303 );
171 $response->setStatus( 304 );
183 if ( $errorCode < 400 || $errorCode >= 600 ) {
184 throw new InvalidArgumentException(
'error code must be 4xx or 5xx' );
186 $response = $this->createJson( $bodyData + [
187 'httpCode' => $errorCode,
188 'httpReason' => HttpStatus::getMessage( $errorCode )
191 $response->setStatus( $errorCode );
207 array $extraData = []
209 return $this->createHttpError(
211 array_merge( $extraData, $this->formatMessage( $messageValue ) )
222 $response = $this->createLocalizedHttpError(
223 $exception->getCode(),
224 $exception->getMessageValue(),
225 $exception->getErrorData() + [
226 'errorKey' => $exception->getErrorKey(),
230 return $exception->getResponse();
232 $response = $this->createRedirectBase( $exception->getTarget() );
233 $response->setStatus( $exception->getCode() );
235 if ( in_array( $exception->getCode(), [ 204, 304 ],
true ) ) {
236 $response = $this->create();
237 $response->setStatus( $exception->getCode() );
239 $response = $this->createHttpError(
240 $exception->getCode(),
242 [
'message' => $exception->getMessage() ],
243 $exception->getErrorData()
247 } elseif ( $this->showExceptionDetails ) {
248 $response = $this->createHttpError( 500, [
249 'message' =>
'Error: exception of type ' . get_class( $exception ) .
': '
250 . $exception->getMessage(),
251 'exception' => MWExceptionHandler::getStructuredExceptionData(
253 MWExceptionHandler::CAUGHT_BY_OTHER
259 $response = $this->createHttpError( 500, [
260 'message' =>
'Error: exception of type ' . get_class( $exception ),
274 $originalValue = $value;
275 if ( is_scalar( $value ) ) {
276 $data = [
'value' => $value ];
277 } elseif ( is_array( $value ) || $value instanceof stdClass ) {
280 $type = gettype( $originalValue );
281 if (
$type ===
'object' ) {
282 $type = get_class( $originalValue );
284 throw new InvalidArgumentException( __METHOD__ .
": Invalid return value type $type" );
286 $response = $this->createJson( $data );
296 $response =
new Response( $this->getHyperLink( $target ) );
297 $response->setHeader(
'Content-Type', self::CT_HTML );
298 $response->setHeader(
'Location', $target );
309 $url = htmlspecialchars( $url, ENT_COMPAT );
310 return "<!doctype html><title>Redirect</title><a href=\"$url\">$url</a>";
314 if ( !$this->textFormatters ) {
319 foreach ( $this->textFormatters as $formatter ) {
320 $lang = LanguageCode::bcp47( $formatter->getLangCode() );
321 $messageText = $formatter->format( $messageValue );
322 $translations[
$lang] = $messageText;
324 return [
'messageTranslations' => $translations ];
Methods for dealing with language codes.
Handler class for MWExceptions.
if(!isset( $args[0])) $lang