aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/updater/exception/UpdaterException.php
blob: 20aceccfa73edb97a5144a1596ae6dd20371d410 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php

namespace Shaarli\Updater\Exception;

use Exception;

/**
 * Class UpdaterException.
 */
class UpdaterException extends Exception
{
    /**
     * @var string Method where the error occurred.
     */
    protected $method;

    /**
     * @var Exception The parent exception.
     */
    protected $previous;

    /**
     * Constructor.
     *
     * @param string         $message  Force the error message if set.
     * @param string         $method   Method where the error occurred.
     * @param Exception|bool $previous Parent exception.
     */
    public function __construct($message = '', $method = '', $previous = false)
    {
        $this->method = $method;
        $this->previous = $previous;
        $this->message = $this->buildMessage($message);
    }

    /**
     * Build the exception error message.
     *
     * @param string $message Optional given error message.
     *
     * @return string The built error message.
     */
    private function buildMessage($message)
    {
        $out = '';
        if (!empty($message)) {
            $out .= $message . PHP_EOL;
        }

        if (!empty($this->method)) {
            $out .= t('An error occurred while running the update ') . $this->method . PHP_EOL;
        }

        if (!empty($this->previous)) {
            $out .= '  ' . $this->previous->getMessage();
        }

        return $out;
    }
}