Skip to content

Commit dbf4f1a

Browse files
author
Unai Abrisketa
authored
Fix logger and add raise option (#11)
* Fix logger * Add option to raise on error requests
1 parent 4bb4026 commit dbf4f1a

File tree

5 files changed

+21
-5
lines changed

5 files changed

+21
-5
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
beyond_api (0.2.1.pre)
4+
beyond_api (0.3.0.pre)
55
faraday (~> 0.15)
66

77
GEM

lib/beyond_api.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
module BeyondApi
1111
class Error < StandardError; end
1212

13+
def self.logger
14+
@@logger ||= defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
15+
end
16+
17+
def self.logger=(logger)
18+
@@logger = logger
19+
end
20+
1321
class << self
1422
attr_accessor :configuration
1523
end
@@ -22,7 +30,7 @@ def self.setup
2230

2331
class Configuration
2432
attr_accessor :client_id, :client_secret, :open_timeout, :timeout, :remove_response_links,
25-
:remove_response_key_underscores, :object_struct_responses
33+
:remove_response_key_underscores, :object_struct_responses, :raise_error_requests
2634

2735
def initialize
2836
@client_id = nil
@@ -32,6 +40,7 @@ def initialize
3240
@remove_response_links = false
3341
@remove_response_key_underscores = false
3442
@object_struct_responses = false
43+
@raise_error_requests = false
3544
end
3645
end
3746
end

lib/beyond_api/utils.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ def handle_response(response, status, respond_with_true: false)
1010
response = sanitize_response(response)
1111
BeyondApi.configuration.object_struct_responses ? to_object_struct(response) : response
1212
else
13-
logger.error "[Beyond API] #{response}" if logger
14-
BeyondApi::Error.new(response)
13+
error = BeyondApi::Error.new(response)
14+
BeyondApi.logger.error "[Beyond API] #{response}"
15+
BeyondApi.configuration.raise_error_requests ? raise(error) : error
1516
end
1617
end
1718

lib/beyond_api/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module BeyondApi
2-
VERSION = "0.2.1.pre".freeze
2+
VERSION = "0.3.0.pre".freeze
33
end

lib/generators/templates/beyond_api_initializer.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,10 @@
2626
# With OpenStructs => response.embeded.products.first.id
2727
# Without OpenStructs => response["embeded"]["products"].first["id"]
2828
# config.object_struct_responses = false
29+
30+
# Configure if the gem should raise on error requests. Setting it to true is
31+
# useful for working with exceptions. Setting it to false will return a
32+
# BeyondApi::Error object with detailed information of the error.
33+
# Default is false.
34+
# config.raise_error_requests = false
2935
end

0 commit comments

Comments
 (0)