Skip to content

Commit

Permalink
rename error with errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ignacio-chiazzo committed Sep 15, 2023
1 parent e70a802 commit 4958c37
Show file tree
Hide file tree
Showing 16 changed files with 63 additions and 54 deletions.
23 changes: 16 additions & 7 deletions lib/whatsapp_sdk/api/messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def send_location(
def send_image(
sender_id:, recipient_number:, image_id: nil, link: nil, caption: "", message_id: nil
)
raise WhatsappSdk::Resource::Error::MissingArgumentError, "image_id or link is required" if !image_id && !link
raise WhatsappSdk::Resource::Errors::MissingArgumentError, "image_id or link is required" if !image_id && !link

params = {
messaging_product: "whatsapp",
Expand Down Expand Up @@ -153,7 +153,7 @@ def send_image(
).returns(WhatsappSdk::Api::Response)
end
def send_audio(sender_id:, recipient_number:, audio_id: nil, link: nil, message_id: nil)
raise WhatsappSdk::Resource::Error::MissingArgumentError, "audio_id or link is required" if !audio_id && !link
raise WhatsappSdk::Resource::Errors::MissingArgumentError, "audio_id or link is required" if !audio_id && !link

params = {
messaging_product: "whatsapp",
Expand Down Expand Up @@ -195,7 +195,7 @@ def send_audio(sender_id:, recipient_number:, audio_id: nil, link: nil, message_
def send_video(
sender_id:, recipient_number:, video_id: nil, link: nil, caption: "", message_id: nil
)
raise WhatsappSdk::Resource::Error::MissingArgumentError, "video_id or link is required" if !video_id && !link
raise WhatsappSdk::Resource::Errors::MissingArgumentError, "video_id or link is required" if !video_id && !link

params = {
messaging_product: "whatsapp",
Expand Down Expand Up @@ -241,7 +241,10 @@ def send_video(
def send_document(
sender_id:, recipient_number:, document_id: nil, link: nil, caption: "", message_id: nil
)
raise WhatsappSdk::Resource::Error::MissingArgumentError, "document or link is required" if !document_id && !link
if !document_id && !link
raise WhatsappSdk::Resource::Errors::MissingArgumentError,
"document or link is required"
end

params = {
messaging_product: "whatsapp",
Expand Down Expand Up @@ -283,7 +286,7 @@ def send_document(
).returns(WhatsappSdk::Api::Response)
end
def send_sticker(sender_id:, recipient_number:, sticker_id: nil, link: nil, message_id: nil)
raise WhatsappSdk::Resource::Error::MissingArgumentError, "sticker or link is required" if !sticker_id && !link
raise WhatsappSdk::Resource::Errors::MissingArgumentError, "sticker or link is required" if !sticker_id && !link

params = {
messaging_product: "whatsapp",
Expand Down Expand Up @@ -371,7 +374,10 @@ def send_contacts(
def send_interactive_message(
sender_id:, recipient_number:, interactive: nil, interactive_json: nil, message_id: nil
)
raise WhatsappSdk::Resource::Error::MissingArgumentError, "interactive or interactive_json is required" if !interactive && !interactive_json
if !interactive && !interactive_json
raise WhatsappSdk::Resource::Errors::MissingArgumentError,
"interactive or interactive_json is required"
end

params = {
messaging_product: "whatsapp",
Expand Down Expand Up @@ -446,7 +452,10 @@ def read_message(sender_id:, message_id:)
def send_template(
sender_id:, recipient_number:, name:, language:, components: nil, components_json: nil
)
raise WhatsappSdk::Resource::Error::MissingArgumentError, "components or components_json is required" if !components && !components_json
if !components && !components_json
raise WhatsappSdk::Resource::Errors::MissingArgumentError,
"components or components_json is required"
end

params = {
messaging_product: "whatsapp",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module WhatsappSdk
module Resource
module Error
module Errors
class MissingArgumentError < StandardError
extend T::Sig

Expand Down
10 changes: 5 additions & 5 deletions lib/whatsapp_sdk/resource/interactive_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,18 @@ def validate_list_message
button_length = button.length
sections_count = sections.length
unless button_length.positive?
raise WhatsappSdk::Resource::Error::InvalidInteractiveActionButton,
raise WhatsappSdk::Resource::Errors::InvalidInteractiveActionButton,
"Invalid button in action. Button label is required."
end

unless button_length <= LIST_BUTTON_TITLE_MAXIMUM
raise WhatsappSdk::Resource::Error::InvalidInteractiveActionButton,
raise WhatsappSdk::Resource::Errors::InvalidInteractiveActionButton,
"Invalid length #{button_length} for button. Maximum length: " \
"#{LIST_BUTTON_TITLE_MAXIMUM} characters."
end

unless (LIST_SECTIONS_MINIMUM..LIST_SECTIONS_MAXIMUM).cover?(sections_count)
raise WhatsappSdk::Resource::Error::InvalidInteractiveActionSection,
raise WhatsappSdk::Resource::Errors::InvalidInteractiveActionSection,
"Invalid length #{sections_count} for sections in action. It should be between " \
"#{LIST_SECTIONS_MINIMUM} and #{LIST_SECTIONS_MAXIMUM}."
end
Expand All @@ -125,15 +125,15 @@ def validate_list_message
def validate_reply_button
buttons_count = buttons.length
unless (REPLY_BUTTONS_MINIMUM..REPLY_BUTTONS_MAXIMUM).cover?(buttons_count)
raise WhatsappSdk::Resource::Error::InvalidInteractiveActionReplyButton,
raise WhatsappSdk::Resource::Errors::InvalidInteractiveActionReplyButton,
"Invalid length #{buttons_count} for buttons in action. It should be between " \
"#{REPLY_BUTTONS_MINIMUM} and #{REPLY_BUTTONS_MAXIMUM}."
end

button_ids = buttons.map(&:id)
return if button_ids.length.eql?(button_ids.uniq.length)

raise WhatsappSdk::Resource::Error::InvalidInteractiveActionReplyButton,
raise WhatsappSdk::Resource::Errors::InvalidInteractiveActionReplyButton,
"Duplicate ids #{button_ids} for buttons in action. They should be unique."
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/whatsapp_sdk/resource/interactive_action_reply_button.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def validate_title
title_length = title.length
return if title_length <= ACTION_BUTTON_TITLE_MAXIMUM

raise WhatsappSdk::Resource::Error::InvalidInteractiveActionReplyButton,
raise WhatsappSdk::Resource::Errors::InvalidInteractiveActionReplyButton,
"Invalid length #{title_length} for title in button. " \
"Maximum length: #{ACTION_BUTTON_TITLE_MAXIMUM} characters."
end
Expand All @@ -80,7 +80,7 @@ def validate_id
id_length = id.length
return if id_length <= ACTION_BUTTON_ID_MAXIMUM

raise WhatsappSdk::Resource::Error::InvalidInteractiveActionReplyButton,
raise WhatsappSdk::Resource::Errors::InvalidInteractiveActionReplyButton,
"Invalid length #{id_length} for id in button. Maximum length: #{ACTION_BUTTON_ID_MAXIMUM} characters."
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/whatsapp_sdk/resource/interactive_action_section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def validate_title
title_length = title.length
return if title_length <= ACTION_SECTION_TITLE_MAXIMUM

raise WhatsappSdk::Resource::Error::InvalidInteractiveActionSection,
raise WhatsappSdk::Resource::Errors::InvalidInteractiveActionSection,
"Invalid length #{title_length} for title in section. Maximum length: " \
"#{ACTION_SECTION_TITLE_MAXIMUM} characters."
end
Expand All @@ -63,7 +63,7 @@ def validate_rows
rows_length = rows.length
return if rows_length <= ACTION_SECTION_ROWS_MAXIMUM

raise WhatsappSdk::Resource::Error::InvalidInteractiveActionSection,
raise WhatsappSdk::Resource::Errors::InvalidInteractiveActionSection,
"Invalid number of rows #{rows_length} in section. Maximum count: " \
"#{ACTION_SECTION_ROWS_MAXIMUM}."
end
Expand Down
6 changes: 3 additions & 3 deletions lib/whatsapp_sdk/resource/interactive_action_section_row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def validate_title
title_length = title.length
return if title_length <= ACTION_SECTION_TITLE_MAXIMUM

raise WhatsappSdk::Resource::Error::InvalidInteractiveActionSectionRow,
raise WhatsappSdk::Resource::Errors::InvalidInteractiveActionSectionRow,
"Invalid length #{title_length} for title in section row. "\
"Maximum length: #{ACTION_SECTION_TITLE_MAXIMUM} characters."
end
Expand All @@ -74,7 +74,7 @@ def validate_id
id_length = id.length
return if id_length <= ACTION_SECTION_ID_MAXIMUM

raise WhatsappSdk::Resource::Error::InvalidInteractiveActionSectionRow,
raise WhatsappSdk::Resource::Errors::InvalidInteractiveActionSectionRow,
"Invalid length #{id_length} for id in section row. Maximum length: "\
"#{ACTION_SECTION_ID_MAXIMUM} characters."
end
Expand All @@ -84,7 +84,7 @@ def validate_description
description_length = description.length
return if description_length <= ACTION_SECTION_DESCRIPTION_MAXIMUM

raise WhatsappSdk::Resource::Error::InvalidInteractiveActionSectionRow,
raise WhatsappSdk::Resource::Errors::InvalidInteractiveActionSectionRow,
"Invalid length #{description_length} for description in section " \
"row. Maximum length: #{ACTION_SECTION_DESCRIPTION_MAXIMUM} characters."
end
Expand Down
2 changes: 1 addition & 1 deletion lib/whatsapp_sdk/resource/interactive_body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def validate_text
text_length = text.length
return if text_length <= MAXIMUM_LENGTH

raise WhatsappSdk::Resource::Error::InvalidInteractiveBody,
raise WhatsappSdk::Resource::Errors::InvalidInteractiveBody,
"Invalid length #{text_length} for text in body. Maximum length: #{MAXIMUM_LENGTH} characters."
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/whatsapp_sdk/resource/interactive_footer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def validate_text
text_length = text.length
return if text_length <= MAXIMUM_LENGTH

raise WhatsappSdk::Resource::Error::InvalidInteractiveFooter,
raise WhatsappSdk::Resource::Errors::InvalidInteractiveFooter,
"Invalid length #{text_length} for text in footer. Maximum length: #{MAXIMUM_LENGTH} characters."
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/whatsapp_sdk/resource/interactive_header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def validate_attributes

next unless value.nil?

raise WhatsappSdk::Resource::Error::MissingValue.new(
raise WhatsappSdk::Resource::Errors::MissingValue.new(
type.serialize,
"#{type.serialize} is required when the type is #{type_b}"
)
Expand Down
4 changes: 2 additions & 2 deletions lib/whatsapp_sdk/resource/parameter_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ def validate_attributes
next unless type == type_b

if value.nil?
raise WhatsappSdk::Resource::Error::MissingValue.new(type.serialize,
"#{type_b} is required when the type is #{type_b}")
raise WhatsappSdk::Resource::Errors::MissingValue.new(type.serialize,
"#{type_b} is required when the type is #{type_b}")
end
end
end
Expand Down
12 changes: 6 additions & 6 deletions test/whatsapp/api/messages_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def test_send_location_message_with_valid_params
end

def test_send_image_raises_an_error_if_link_and_image_are_not_provided
assert_raises(WhatsappSdk::Resource::Error::MissingArgumentError) do
assert_raises(WhatsappSdk::Resource::Errors::MissingArgumentError) do
@messages_api.send_image(
sender_id: 123_123, recipient_number: 56_789,
image_id: nil, link: nil, caption: ""
Expand Down Expand Up @@ -205,7 +205,7 @@ def test_send_audio_message_with_success_response
end

def test_send_audio_raises_an_error_if_link_and_image_are_not_provided
assert_raises(WhatsappSdk::Resource::Error::MissingArgumentError) do
assert_raises(WhatsappSdk::Resource::Errors::MissingArgumentError) do
@messages_api.send_audio(
sender_id: 123_123, recipient_number: 56_789, link: nil, audio_id: nil
)
Expand Down Expand Up @@ -255,7 +255,7 @@ def test_send_audio_message_with_an_audio_id
end

def test_send_video_raises_an_error_if_link_and_image_are_not_provided
assert_raises(WhatsappSdk::Resource::Error::MissingArgumentError) do
assert_raises(WhatsappSdk::Resource::Errors::MissingArgumentError) do
@messages_api.send_video(
sender_id: 123_123, recipient_number: 56_789, link: nil, video_id: nil
)
Expand Down Expand Up @@ -316,7 +316,7 @@ def test_send_video_message_with_an_video_id
end

def test_send_document_raises_an_error_if_link_and_image_are_not_provided
assert_raises(WhatsappSdk::Resource::Error::MissingArgumentError) do
assert_raises(WhatsappSdk::Resource::Errors::MissingArgumentError) do
@messages_api.send_document(
sender_id: 123_123, recipient_number: 56_789, link: nil, document_id: nil
)
Expand Down Expand Up @@ -377,7 +377,7 @@ def test_send_document_message_with_an_document_id
end

def test_send_sticker_raises_an_error_if_link_and_image_are_not_provided
assert_raises(WhatsappSdk::Resource::Error::MissingArgumentError) do
assert_raises(WhatsappSdk::Resource::Errors::MissingArgumentError) do
@messages_api.send_sticker(
sender_id: 123_123, recipient_number: 56_789, link: nil, sticker_id: nil
)
Expand Down Expand Up @@ -497,7 +497,7 @@ def test_read_message_with_an_invalid_response
end

def test_send_template_raises_an_error_when_component_and_component_json_are_not_provided
error = assert_raises(WhatsappSdk::Resource::Error::MissingArgumentError) do
error = assert_raises(WhatsappSdk::Resource::Errors::MissingArgumentError) do
@messages_api.send_template(
sender_id: 123_123, recipient_number: 56_789, name: "template", language: "en_US"
)
Expand Down
6 changes: 3 additions & 3 deletions test/whatsapp/resource/interactive_action_section_row_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module WhatsappSdk
module Resource
class InteractionActionSectionRowTest < Minitest::Test
def test_validation
error = assert_raises(WhatsappSdk::Resource::Error::InvalidInteractiveActionSectionRow) do
error = assert_raises(WhatsappSdk::Resource::Errors::InvalidInteractiveActionSectionRow) do
WhatsappSdk::Resource::InteractiveActionSectionRow.new(
title: "I am the longer row title",
id: "section_row",
Expand All @@ -19,7 +19,7 @@ def test_validation
error.message
)

error = assert_raises(WhatsappSdk::Resource::Error::InvalidInteractiveActionSectionRow) do
error = assert_raises(WhatsappSdk::Resource::Errors::InvalidInteractiveActionSectionRow) do
WhatsappSdk::Resource::InteractiveActionSectionRow.new(
title: "I am the row title",
id: "section_row",
Expand All @@ -31,7 +31,7 @@ def test_validation
error.message
)

error = assert_raises(WhatsappSdk::Resource::Error::InvalidInteractiveActionSectionRow) do
error = assert_raises(WhatsappSdk::Resource::Errors::InvalidInteractiveActionSectionRow) do
WhatsappSdk::Resource::InteractiveActionSectionRow.new(
title: "I am the row title",
id: "section_row" * 25,
Expand Down
2 changes: 1 addition & 1 deletion test/whatsapp/resource/interactive_action_section_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module WhatsappSdk
module Resource
class InteractionActionSectionTest < Minitest::Test
def test_validation
error = assert_raises(WhatsappSdk::Resource::Error::InvalidInteractiveActionSection) do
error = assert_raises(WhatsappSdk::Resource::Errors::InvalidInteractiveActionSection) do
WhatsappSdk::Resource::InteractiveActionSection.new(
title: "I am the longer section title"
)
Expand Down
10 changes: 5 additions & 5 deletions test/whatsapp/resource/interactive_action_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module WhatsappSdk
module Resource
class InteractionActionTest < Minitest::Test
def test_validation
error = assert_raises(WhatsappSdk::Resource::Error::InvalidInteractiveActionButton) do
error = assert_raises(WhatsappSdk::Resource::Errors::InvalidInteractiveActionButton) do
interactive_action = WhatsappSdk::Resource::InteractiveAction.new(
type: WhatsappSdk::Resource::InteractiveAction::Type::ListMessage
)
Expand All @@ -18,7 +18,7 @@ def test_validation
error.message
)

error = assert_raises(WhatsappSdk::Resource::Error::InvalidInteractiveActionButton) do
error = assert_raises(WhatsappSdk::Resource::Errors::InvalidInteractiveActionButton) do
interactive_action = WhatsappSdk::Resource::InteractiveAction.new(
type: WhatsappSdk::Resource::InteractiveAction::Type::ListMessage
)
Expand All @@ -30,7 +30,7 @@ def test_validation
error.message
)

error = assert_raises(WhatsappSdk::Resource::Error::InvalidInteractiveActionSection) do
error = assert_raises(WhatsappSdk::Resource::Errors::InvalidInteractiveActionSection) do
interactive_action = WhatsappSdk::Resource::InteractiveAction.new(
type: WhatsappSdk::Resource::InteractiveAction::Type::ListMessage
)
Expand All @@ -42,7 +42,7 @@ def test_validation
error.message
)

error = assert_raises(WhatsappSdk::Resource::Error::InvalidInteractiveActionReplyButton) do
error = assert_raises(WhatsappSdk::Resource::Errors::InvalidInteractiveActionReplyButton) do
interactive_action = WhatsappSdk::Resource::InteractiveAction.new(
type: WhatsappSdk::Resource::InteractiveAction::Type::ReplyButton
)
Expand All @@ -53,7 +53,7 @@ def test_validation
error.message
)

error = assert_raises(WhatsappSdk::Resource::Error::InvalidInteractiveActionReplyButton) do
error = assert_raises(WhatsappSdk::Resource::Errors::InvalidInteractiveActionReplyButton) do
interactive_action = WhatsappSdk::Resource::InteractiveAction.new(
type: WhatsappSdk::Resource::InteractiveAction::Type::ReplyButton
)
Expand Down
Loading

0 comments on commit 4958c37

Please sign in to comment.