Skip to content

Commit 0cf937e

Browse files
committed
chore(client): throw specific errors
1 parent 6228400 commit 0cf937e

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

lib/openai/errors.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ class Error < StandardError
88
# @return [StandardError, nil]
99
end
1010

11+
class InvalidWebhookSignatureError < OpenAI::Errors::Error
12+
end
13+
1114
class ConversionError < OpenAI::Errors::Error
1215
# @return [StandardError, nil]
1316
def cause = @cause.nil? ? super : @cause

lib/openai/resources/webhooks.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def unwrap(
2121
webhook_secret = @client.webhook_secret || ENV["OPENAI_WEBHOOK_SECRET"]
2222
)
2323
verify_signature(payload, headers, webhook_secret)
24-
24+
2525
parsed = JSON.parse(payload, symbolize_names: true)
2626
OpenAI::Internal::Type::Converter.coerce(OpenAI::Models::Webhooks::UnwrapWebhookEvent, parsed)
2727
end
@@ -73,11 +73,11 @@ def verify_signature(
7373
now = Time.now.to_i
7474

7575
if now - timestamp_seconds > tolerance
76-
raise ArgumentError, "Webhook timestamp is too old"
76+
raise OpenAI::Errors::InvalidWebhookSignatureError, "Webhook timestamp is too old"
7777
end
7878

7979
if timestamp_seconds > now + tolerance
80-
raise ArgumentError, "Webhook timestamp is too new"
80+
raise OpenAI::Errors::InvalidWebhookSignatureError, "Webhook timestamp is too new"
8181
end
8282

8383
# Extract signatures from v1,<base64> format
@@ -109,7 +109,8 @@ def verify_signature(
109109
# Accept if any signature matches using timing-safe comparison
110110
return if signatures.any? { |signature| OpenSSL.secure_compare(expected_signature, signature) }
111111

112-
raise ArgumentError, "The given webhook signature does not match the expected signature"
112+
raise OpenAI::Errors::InvalidWebhookSignatureError,
113+
"The given webhook signature does not match the expected signature"
113114
end
114115

115116
# @api private

test/openai/resources/webhooks_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def test_verify_signature_with_invalid_secret
3838
"webhook-id" => @webhook_id
3939
}
4040

41-
assert_raises(ArgumentError) do
41+
assert_raises(OpenAI::Errors::InvalidWebhookSignatureError) do
4242
@webhook_service.verify_signature(@test_payload, headers, "invalid_secret")
4343
end
4444
end
@@ -60,7 +60,7 @@ def test_verify_signature_with_old_timestamp
6060
"webhook-id" => @webhook_id
6161
}
6262

63-
assert_raises(ArgumentError) do
63+
assert_raises(OpenAI::Errors::InvalidWebhookSignatureError) do
6464
@webhook_service.verify_signature(@test_payload, headers, @test_secret)
6565
end
6666
end
@@ -90,7 +90,7 @@ def test_verify_signature_with_custom_tolerance
9090
}
9191

9292
# Should fail due to old timestamp
93-
assert_raises(ArgumentError) do
93+
assert_raises(OpenAI::Errors::InvalidWebhookSignatureError) do
9494
@webhook_service.verify_signature(@test_payload, headers, @test_secret, 300)
9595
end
9696
end
@@ -136,7 +136,7 @@ def test_verify_signature_with_multiple_signatures_all_invalid
136136
"webhook-id" => @webhook_id
137137
}
138138

139-
assert_raises(ArgumentError) do
139+
assert_raises(OpenAI::Errors::InvalidWebhookSignatureError) do
140140
@webhook_service.verify_signature(@test_payload, headers, @test_secret)
141141
end
142142
end

0 commit comments

Comments
 (0)