frederick_api 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3655ceb3d6aec4c8649bc0389c9f998c25df9280
4
- data.tar.gz: '01974c4215719d6843f2375c75ed91254550d78c'
3
+ metadata.gz: dcd5376c71e168c4a465fea05d201c98fdeb34a8
4
+ data.tar.gz: c32d4ff431afc095213e192113857e7cf64008c7
5
5
  SHA512:
6
- metadata.gz: 324ba1bf6232d75d8e0f6979d0f14d180aecac348147ef948d9877b15e3637729ae466b5871ff08325d858063719cee2cd239d72bf8cb2ad2249048ec665137d
7
- data.tar.gz: 5d6421390051455d9a1b6e77545a5295d0207d65d93d8692000dbce6a0066d10060ad8cdce762e7374016c483c2da60b5d832872c55c9044e920e4519e324a82
6
+ metadata.gz: 25a914b9aa9e5d2db5cb6c0fff20e7fa3ac9fd81723bfb036dd4a17beb486ea4c1a72afc1a46e2fb40c93001a86f24859110192a8bdcd79c01fa6f12870a44c0
7
+ data.tar.gz: ec3f75966e35fea113626823e6e5b8429280377ace75231a63d6ce4caf4873d3b5afe4b16426518091f80ae20463af2f1645d7c795434c065372c0efc3558935
data/lib/frederick_api.rb CHANGED
@@ -7,6 +7,7 @@ require 'active_support/concern'
7
7
 
8
8
  # FrederickAPI libs
9
9
  require 'frederick_api/configuration'
10
+ require 'frederick_api/v2/errors/errors'
10
11
  require 'frederick_api/v2/helpers/has_many'
11
12
  require 'frederick_api/v2/helpers/paginator'
12
13
  require 'frederick_api/v2/helpers/query_builder'
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FrederickAPI
4
+ module V2
5
+ module Errors
6
+ # Base exception class for client errors (i.e. validation, bad request)
7
+ class Error < JsonApiClient::Errors::ClientError
8
+ attr_reader :errors
9
+
10
+ # Initialize with a JsonApiClient::ResultSet or a Resource
11
+ def initialize(result)
12
+ # @env is used in base class JsonApiClient::Errors::Error
13
+ @env = result
14
+ @errors = result.errors || []
15
+ end
16
+
17
+ def to_s
18
+ return "Client Error: #{self.errors.first['detail']}" if self.errors.any?
19
+ super
20
+ end
21
+ end
22
+
23
+ class BadRequest < Error; end
24
+ class UnprocessableEntity < Error; end
25
+
26
+ ERROR_CODES = {
27
+ '400' => BadRequest,
28
+ '422' => UnprocessableEntity
29
+ }.freeze
30
+ end
31
+ end
32
+ end
@@ -23,11 +23,20 @@ module FrederickAPI
23
23
 
24
24
  # Retry once on unhandled server errors
25
25
  def request(type, path, params)
26
- super
26
+ handle_errors(super)
27
27
  rescue JsonApiClient::Errors::ConnectionError, JsonApiClient::Errors::ServerError => ex
28
28
  raise ex if ex.is_a?(JsonApiClient::Errors::NotFound) || ex.is_a?(JsonApiClient::Errors::Conflict)
29
- super
29
+ handle_errors(super)
30
30
  end
31
+
32
+ private
33
+
34
+ def handle_errors(result)
35
+ return result unless result.has_errors?
36
+ error_klass = FrederickAPI::V2::Errors::ERROR_CODES[result.errors.first[:status]] ||
37
+ FrederickAPI::V2::Errors::Error
38
+ raise error_klass, result
39
+ end
31
40
  end
32
41
  end
33
42
  end
@@ -18,6 +18,10 @@ module FrederickAPI
18
18
  super
19
19
  end
20
20
 
21
+ def has_errors?
22
+ self.errors.present?
23
+ end
24
+
21
25
  def self.all_records
22
26
  self.all.pages.all_records
23
27
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  module FrederickAPI
4
4
  # Current gem version
5
- VERSION = '0.3.2'
5
+ VERSION = '0.4.0'
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: frederick_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Frederick Engineering
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-10 00:00:00.000000000 Z
11
+ date: 2017-12-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json_api_client
@@ -41,6 +41,7 @@ files:
41
41
  - lib/frederick_api/v2/contact_list.rb
42
42
  - lib/frederick_api/v2/contact_property.rb
43
43
  - lib/frederick_api/v2/contact_type.rb
44
+ - lib/frederick_api/v2/errors/errors.rb
44
45
  - lib/frederick_api/v2/helpers/has_many.rb
45
46
  - lib/frederick_api/v2/helpers/paginator.rb
46
47
  - lib/frederick_api/v2/helpers/query_builder.rb