aboutsummaryrefslogtreecommitdiff
path: root/lib/error.php
blob: f9950ceac1ba582ffed59adc5b63e1f610d08fe1 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php

/**
 * This file is part of RSS-Bridge, a PHP project capable of generating RSS and
 * Atom feeds for websites that don't have one.
 *
 * For the full license information, please view the UNLICENSE file distributed
 * with this source code.
 *
 * @package Core
 * @license http://unlicense.org/ UNLICENSE
 * @link    https://github.com/rss-bridge/rss-bridge
 */

/**
 * Throws an exception when called.
 *
 * @throws \Exception when called
 * @param string $message The error message
 * @param int $code The HTTP error code
 * @link https://en.wikipedia.org/wiki/List_of_HTTP_status_codes List of HTTP
 * status codes
 */
function returnError($message, $code)
{
    throw new \Exception($message, $code);
}

/**
 * Returns HTTP Error 400 (Bad Request) when called.
 *
 * @param string $message The error message
 */
function returnClientError($message)
{
    returnError($message, 400);
}

/**
 * Returns HTTP Error 500 (Internal Server Error) when called.
 *
 * @param string $message The error message
 */
function returnServerError($message)
{
    returnError($message, 500);
}

/**
 * Stores bridge-specific errors in a cache file.
 *
 * @param string $bridgeName The name of the bridge that failed.
 * @param int $code The error code
 *
 * @return int The total number the same error has appeared
 */
function logBridgeError($bridgeName, $code)
{
    $cacheFac = new CacheFactory();

    $cache = $cacheFac->create(Configuration::getConfig('cache', 'type'));
    $cache->setScope('error_reporting');
    $cache->setkey($bridgeName . '_' . $code);
    $cache->purgeCache(86400); // 24 hours

    if ($report = $cache->loadData()) {
        $report = json_decode($report, true);
        $report['time'] = time();
        $report['count']++;
    } else {
        $report = [
            'error' => $code,
            'time' => time(),
            'count' => 1,
        ];
    }

    $cache->saveData(json_encode($report));

    return $report['count'];
}