Skip to content

Commit 1536161

Browse files
author
Unai Abrisketa
authored
v0.11.0.pre
* enhancements * Improve error handling class * Improve `BeyondApi::Error` class
1 parent c4cd06e commit 1536161

File tree

6 files changed

+30
-12
lines changed

6 files changed

+30
-12
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
### v0.11.0.pre
2+
3+
* enhancements
4+
* Improve error handling class
5+
* Improve `BeyondApi::Error` class
6+
17
### v0.10.0.pre
28

39
* features

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.10.0.pre)
4+
beyond_api (0.11.0.pre)
55
faraday (~> 0.15)
66

77
GEM

lib/beyond_api/error.rb

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
# frozen_string_literal: true
22

33
module BeyondApi
4-
class Error
5-
attr_reader :error_id, :details, :message, :trace_id
4+
class Error < StandardError
5+
attr_reader :error_id, :details, :trace_id, :full_message, :status_code
66

7-
def initialize(data)
8-
@error_id = data['errorId']
9-
@details = data['details']
10-
@message = data['message']
11-
@trace_id = data['traceId']
7+
def initialize(data, status_code = nil)
8+
@error_id = data['errorId']
9+
@details = data['details']
10+
@trace_id = data['traceId']
11+
@full_message = data
12+
@status_code = status_code
13+
14+
super(data['message'])
1215
end
1316
end
1417
end

lib/beyond_api/resources/token.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
# frozen_string_literal: true
22

3+
require "beyond_api/utils"
4+
35
module BeyondApi
46
class Token
57
class InvalidSessionError < StandardError; end
68

9+
include BeyondApi::Utils
10+
711
attr_reader :session
812

913
def initialize(session)
@@ -44,7 +48,7 @@ def handle_response(response, status)
4448
@session.refresh_token = response["refresh_token"]
4549
@session
4650
else
47-
BeyondApi::Error.new(response)
51+
handle_error(response, status)
4852
end
4953
end
5054
end

lib/beyond_api/utils.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@ 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-
BeyondApi.logger.error "[Beyond API] #{response}"
14-
BeyondApi.configuration.raise_error_requests ? raise(response.to_s) : BeyondApi::Error.new(response)
13+
handle_error(response, status)
1514
end
1615
end
1716

17+
def handle_error(response, status)
18+
BeyondApi.logger.error "[Beyond API] #{status}: #{response}"
19+
error = BeyondApi::Error.new(response, status)
20+
BeyondApi.configuration.raise_error_requests ? raise(error) : error
21+
end
22+
1823
def to_object_struct(data)
1924
if data.is_a? Hash
2025
return OpenStruct.new(data.map { |key, val| [key, to_object_struct(val)] }.to_h)

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.10.0.pre".freeze
2+
VERSION = "0.11.0.pre".freeze
33
end

0 commit comments

Comments
 (0)