forest_admin_datasource_rpc 1.2.1 → 1.3.0
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.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dd4932d629754f42fd137a8d3d85360c85f2b0c76a0dfcb97427a634c9468a79
|
4
|
+
data.tar.gz: a19fc99287514fea329de2ed9fa5aa9612080f1f5de431dd6cde80527f7045b8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bc2dd853f80b41569484b14b3b2a6dba697099630f9bf9eab72612948a459c766f1d6a10ffdddb03f166cae1059fdccec92a9b6ee07ac34836475cd4067f1444
|
7
|
+
data.tar.gz: 22ea9393683dc7392668ec1da4f3bbdc7c884cc7ba0c17d8ef1431ce57affd4e0e1cdc2017940ee52e58207cdaae0308bf4c52b1341a44fc1ed42b98a7620f2f
|
@@ -6,6 +6,28 @@ require 'time'
|
|
6
6
|
module ForestAdminDatasourceRpc
|
7
7
|
module Utils
|
8
8
|
class RpcClient
|
9
|
+
# RpcClient handles HTTP communication with the RPC Agent.
|
10
|
+
#
|
11
|
+
# Error Handling:
|
12
|
+
# When the RPC agent returns an error, this client automatically maps HTTP status codes
|
13
|
+
# to appropriate Forest Admin exception types. This ensures business errors from the
|
14
|
+
# RPC agent are properly propagated to the datasource_rpc.
|
15
|
+
#
|
16
|
+
# To add support for a new error type:
|
17
|
+
# 1. Add the status code and exception class to ERROR_STATUS_MAP
|
18
|
+
# 2. (Optional) Add a default message to generate_default_message method
|
19
|
+
# 3. Tests will automatically cover the new mapping
|
20
|
+
|
21
|
+
# Map HTTP status codes to Forest Admin exception classes
|
22
|
+
ERROR_STATUS_MAP = {
|
23
|
+
400 => ForestAdminAgent::Http::Exceptions::ValidationError,
|
24
|
+
401 => ForestAdminAgent::Http::Exceptions::AuthenticationOpenIdClient,
|
25
|
+
403 => ForestAdminAgent::Http::Exceptions::ForbiddenError,
|
26
|
+
404 => ForestAdminAgent::Http::Exceptions::NotFoundError,
|
27
|
+
409 => ForestAdminAgent::Http::Exceptions::ConflictError,
|
28
|
+
422 => ForestAdminAgent::Http::Exceptions::UnprocessableError
|
29
|
+
}.freeze
|
30
|
+
|
9
31
|
def initialize(api_url, auth_secret)
|
10
32
|
@api_url = api_url
|
11
33
|
@auth_secret = auth_secret
|
@@ -40,12 +62,70 @@ module ForestAdminDatasourceRpc
|
|
40
62
|
end
|
41
63
|
|
42
64
|
def handle_response(response)
|
43
|
-
unless response.success?
|
65
|
+
raise_appropriate_error(response) unless response.success?
|
66
|
+
|
67
|
+
response.body
|
68
|
+
end
|
69
|
+
|
70
|
+
def raise_appropriate_error(response)
|
71
|
+
error_body = parse_error_body(response)
|
72
|
+
status = response.status
|
73
|
+
url = response.env.url
|
74
|
+
message = error_body[:message] || generate_default_message(status, url)
|
75
|
+
|
76
|
+
exception_class = ERROR_STATUS_MAP[status]
|
77
|
+
|
78
|
+
if exception_class
|
79
|
+
raise exception_class, message
|
80
|
+
elsif status >= 500
|
81
|
+
raise ForestAdminDatasourceToolkit::Exceptions::ForestException,
|
82
|
+
"Server Error: #{message}"
|
83
|
+
else
|
44
84
|
raise ForestAdminDatasourceToolkit::Exceptions::ForestException,
|
45
|
-
"RPC request failed: #{
|
85
|
+
"RPC request failed: #{status} - #{message}"
|
46
86
|
end
|
87
|
+
end
|
47
88
|
|
48
|
-
|
89
|
+
def generate_default_message(status, url)
|
90
|
+
default_messages = {
|
91
|
+
400 => "Bad Request: #{url}",
|
92
|
+
401 => "Unauthorized: #{url}",
|
93
|
+
403 => "Forbidden: #{url}",
|
94
|
+
404 => "Not Found: #{url}",
|
95
|
+
409 => "Conflict: #{url}",
|
96
|
+
422 => "Unprocessable Entity: #{url}"
|
97
|
+
}
|
98
|
+
|
99
|
+
default_messages[status] || "Unknown error (#{url})"
|
100
|
+
end
|
101
|
+
|
102
|
+
def parse_error_body(response)
|
103
|
+
body = response.body
|
104
|
+
|
105
|
+
# If body is already a hash (Faraday parsed it as JSON)
|
106
|
+
return symbolize_error_keys(body) if body.is_a?(Hash)
|
107
|
+
|
108
|
+
# Try to parse as JSON if it's a string
|
109
|
+
if body.is_a?(String) && !body.empty?
|
110
|
+
begin
|
111
|
+
parsed = JSON.parse(body)
|
112
|
+
return symbolize_error_keys(parsed)
|
113
|
+
rescue JSON::ParserError
|
114
|
+
# If parsing fails, return the body as the message
|
115
|
+
return { message: body }
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
# Fallback for empty or unexpected body types
|
120
|
+
{ message: 'Unknown error' }
|
121
|
+
end
|
122
|
+
|
123
|
+
def symbolize_error_keys(hash)
|
124
|
+
{
|
125
|
+
message: hash['error'] || hash['message'] || hash[:error] || hash[:message],
|
126
|
+
errors: hash['errors'] || hash[:errors],
|
127
|
+
name: hash['name'] || hash[:name]
|
128
|
+
}.compact
|
49
129
|
end
|
50
130
|
end
|
51
131
|
end
|