forest_admin_datasource_rpc 1.2.0 → 1.2.2

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: fec7958398b22314b068fdebe6fe93130a50d5d039766cf1c7a757b8c1491236
4
- data.tar.gz: 3c351dea49b456ba4f73042c4f6c4c971bed51a9831323f5bf8785589d38b03e
3
+ metadata.gz: 44bec686335e416ffbe42844a28f69965e0c547f18e0310acd5e9675d9774e01
4
+ data.tar.gz: 568b5ba9225551088ba5b84c08e9662e9a3e6cc609bc0f3598bfe54cb3e8b4be
5
5
  SHA512:
6
- metadata.gz: 4af07a7694001036f6e88373378d31b2e0ca66706f11f526d054484058d0b68c7c6d9e1b4635461ff805b4a52866495a777c94632fd757e770e9eb27f2b652bb
7
- data.tar.gz: f2909cc00aeb3503030ff48a386aa61221dd57cc2482bc93c9918b70e283e9cc948763de774b42d400170fcc1e1e5508a6f13ac0d12a19d06532426bfa36afb1
6
+ metadata.gz: 8aa0521071631fb89f7ce39b7ee335777190abac1109a257d1fe1da07abd6996589a3cba2ee338fbe5afb36adb3ae8d73669716280e4a97f8c8da4c1c45d84ba
7
+ data.tar.gz: 6c97dcb89283d849efee051a0a3a142a68a12dc13d16a4814575269e44e71b6d364c2bdc79e61012186ad188cc6bf11b2f3b3c2ffada872188acf274605b520c
@@ -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: #{response.status} for uri #{response.env.url}"
85
+ "RPC request failed: #{status} - #{message}"
46
86
  end
87
+ end
47
88
 
48
- response.body
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
@@ -1,3 +1,3 @@
1
1
  module ForestAdminDatasourceRpc
2
- VERSION = "1.2.0"
2
+ VERSION = "1.2.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: forest_admin_datasource_rpc
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthieu
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2025-10-02 00:00:00.000000000 Z
12
+ date: 2025-10-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday