Skip to content

Commit

Permalink
refactor errors classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ignacio-chiazzo committed Sep 15, 2023
1 parent 0766835 commit e70a802
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 44 deletions.
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
13 changes: 13 additions & 0 deletions lib/whatsapp_sdk/resource/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@
module WhatsappSdk
module Resource
module Error
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
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::Api::Messages::MissingArgumentError) do
assert_raises(WhatsappSdk::Resource::Error::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::Api::Messages::MissingArgumentError) do
assert_raises(WhatsappSdk::Resource::Error::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::Api::Messages::MissingArgumentError) do
assert_raises(WhatsappSdk::Resource::Error::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::Api::Messages::MissingArgumentError) do
assert_raises(WhatsappSdk::Resource::Error::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::Api::Messages::MissingArgumentError) do
assert_raises(WhatsappSdk::Resource::Error::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::Api::Messages::MissingArgumentError) do
error = assert_raises(WhatsappSdk::Resource::Error::MissingArgumentError) do
@messages_api.send_template(
sender_id: 123_123, recipient_number: 56_789, name: "template", language: "en_US"
)
Expand Down

0 comments on commit e70a802

Please sign in to comment.