jsonrpc2.0 0.1.9 → 0.1.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/jsonrpc2.0.gemspec +1 -1
- data/lib/jsonrpc2.0/Client.rb +5 -2
- data/lib/jsonrpc2.0/Errors.rb +29 -10
- data/lib/jsonrpc2.0/Server.rb +19 -12
- metadata +2 -2
data/jsonrpc2.0.gemspec
CHANGED
data/lib/jsonrpc2.0/Client.rb
CHANGED
@@ -21,10 +21,13 @@ class JsonRpcClient
|
|
21
21
|
response = send_request request.to_json
|
22
22
|
response = JSON.parse response
|
23
23
|
|
24
|
-
|
24
|
+
begin
|
25
|
+
raise JsonRpcErrors::error_from_hash(response['error'], true) if response.key?('error')
|
26
|
+
rescue ApplicationError => e
|
27
|
+
raise JsonRpcErrors::error_from_hash(e.data)
|
28
|
+
end
|
25
29
|
|
26
30
|
result = response['result']
|
27
|
-
result
|
28
31
|
end
|
29
32
|
|
30
33
|
def fire_notification(inMethod, *inParams)
|
data/lib/jsonrpc2.0/Errors.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
require 'json'
|
2
2
|
|
3
3
|
module JsonRpcErrors
|
4
|
-
|
4
|
+
ImplementationErrors = {'ParseError' => -32700, 'InvalidRequest' => -32600, 'MethodNotFound' => -32601, 'InvalidParams' => -32602, 'InternalError' => -32603, 'ApplicationError' => -32550}
|
5
|
+
ApplicationErrors = {}
|
5
6
|
|
6
|
-
class
|
7
|
+
class BaseError < StandardError
|
7
8
|
CODE = nil
|
8
9
|
|
9
10
|
attr_reader :data
|
@@ -26,19 +27,37 @@ module JsonRpcErrors
|
|
26
27
|
end
|
27
28
|
end
|
28
29
|
|
29
|
-
|
30
|
-
c = self.class.const_get ERRORS.key(inHash['code'])
|
31
|
-
c.new(inHash['message'], inHash['data'])
|
30
|
+
class BaseImplementationError < BaseError
|
32
31
|
end
|
33
32
|
|
34
|
-
|
35
|
-
|
36
|
-
|
33
|
+
class BaseApplicationError < BaseError
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.error_from_hash(inHash, inIsImplementationError = false)
|
37
|
+
if inIsImplementationError
|
38
|
+
errorList = ImplementationErrors
|
39
|
+
else
|
40
|
+
errorList = ApplicationErrors
|
41
|
+
end
|
42
|
+
|
43
|
+
errorName = errorList.key(inHash['code'])
|
44
|
+
errorClass = const_get(errorName)
|
45
|
+
errorClass.new(inHash['message'], inHash['data'])
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.add_application_errors(inErrors)
|
49
|
+
ApplicationErrors.merge!(inErrors)
|
50
|
+
create_error_classes(inErrors, BaseApplicationError)
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.create_error_classes(inErrors, inBaseClass)
|
54
|
+
inErrors.each do |errorName, errorCode|
|
55
|
+
errorClass = Class::new(inBaseClass) do
|
37
56
|
const_set('CODE', errorCode)
|
38
57
|
end
|
39
|
-
const_set(
|
58
|
+
const_set(errorName, errorClass)
|
40
59
|
end
|
41
60
|
end
|
42
61
|
|
43
|
-
|
62
|
+
create_error_classes(ImplementationErrors, BaseImplementationError)
|
44
63
|
end
|
data/lib/jsonrpc2.0/Server.rb
CHANGED
@@ -20,7 +20,7 @@ class JsonRpcServer
|
|
20
20
|
else
|
21
21
|
response = handle_request(message).to_json
|
22
22
|
end
|
23
|
-
rescue
|
23
|
+
rescue BaseImplementationError => e
|
24
24
|
id = nil
|
25
25
|
id = message['id'] if (message.class == Hash)
|
26
26
|
error = create_message(id)
|
@@ -39,19 +39,26 @@ class JsonRpcServer
|
|
39
39
|
raise InvalidRequest if (inRequest['jsonrpc'] != VERSION)
|
40
40
|
raise InvalidRequest if (!inRequest.key?('method'))
|
41
41
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
42
|
+
methodName = 'r_' + inRequest['method']
|
43
|
+
|
44
|
+
unless self.class.private_method_defined?(methodName)
|
45
|
+
raise MethodNotFound
|
46
|
+
end
|
47
|
+
|
48
|
+
method = method(methodName)
|
46
49
|
|
47
|
-
|
50
|
+
params = inRequest['params']
|
51
|
+
paramCount = 0
|
52
|
+
paramCount = params.count if params.respond_to?("count")
|
53
|
+
|
54
|
+
unless method.arity == paramCount
|
55
|
+
raise InvalidParams
|
56
|
+
end
|
57
|
+
|
58
|
+
begin
|
48
59
|
result = method.call(*params)
|
49
|
-
rescue
|
50
|
-
raise(
|
51
|
-
rescue ArgumentError => e
|
52
|
-
raise(InvalidParams, e.message)
|
53
|
-
rescue StandardError => e
|
54
|
-
raise(InternalError, e.message)
|
60
|
+
rescue BaseApplicationError => e
|
61
|
+
raise ApplicationError.new("", e)
|
55
62
|
end
|
56
63
|
|
57
64
|
if inRequest.key?('id')
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonrpc2.0
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.18
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-
|
12
|
+
date: 2011-07-07 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
14
|
description:
|
15
15
|
email: dev@coldsun.org
|