/
RPC.pm
101 lines (72 loc) · 2.64 KB
/
RPC.pm
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# JSON-PRC 2.0 spec defines Error Object in chapter 5.1
# derived from http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php
role X::JSON::RPC is Exception {
# SPEC: When a rpc call encounters an error,
# the Response Object MUST contain the error member
# with a value that is a Object with the following members:
# SPEC: A Number that indicates the error type that occurred.
# This MUST be an integer.
has Int $.code is rw;
# SPEC: A String providing a short description of the error.
# The message SHOULD be limited to a concise single sentence.
has Str $.message is rw;
# SPEC: A Primitive or Structured value that contains additional information about the error.
# This may be omitted.
has Any $.data is rw;
# stringify output for debug purposes.
method Str ( ) {
my $error = $.message ~ ' (' ~ $.code ~ ')';
$error ~= ': ' ~ $.data.perl if $.data.defined;
return $error;
}
# make response error member for serving purposes.
method Hash {
my %error = (
'code' => $.code,
'message' => $.message,
);
%error{'data'} = $.data if $.data.defined;
return %error;
}
# make gist output for console printing purposes
method gist ( ) {
return self.Str;
}
}
# invalid JSON was received by the server.
# an error occurred on the server while parsing the JSON text.
class X::JSON::RPC::ParseError does X::JSON::RPC {
method new ( :$data ) {
self.bless( *, code => -32700, message => 'Parse error.', data => $data );
}
}
# the JSON sent is not a valid Request object
class X::JSON::RPC::InvalidRequest does X::JSON::RPC {
method new ( :$data ) {
self.bless( *, code => -32600, message => 'Invalid Request.', data => $data );
}
}
# the method does not exist / is not available
class X::JSON::RPC::MethodNotFound does X::JSON::RPC {
method new ( :$data ) {
self.bless( *, code => -32601, message => 'Method not found.', data => $data );
}
}
# invalid method parameter(s)
class X::JSON::RPC::InvalidParams does X::JSON::RPC {
method new ( :$data ) {
self.bless( *, code => -32602, message => 'Invalid params.', data => $data );
}
}
# internal JSON-RPC error
class X::JSON::RPC::InternalError does X::JSON::RPC {
method new ( :$data ) {
self.bless( *, code => -32603, message => 'Internal error.', data => $data );
}
}
# protocol error
class X::JSON::RPC::ProtocolError does X::JSON::RPC {
method new ( :$message, :$data ) {
self.bless( *, code => -32000, message => $message // 'Protocol Error.', data => $data );
}
}