Completed
Push — master ( 39b3fc...3ebb26 )
by Sergey
12:04 queued 07:06
created

RpcError::getErrorByName()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.0342

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 8
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 15
ccs 8
cts 9
cp 0.8889
crap 5.0342
rs 9.6111
1
<?php
2
3
/**
4
 * Created by PhpStorm.
5
 * Project: json-rpc-server
6
 * User: sv
7
 * Date: 05.02.2021
8
 * Time: 23:37
9
 */
10
11
declare(strict_types=1);
12
13
namespace Onnov\JsonRpcServer\Error;
14
15
use Onnov\JsonRpcServer\Definition\RpcErrorDefinition;
16
use Psr\Log\LogLevel;
17
use Throwable;
18
19
/**
20
 * Class ErrorService
21
 * @package Onnov\JsonRpcServer\Service
22
 */
23
class RpcError
24
{
25
    /**
26
     * @var RpcErrorDefinition[]
27
     */
28
    private $errors;
29
30
    /**
31
     * ErrorService constructor.
32
     * @param RpcErrorDefinition $authError
33
     */
34 9
    public function __construct(RpcErrorDefinition $authError)
35
    {
36 9
        $this->errors = [
37 9
            'InvalidAuthorizeException' => $authError,
38 9
            'MethodErrorException'      => new RpcErrorDefinition(
39
                [
40 9
                    'code'       => 600,
41
                    'message'    => 'Error',
42
                    'errorLevel' => LogLevel::NOTICE,
43
                ]
44
            ),
45 9
            'ParseErrorException'       => new RpcErrorDefinition(
46
                [
47 9
                    'code'       => -32700,
48
                    'message'    => 'Parse error',
49
                    'errorLevel' => LogLevel::ERROR,
50
                ]
51
            ),
52 9
            'InvalidRequestException'   => new RpcErrorDefinition(
53
                [
54 9
                    'code'       => -32600,
55
                    'message'    => 'Invalid Request',
56
                    'errorLevel' => LogLevel::ERROR,
57
                ]
58
            ),
59 9
            'MethodNotFoundException'   => new RpcErrorDefinition(
60
                [
61 9
                    'code'       => -32601,
62
                    'message'    => 'Method not found',
63
                    'errorLevel' => LogLevel::ERROR,
64
                ]
65
            ),
66 9
            'InvalidParamsException'    => new RpcErrorDefinition(
67
                [
68 9
                    'code'       => -32602,
69
                    'message'    => 'Invalid params',
70
                    'errorLevel' => LogLevel::ERROR,
71
                ]
72
            ),
73 9
            'Throwable'                 => new RpcErrorDefinition(
74
                [
75 9
                    'code'       => -32603,
76
                    'message'    => 'Internal error',
77
                    'errorLevel' => LogLevel::EMERGENCY,
78
                ]
79
            ),
80
        ];
81 9
    }
82
83
    /**
84
     * @param string $errorName
85
     * @param Throwable|null $throw
86
     * @return RpcErrorDefinition
87
     */
88 4
    public function getErrorByName(string $errorName, Throwable $throw = null): RpcErrorDefinition
89
    {
90 4
        $error = $this->errors['Throwable'];
91 4
        if ($throw !== null && isset($this->errors[$errorName])) {
92 4
            $error = $this->errors[$errorName];
93
94 4
            if ($throw->getCode() !== 0) {
95
                $error->setCode($throw->getCode());
96
            }
97 4
            if ($throw->getMessage() !== '') {
98 1
                $error->setMessage($throw->getMessage());
99
            }
100
        }
101
102 4
        return $error;
103
    }
104
105
    /**
106
     * @param string $errorName
107
     * @param RpcErrorDefinition $error
108
     */
109
    public function addOrReplaceError(string $errorName, RpcErrorDefinition $error): void
110
    {
111
        $this->errors[$errorName] = $error;
112
    }
113
114
    /**
115
     * @return RpcErrorDefinition[]
116
     */
117
    public function getErrors(): array
118
    {
119
        return $this->errors;
120
    }
121
122
    /**
123
     * @param RpcErrorDefinition[] $errors
124
     */
125
    public function setErrors(array $errors): void
126
    {
127
        $this->errors = $errors;
128
    }
129
}
130