xref: /dokuwiki/lib/plugins/extension/Exception.php (revision cf2dcf1b9ac1f331d4667a6c82d326f1a3e5d4c7)
1<?php
2
3namespace dokuwiki\plugin\extension;
4
5use Throwable;
6
7/**
8 * Implements translatable exception messages
9 */
10class Exception extends \Exception
11{
12    /**
13     * @param string $message The error message or language string
14     * @param array $context array of sprintf variables to be replaced in the message
15     * @param Throwable|null $previous Previous exception
16     */
17    public function __construct($message = "", $context = [], Throwable $previous = null)
18    {
19        // try to translate the message
20        $helper = plugin_load('helper', 'extension');
21        $newmessage = $helper->getLang($message);
22        if ($newmessage === '') {
23            $newmessage = $message;
24        } else {
25            // add original language string so we still recognize it when reported by foreign users
26            $newmessage .= ' [' . $message . ']';
27        }
28
29        if ($context) {
30            $newmessage = vsprintf($newmessage, $context);
31        }
32
33        parent::__construct($newmessage, 0, $previous);
34    }
35
36}
37