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