xref: /dokuwiki/lib/plugins/extension/Exception.php (revision 7c184cfca36dde23d8ddd540c4826dfe1f86e2e3)
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        }
25*cf2dcf1bSAndreas Gohr
26*cf2dcf1bSAndreas Gohr        if ($context) {
27*cf2dcf1bSAndreas Gohr            $newmessage = vsprintf($newmessage, $context);
28*cf2dcf1bSAndreas Gohr        }
29*cf2dcf1bSAndreas Gohr
30*cf2dcf1bSAndreas Gohr        parent::__construct($newmessage, 0, $previous);
31*cf2dcf1bSAndreas Gohr    }
32*cf2dcf1bSAndreas Gohr}
33