Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
FailStatusUtilTrait
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
2.03
0.00% covered (danger)
0.00%
0 / 1
 exitWithStatus
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
2.03
1<?php
2
3declare( strict_types=1 );
4
5namespace MediaWiki\Extension\CampaignEvents\Rest;
6
7use InvalidArgumentException;
8use MediaWiki\Message\Converter;
9use MediaWiki\Rest\LocalizedHttpException;
10use StatusValue;
11
12/**
13 * Helper to exit in case of a fatal StatusValue.
14 */
15trait FailStatusUtilTrait {
16    /**
17     * @param StatusValue $status
18     * @param int $statusCode
19     * @return never
20     */
21    private function exitWithStatus( StatusValue $status, int $statusCode = 400 ): void {
22        $msgs = $status->getMessages();
23        if ( !$msgs ) {
24            throw new InvalidArgumentException( "Got status without errors" );
25        }
26        // TODO Report all errors, not just the first one.
27        $converter = new Converter();
28        throw new LocalizedHttpException( $converter->convertMessage( $msgs[0] ), $statusCode );
29    }
30}