Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor error #101

Merged
merged 5 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Unreleased
- [Breaking Change] Rename `error` module with `errors`. @ignacio-chiazzo [#101](https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk/pull/101)

If you are calling an error using `WhatsappSdk::Resource::Error`, you should update it to `WhatsappSdk::Resource::Errors`, e.g. `WhatsappSdk::Resource::Errors::MissingArgumentError`


# v 0.9.2
- Add Support to image/webp sticker media. @renatovico [#94](https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk/issues/94)
Expand Down
36 changes: 16 additions & 20 deletions lib/whatsapp_sdk/api/messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,6 @@ class Messages < Request

DEFAULT_HEADERS = T.let({ 'Content-Type' => 'application/json' }.freeze, Hash)

class MissingArgumentError < StandardError
extend T::Sig

sig { returns(String) }
attr_reader :message

sig { params(message: String).void }
def initialize(message)
@message = message
super(message)
end
end

# Send a text message.
#
# @param sender_id [Integer] Sender' phone number.
Expand Down Expand Up @@ -124,7 +111,7 @@ def send_location(
def send_image(
sender_id:, recipient_number:, image_id: nil, link: nil, caption: "", message_id: nil
)
raise 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 @@ -166,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 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 @@ -208,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 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 @@ -254,7 +241,10 @@ def send_video(
def send_document(
sender_id:, recipient_number:, document_id: nil, link: nil, caption: "", message_id: nil
)
raise 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 @@ -296,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 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 @@ -384,7 +374,10 @@ def send_contacts(
def send_interactive_message(
sender_id:, recipient_number:, interactive: nil, interactive_json: nil, message_id: nil
)
raise 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 @@ -459,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 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
49 changes: 49 additions & 0 deletions lib/whatsapp_sdk/api/responses/generic_error_response.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# typed: strict
# frozen_string_literal: true

require_relative "error_response"

module WhatsappSdk
module Api
module Responses
class GenericErrorResponse < ErrorResponse
sig { returns(Integer) }
attr_reader :code

sig { returns(T.nilable(Integer)) }
attr_reader :subcode

sig { returns(T.nilable(String)) }
attr_reader :message

sig { returns(T.nilable(String)) }
attr_reader :type

sig { returns(T.nilable(String)) }
attr_reader :data

sig { returns(T.nilable(String)) }
attr_reader :fbtrace_id

sig { params(response: T::Hash[T.untyped, T.untyped]).void }
def initialize(response:)
@code = T.let(response["code"], Integer)
@subcode = T.let(response["error_subcode"], T.nilable(Integer))
@message = T.let(response["message"], T.nilable(String))
@type = T.let(response["type"], T.nilable(String))
@data = T.let(response["data"], T.nilable(String))
@fbtrace_id = T.let(response["fbtrace_id"], T.nilable(String))
super(response: response)
end

sig { override.params(response: T::Hash[T.untyped, T.untyped]).returns(T.nilable(GenericErrorResponse)) }
def self.build_from_response(response:)
error_response = response["error"]
return unless error_response

new(response: error_response)
end
end
end
end
end
40 changes: 2 additions & 38 deletions lib/whatsapp_sdk/api/responses/message_error_response.rb
Original file line number Diff line number Diff line change
@@ -1,48 +1,12 @@
# typed: strict
# frozen_string_literal: true

require_relative "error_response"
require_relative "generic_error_response"

module WhatsappSdk
module Api
module Responses
class MessageErrorResponse < ErrorResponse
sig { returns(Integer) }
attr_reader :code

sig { returns(T.nilable(Integer)) }
attr_reader :subcode

sig { returns(T.nilable(String)) }
attr_reader :message

sig { returns(T.nilable(String)) }
attr_reader :type

sig { returns(T.nilable(String)) }
attr_reader :data

sig { returns(T.nilable(String)) }
attr_reader :fbtrace_id

sig { params(response: T::Hash[T.untyped, T.untyped]).void }
def initialize(response:)
@code = T.let(response["code"], Integer)
@subcode = T.let(response["error_subcode"], T.nilable(Integer))
@message = T.let(response["message"], T.nilable(String))
@type = T.let(response["type"], T.nilable(String))
@data = T.let(response["data"], T.nilable(String))
@fbtrace_id = T.let(response["fbtrace_id"], T.nilable(String))
super(response: response)
end

sig { override.params(response: T::Hash[T.untyped, T.untyped]).returns(T.nilable(MessageErrorResponse)) }
def self.build_from_response(response:)
error_response = response["error"]
return unless error_response

new(response: error_response)
end
class MessageErrorResponse < GenericErrorResponse
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,20 @@

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

sig { returns(String) }
attr_reader :message

sig { params(message: String).void }
def initialize(message)
@message = message
super(message)
end
end

class MissingValue < WhatsappSdk::Error
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
Loading