Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
55.56% covered (warning)
55.56%
15 / 27
60.00% covered (warning)
60.00%
6 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
WikifunctionCallException
55.56% covered (warning)
55.56%
15 / 27
60.00% covered (warning)
60.00%
6 / 10
27.84
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 getMessageObject
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMessageKey
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getHttpStatusCode
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getZError
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasZError
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getErrorCode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 getZErrorType
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 toArray
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 fromArray
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * WikiLambda extension specialised Exception wrapper for a remote call issue.
4 *
5 * This exception is designed to be used in Client settings, for example:
6 * * On a Client wiki that runs embedded fragments, or
7 * * On Abstract Wiki, who uses ZObjects from Wikifunctions
8 *
9 * While this Exception might contain Z5/Error objects returned by Wikifunctions,
10 * it must not use ZObjectFactory::create methods, as ZObjectFactory is currently
11 * tightly coupled with the assumption that referred objects are stored locally.
12 *
13 * @file
14 * @ingroup Extensions
15 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
16 * @license MIT
17 */
18
19namespace MediaWiki\Extension\WikiLambda;
20
21use Exception;
22use MediaWiki\Message\Message;
23use stdClass;
24
25class WikifunctionCallException extends Exception {
26
27    // Http status code (if applicable)
28    private int $httpStatusCode;
29
30    // Error message code and parameters
31    private string $msg;
32    private array $params;
33
34    // ZError (if applicable)
35    private ?stdClass $zerror;
36
37    /**
38     * @param string $msg
39     * @param int $httpStatusCode
40     * @param ?stdClass $zerror
41     * @param array $params
42     */
43    public function __construct(
44        string $msg,
45        int $httpStatusCode = HttpStatus::BAD_REQUEST,
46        ?stdClass $zerror = null,
47        array $params = []
48    ) {
49        $this->msg = $msg;
50        $this->params = $params;
51
52        $this->httpStatusCode = $httpStatusCode;
53        $this->zerror = $zerror;
54
55        parent::__construct( $this->getMessageObject()->text() );
56    }
57
58    /**
59     * @return Message
60     */
61    public function getMessageObject(): Message {
62        return wfMessage( $this->msg, $this->params );
63    }
64
65    /**
66     * @return string
67     */
68    public function getMessageKey(): string {
69        return $this->msg;
70    }
71
72    /**
73     * @return int
74     */
75    public function getHttpStatusCode(): int {
76        return $this->httpStatusCode;
77    }
78
79    /**
80     * @return stdClass|null
81     */
82    public function getZError() {
83        return $this->zerror;
84    }
85
86    /**
87     * Whether this exception wraps a ZError.
88     *
89     * @return bool
90     */
91    public function hasZError(): bool {
92        return is_object( $this->zerror );
93    }
94
95    /**
96     * If this exception contains a ZError, return 'wikilambda-zerror',
97     * else, return the message key.
98     *
99     * @return string
100     */
101    public function getErrorCode(): string {
102        return $this->hasZError() ? 'wikilambda-zerror' : $this->msg;
103    }
104
105    /**
106     * @return string|null
107     */
108    public function getZErrorType() {
109        return $this->hasZError() ? $this->zerror->Z5K1 : null;
110    }
111
112    /**
113     * Serializes the exception as an associative array.
114     *
115     * @return array
116     */
117    public function toArray(): array {
118        return [
119            'msg' => $this->msg,
120            'httpStatusCode' => $this->httpStatusCode,
121            'zerror' => $this->zerror,
122            'params' => $this->params,
123        ];
124    }
125
126    /**
127     * Static method to build a WikifunctionCallException
128     * instance from a given associative array.
129     *
130     * @param array $data
131     * @return WikifunctionCallException
132     */
133    public static function fromArray( $data ): WikifunctionCallException {
134        $zerror = null;
135
136        if ( $data[ 'zerror' ] !== null ) {
137            // Convert zerror to stdClass
138            $zerror = json_decode( json_encode( $data[ 'zerror' ] ) );
139        }
140
141        return new self(
142            $data[ 'msg' ],
143            $data[ 'httpStatusCode' ] ?? HttpStatus::BAD_REQUEST,
144            $zerror,
145            $data[ 'params' ] ?? []
146        );
147    }
148}