Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
FailStatusUtilTrait
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
0.00% covered (danger)
0.00%
0 / 1
 exitWithStatus
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
1<?php
2
3declare( strict_types=1 );
4
5namespace MediaWiki\Extension\CampaignEvents\Rest;
6
7use InvalidArgumentException;
8use MediaWiki\Rest\LocalizedHttpException;
9use StatusValue;
10use Wikimedia\Message\MessageValue;
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        throw new LocalizedHttpException( MessageValue::newFromSpecifier( $msgs[0] ), $statusCode );
28    }
29}