aboutsummaryrefslogtreecommitdiffstats
path: root/includes/Rest/LocalizedHttpException.php
blob: 1980838c529b895d69cfc2e26c2bb2ee7ac3bba6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php

namespace MediaWiki\Rest;

use Wikimedia\Message\DataMessageValue;
use Wikimedia\Message\MessageValue;

/**
 * @newable
 */
class LocalizedHttpException extends HttpException {
	private MessageValue $messageValue;
	private string $errorKey;

	/**
	 * @stable to call
	 *
	 * @param MessageValue $messageValue
	 * @param int $code
	 * @param array $errorData
	 */
	public function __construct( MessageValue $messageValue, $code = 500, $errorData = [] ) {
		if ( $messageValue instanceof DataMessageValue ) {
			$errorKey = $messageValue->getCode();
			$errorData += $messageValue->getData() ?? [];
		} else {
			$errorKey = $messageValue->getKey();
		}
		parent::__construct(
			'Localized exception with key ' . $messageValue->getKey(), $code, $errorData
		);
		$this->messageValue = $messageValue;
		$this->errorKey = $errorKey;
	}

	public function getMessageValue(): MessageValue {
		return $this->messageValue;
	}

	public function getErrorKey(): string {
		return $this->errorKey;
	}
}