Skip to content

Commit 7f171e2

Browse files
committed
feat: Add PROVIDER_FATAL error code to ProviderInitializationError
Per reviewer feedback, add OpenFeature error code support to the ProviderInitializationError exception class: - Add error_code attribute with PROVIDER_FATAL default per OpenFeature spec - Support custom error codes via constructor parameter - Update all tests to verify error_code is properly set - Update documentation to show error_code usage in exception handling Addresses PR feedback from @beeme1mr regarding OpenFeature specification requirement for provider initialization error codes. Signed-off-by: Leo Romanovsky <[email protected]>
1 parent fa296fa commit 7f171e2

File tree

5 files changed

+34
-4
lines changed

5 files changed

+34
-4
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ begin
141141
puts "Provider is ready!"
142142
rescue OpenFeature::SDK::ProviderInitializationError => e
143143
puts "Provider failed to initialize: #{e.message}"
144+
puts "Error code: #{e.error_code}"
144145
puts "Original error: #{e.original_error}"
145146
end
146147

@@ -162,8 +163,8 @@ end
162163

163164
The `set_provider_and_wait` method:
164165
- Waits for the provider's `init` method to complete successfully
165-
- Raises `ProviderInitializationError` if initialization fails or times out
166-
- Provides access to the original error and provider instance for debugging
166+
- Raises `ProviderInitializationError` with `PROVIDER_FATAL` error code if initialization fails or times out
167+
- Provides access to the original error, provider instance, and error code for debugging
167168
- Uses the same thread-safe provider switching as `set_provider`
168169

169170
In some situations, it may be beneficial to register multiple providers in the same application.

lib/open_feature/sdk/provider_initialization_error.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require_relative "provider/error_code"
4+
35
module OpenFeature
46
module SDK
57
# Exception raised when a provider fails to initialize during setProviderAndWait
@@ -13,13 +15,18 @@ class ProviderInitializationError < StandardError
1315
# @return [Exception] the original error that caused the initialization failure
1416
attr_reader :original_error
1517

18+
# @return [String] the OpenFeature error code
19+
attr_reader :error_code
20+
1621
# @param message [String] the error message
1722
# @param provider [Object] the provider that failed to initialize
1823
# @param original_error [Exception] the original error that caused the failure
19-
def initialize(message, provider: nil, original_error: nil)
24+
# @param error_code [String] the OpenFeature error code (defaults to PROVIDER_FATAL)
25+
def initialize(message, provider: nil, original_error: nil, error_code: Provider::ErrorCode::PROVIDER_FATAL)
2026
super(message)
2127
@provider = provider
2228
@original_error = original_error
29+
@error_code = error_code
2330
end
2431
end
2532
end

spec/open_feature/sdk/configuration_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
expect(error.provider).to be(provider)
116116
expect(error.original_error).to be_a(StandardError)
117117
expect(error.original_error.message).to eq(error_message)
118+
expect(error.error_code).to eq(OpenFeature::SDK::Provider::ErrorCode::PROVIDER_FATAL)
118119
end
119120
end
120121

@@ -145,6 +146,7 @@
145146
expect(error.message).to include("Provider initialization timed out after 0.1 seconds")
146147
expect(error.provider).to be(provider)
147148
expect(error.original_error).to be_a(Timeout::Error)
149+
expect(error.error_code).to eq(OpenFeature::SDK::Provider::ErrorCode::PROVIDER_FATAL)
148150
end
149151
end
150152

spec/open_feature/sdk/provider_initialization_error_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
it "inherits from StandardError" do
2929
expect(error).to be_a(StandardError)
3030
end
31+
32+
it "sets the error code to PROVIDER_FATAL by default" do
33+
expect(error.error_code).to eq(OpenFeature::SDK::Provider::ErrorCode::PROVIDER_FATAL)
34+
end
3135
end
3236

3337
context "with minimal parameters" do
@@ -44,6 +48,10 @@
4448
it "has nil original_error" do
4549
expect(error.original_error).to be_nil
4650
end
51+
52+
it "sets the error code to PROVIDER_FATAL by default" do
53+
expect(error.error_code).to eq(OpenFeature::SDK::Provider::ErrorCode::PROVIDER_FATAL)
54+
end
4755
end
4856

4957
context "with provider but no original_error" do
@@ -69,6 +77,16 @@
6977
expect(error.original_error).to be(original_error)
7078
end
7179
end
80+
81+
context "with custom error code" do
82+
subject(:error) do
83+
described_class.new(message, provider:, original_error:, error_code: "CUSTOM_ERROR")
84+
end
85+
86+
it "sets the custom error code" do
87+
expect(error.error_code).to eq("CUSTOM_ERROR")
88+
end
89+
end
7290
end
7391

7492
describe "inheritance" do

spec/open_feature/sdk_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@
4444

4545
expect do
4646
OpenFeature::SDK.set_provider_and_wait(provider)
47-
end.to raise_error(OpenFeature::SDK::ProviderInitializationError, /Provider initialization failed/)
47+
end.to raise_error(OpenFeature::SDK::ProviderInitializationError, /Provider initialization failed/) do |error|
48+
expect(error.error_code).to eq(OpenFeature::SDK::Provider::ErrorCode::PROVIDER_FATAL)
49+
end
4850

4951
# Clean up
5052
OpenFeature::SDK.set_provider(OpenFeature::SDK::Provider::NoOpProvider.new)

0 commit comments

Comments
 (0)