Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
26.32% covered (danger)
26.32%
5 / 19
33.33% covered (danger)
33.33%
2 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
MWException
27.78% covered (danger)
27.78%
5 / 18
33.33% covered (danger)
33.33%
2 / 6
66.25
0.00% covered (danger)
0.00%
0 / 1
 isLoggable
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 useMessageCache
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
4.25
 msg
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
20
 report
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 hasOverriddenHandler
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isCommandLine
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 */
6
7namespace MediaWiki\Exception;
8
9use Exception;
10use MediaWiki\Debug\MWDebug;
11use MediaWiki\Language\LocalisationCache;
12use Wikimedia\Message\MessageParam;
13use Wikimedia\Message\MessageSpecifier;
14
15/**
16 * MediaWiki exception
17 *
18 * @newable
19 * @stable to extend
20 *
21 * @ingroup Exception
22 * @deprecated since 1.40, use native exceptions instead (either directly, or defining subclasses when appropriate)
23 */
24class MWException extends Exception {
25
26    /**
27     * Whether to log this exception in the exception debug log.
28     *
29     * @stable to override
30     *
31     * @since 1.23
32     * @return bool
33     */
34    public function isLoggable() {
35        return true;
36    }
37
38    /**
39     * Can the extension use the Message class/wfMessage to get i18n-ed messages?
40     *
41     * @stable to override
42     *
43     * @return bool
44     */
45    public function useMessageCache() {
46        foreach ( $this->getTrace() as $frame ) {
47            if ( isset( $frame['class'] ) && $frame['class'] === LocalisationCache::class ) {
48                return false;
49            }
50        }
51        return true;
52    }
53
54    /**
55     * Get a message from i18n
56     *
57     * @param string $key Message name
58     * @param string $fallback Default message if the message cache can't be
59     *                  called by the exception
60     * @phpcs:ignore Generic.Files.LineLength
61     * @param MessageParam|MessageSpecifier|string|int|float|list<MessageParam|MessageSpecifier|string|int|float> ...$params
62     *   See Message::params()
63     * @return string Message with arguments replaced
64     */
65    public function msg( $key, $fallback, ...$params ) {
66        // NOTE: Keep logic in sync with MWExceptionRenderer::msg.
67        $res = false;
68        if ( $this->useMessageCache() ) {
69            try {
70                $res = wfMessage( $key, ...$params )->text();
71            } catch ( Exception ) {
72            }
73        }
74        if ( $res === false ) {
75            // Fallback to static message text and generic sitename.
76            // Avoid live config as this must work before Setup/MediaWikiServices finish.
77            $res = wfMsgReplaceArgs( $fallback, $params );
78            $res = strtr( $res, [
79                '{{SITENAME}}' => 'MediaWiki',
80            ] );
81        }
82        return $res;
83    }
84
85    /**
86     * Output a report about the exception and takes care of formatting.
87     * It will be either HTML or plain text based on isCommandLine().
88     *
89     * @stable to override
90     */
91    public function report() {
92        // May be overridden by subclasses to replace the whole error page
93        MWExceptionRenderer::output( $this, MWExceptionRenderer::AS_PRETTY );
94    }
95
96    /**
97     * @internal
98     */
99    final public function hasOverriddenHandler(): bool {
100        return MWDebug::detectDeprecatedOverride( $this, __CLASS__, 'report' );
101    }
102
103    /**
104     * Check whether we are in command line mode or not to report the exception
105     * in the correct format.
106     *
107     * @return bool
108     */
109    public static function isCommandLine() {
110        return MW_ENTRY_POINT === 'cli';
111    }
112
113}
114
115/** @deprecated class alias since 1.44 */
116class_alias( MWException::class, 'MWException' );