6use InvalidArgumentException;
18 private const CT_PLAIN =
'text/plain; charset=utf-8';
19 private const CT_HTML =
'text/html; charset=utf-8';
20 private const CT_JSON =
'application/json';
23 private $textFormatters;
26 private $showExceptionDetails =
false;
32 $this->textFormatters = $textFormatters;
43 $this->showExceptionDetails = $showExceptionDetails;
54 $json = json_encode( $value,
55 JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE );
56 if ( $json ===
false ) {
78 public function createJson( $value, $contentType =
null ) {
79 $contentType = $contentType ?? self::CT_JSON;
80 $response =
new Response( $this->encodeJson( $value ) );
81 $response->setHeader(
'Content-Type', $contentType );
97 $response->setStatus( 204 );
111 $response = $this->createRedirectBase( $target );
112 $response->setStatus( 301 );
127 $response = $this->createRedirectBase( $target );
128 $response->setStatus( 302 );
141 $response = $this->createRedirectBase( $target );
142 $response->setStatus( 307 );
155 $response = $this->createRedirectBase( $target );
156 $response->setStatus( 303 );
172 $response->setStatus( 304 );
184 if ( $errorCode < 400 || $errorCode >= 600 ) {
185 throw new InvalidArgumentException(
'error code must be 4xx or 5xx' );
187 $response = $this->createJson( $bodyData + [
188 'httpCode' => $errorCode,
189 'httpReason' => HttpStatus::getMessage( $errorCode )
192 $response->setStatus( $errorCode );
208 array $extraData = []
210 return $this->createHttpError(
212 array_merge( $extraData, $this->formatMessage( $messageValue ) )
223 $response = $this->createLocalizedHttpError(
224 $exception->getCode(),
225 $exception->getMessageValue(),
226 (array)$exception->getErrorData()
229 return $exception->getResponse();
231 $response = $this->createRedirectBase( $exception->getTarget() );
232 $response->setStatus( $exception->getCode() );
234 if ( in_array( $exception->getCode(), [ 204, 304 ],
true ) ) {
235 $response = $this->create();
236 $response->setStatus( $exception->getCode() );
238 $response = $this->createHttpError(
239 $exception->getCode(),
241 [
'message' => $exception->getMessage() ],
242 (array)$exception->getErrorData()
246 } elseif ( $this->showExceptionDetails ) {
247 $response = $this->createHttpError( 500, [
248 'message' =>
'Error: exception of type ' . get_class( $exception ) .
': '
249 . $exception->getMessage(),
250 'exception' => MWExceptionHandler::getStructuredExceptionData(
252 MWExceptionHandler::CAUGHT_BY_OTHER
258 $response = $this->createHttpError( 500, [
259 'message' =>
'Error: exception of type ' . get_class( $exception ),
273 $originalValue = $value;
274 if ( is_scalar( $value ) ) {
275 $data = [
'value' => $value ];
276 } elseif ( is_array( $value ) || $value instanceof stdClass ) {
279 $type = gettype( $originalValue );
280 if (
$type ===
'object' ) {
281 $type = get_class( $originalValue );
283 throw new InvalidArgumentException( __METHOD__ .
": Invalid return value type $type" );
285 $response = $this->createJson( $data );
295 $response =
new Response( $this->getHyperLink( $target ) );
296 $response->setHeader(
'Content-Type', self::CT_HTML );
297 $response->setHeader(
'Location', $target );
308 $url = htmlspecialchars( $url, ENT_COMPAT );
309 return "<!doctype html><title>Redirect</title><a href=\"$url\">$url</a>";
313 if ( !$this->textFormatters ) {
318 foreach ( $this->textFormatters as $formatter ) {
319 $lang = LanguageCode::bcp47( $formatter->getLangCode() );
320 $messageText = $formatter->format( $messageValue );
321 $translations[
$lang] = $messageText;
323 return [
'messageTranslations' => $translations ];
Methods for dealing with language codes.
Handler class for MWExceptions.
if(!isset( $args[0])) $lang