Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
47.83% covered (danger)
47.83%
11 / 23
60.00% covered (warning)
60.00%
6 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
WikifunctionCallException
47.83% covered (danger)
47.83%
11 / 23
60.00% covered (warning)
60.00%
6 / 10
37.00
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
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    /**
28     * @param string $msg
29     * @param int $httpStatusCode
30     * @param ?stdClass $zerror
31     * @param array $params
32     */
33    public function __construct(
34        private readonly string $msg,
35        private readonly int $httpStatusCode = HttpStatus::BAD_REQUEST,
36        private readonly ?stdClass $zerror = null,
37        private readonly array $params = []
38    ) {
39        parent::__construct( $this->getMessageObject()->text() );
40    }
41
42    /**
43     * @return Message
44     */
45    public function getMessageObject(): Message {
46        return wfMessage( $this->msg, $this->params );
47    }
48
49    /**
50     * @return string
51     */
52    public function getMessageKey(): string {
53        return $this->msg;
54    }
55
56    /**
57     * @return int
58     */
59    public function getHttpStatusCode(): int {
60        return $this->httpStatusCode;
61    }
62
63    /**
64     * @return stdClass|null
65     */
66    public function getZError() {
67        return $this->zerror;
68    }
69
70    /**
71     * Whether this exception wraps a ZError.
72     *
73     * @return bool
74     */
75    public function hasZError(): bool {
76        return is_object( $this->zerror );
77    }
78
79    /**
80     * If this exception contains a ZError, return 'wikilambda-zerror',
81     * else, return the message key.
82     *
83     * @return string
84     */
85    public function getErrorCode(): string {
86        return $this->hasZError() ? 'wikilambda-zerror' : $this->msg;
87    }
88
89    /**
90     * @return string|null
91     */
92    public function getZErrorType() {
93        return $this->hasZError() ? $this->zerror->Z5K1 : null;
94    }
95
96    /**
97     * Serializes the exception as an associative array.
98     *
99     * @return array
100     */
101    public function toArray(): array {
102        return [
103            'msg' => $this->msg,
104            'httpStatusCode' => $this->httpStatusCode,
105            'zerror' => $this->zerror,
106            'params' => $this->params,
107        ];
108    }
109
110    /**
111     * Static method to build a WikifunctionCallException instance
112     * from a given associative array with the following schema:
113     * [
114     *   'msg' => 'some-error-msg-code',
115     *   'httpStatusCode' => 500,
116     *   'zerror' => [],
117     *   'params' => []
118     * ]
119     *
120     * @param array $data
121     * @return WikifunctionCallException
122     */
123    public static function fromArray( $data ): WikifunctionCallException {
124        $zerror = null;
125
126        if ( $data[ 'zerror' ] !== null ) {
127            // Convert zerror to stdClass
128            $zerror = json_decode( json_encode( $data[ 'zerror' ] ) );
129        }
130
131        return new self(
132            $data[ 'msg' ],
133            $data[ 'httpStatusCode' ] ?? HttpStatus::BAD_REQUEST,
134            $zerror,
135            $data[ 'params' ] ?? []
136        );
137    }
138}