MediaWiki master
RestStatusTrait.php
Go to the documentation of this file.
1<?php
2
4
10
14trait RestStatusTrait {
15
16 private ?Converter $messageValueConverter = null;
17
18 private function getMessageValueConverter(): Converter {
19 if ( !$this->messageValueConverter ) {
20 $this->messageValueConverter = new Converter();
21 }
22 return $this->messageValueConverter;
23 }
24
30 private function convertStatusToMessageValues( StatusValue $status ): array {
31 $conv = $this->getMessageValueConverter();
32 return array_map( static function ( $msg ) use ( $conv ) {
33 // TODO: It should be possible to do this without going through a Message object,
34 // but the internal format of parameters is different in MessageValue (T358779)
35 return $conv->convertMessage( $msg );
36 }, $status->getMessages() );
37 }
38
48 private function throwExceptionForStatus(
49 StatusValue $status,
50 $msg,
51 int $code,
52 array $data = []
53 ) {
54 $data += [ 'error-keys' => $this->getStatusErrorKeys( $status ) ];
55
56 if ( is_string( $msg ) ) {
57 $msg = MessageValue::new( $msg )
58 ->semicolonListParams(
59 $this->convertStatusToMessageValues( $status )
60 );
61 }
62
63 throw new LocalizedHttpException( $msg, $code, $data );
64 }
65
66 private function getStatusErrorKeys( StatusValue $status ) {
67 $keys = [];
68
69 foreach ( $status->getMessages() as $msg ) {
70 $keys[] = $msg->getKey();
71 }
72
73 return array_unique( $keys );
74 }
75
76 private function logStatusError( StatusValue $status, string $message, string $channel ) {
77 LoggerFactory::getInstance( $channel )->error(
78 $message,
79 [ 'reason' => $this->getStatusErrorKeys( $status ) ]
80 );
81 }
82
83}
Create PSR-3 logger objects.
Converter between Message and MessageValue.
Definition Converter.php:18
Generic operation result class Has warning/error list, boolean status and arbitrary value.
getMessages(?string $type=null)
Returns a list of error messages, optionally only those of the given type.
Value object representing a message for i18n.
Copyright (C) 2011-2020 Wikimedia Foundation and others.