Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
69.23% |
9 / 13 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| RestStatusTrait | |
69.23% |
9 / 13 |
|
66.67% |
2 / 3 |
5.73 | |
0.00% |
0 / 1 |
| throwExceptionForStatus | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| getStatusErrorKeys | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| logStatusError | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Rest\Handler\Helper; |
| 4 | |
| 5 | use MediaWiki\Logger\LoggerFactory; |
| 6 | use MediaWiki\Rest\LocalizedHttpException; |
| 7 | use StatusValue; |
| 8 | use Wikimedia\Message\MessageValue; |
| 9 | |
| 10 | /** |
| 11 | * Trait for handling Status objects in REST handlers. |
| 12 | */ |
| 13 | trait RestStatusTrait { |
| 14 | |
| 15 | /** |
| 16 | * @param StatusValue $status |
| 17 | * @param string|MessageValue $msg |
| 18 | * @param int $code |
| 19 | * @param array $data |
| 20 | * |
| 21 | * @return never |
| 22 | * @throws LocalizedHttpException |
| 23 | */ |
| 24 | private function throwExceptionForStatus( |
| 25 | StatusValue $status, |
| 26 | $msg, |
| 27 | int $code, |
| 28 | array $data = [] |
| 29 | ) { |
| 30 | $data += [ 'error-keys' => $this->getStatusErrorKeys( $status ) ]; |
| 31 | |
| 32 | if ( is_string( $msg ) ) { |
| 33 | $msg = MessageValue::new( $msg ) |
| 34 | ->semicolonListParams( $status->getMessages() ); |
| 35 | } |
| 36 | |
| 37 | throw new LocalizedHttpException( $msg, $code, $data ); |
| 38 | } |
| 39 | |
| 40 | private function getStatusErrorKeys( StatusValue $status ): array { |
| 41 | $keys = []; |
| 42 | |
| 43 | foreach ( $status->getMessages() as $msg ) { |
| 44 | $keys[] = $msg->getKey(); |
| 45 | } |
| 46 | |
| 47 | return array_unique( $keys ); |
| 48 | } |
| 49 | |
| 50 | private function logStatusError( StatusValue $status, string $message, string $channel ) { |
| 51 | LoggerFactory::getInstance( $channel )->error( |
| 52 | $message, |
| 53 | [ 'reason' => $this->getStatusErrorKeys( $status ) ] |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | } |