glia-errors 0.3.0 → 0.7.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
  SHA256:
3
- metadata.gz: 71d9c420c3a6350067f4f47cba98ebb07598c203951106c743e8d245938e4fb5
4
- data.tar.gz: 2d74dd1fbe3272416010b8ee66b698484002baaf9f2786efde680d6a6292150e
3
+ metadata.gz: f938565ae0608918f0808c950e1d2621f7d8ef2d347cb9146a37fc1af461a519
4
+ data.tar.gz: 7d8d9672987fe83c77884532e052adee18e3e980462176b6482dd1a41fb755ec
5
5
  SHA512:
6
- metadata.gz: 23120c8979c3ad4b4a9524a9ac61deb2a2452f8c44cd79a059099b201e5cc8e07357e102a20c74c2e1716e22154ce930096be2e8d45ceafa1006aacb643a28c6
7
- data.tar.gz: 9f0848627646f37ee78b24cd3bf3dea1b193e5541f5302782ce891727cfaeb82b502a56103350de43e41ad284340f8e2cf8848d88febf00b30c1492f7bc74b65
6
+ metadata.gz: f09300cb765f7ce0fe8d57cce7f8259303acd5dec9ffd61a36307ba0afb9d89c17ca95eb8010526a442ab0028da8fc9c61821f7fbe3cd5727c5d0df2a33e3821
7
+ data.tar.gz: 8208ee1f6c9e00975df486a1d8fb3f1457f38a576970f15485191f9639340b0ff5247dbcf527f2474ef1f0696ff519994985e9917c91949c33c9e6bcbc9a3c42
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.7.2
data/README.md CHANGED
@@ -18,7 +18,18 @@ require 'glia/errors'
18
18
 
19
19
  ### For Glia developers
20
20
 
21
- #### Map from `dry-validation` result
21
+ #### Create Glia error manually
22
+
23
+ 1. Select error from the [error list](https://internal-docs.at.samo.io/glia-errors/elixir/api-reference.html#modules)
24
+ - Documentation is for Elixir, however, the errors are the same and their initiation is very similar
25
+ 2. Initialize the error according to the error documentation
26
+
27
+ Example:
28
+ ```
29
+ glia_error = Glia::Errors::ResourceNotFoundError.new(resource: :engagement)
30
+ ```
31
+
32
+ #### Create Glia error from `dry-validation` result
22
33
 
23
34
  Currently 2 `dry-validation` versions are supported:
24
35
  * `v0` up to `0.13`
@@ -29,11 +40,10 @@ schema = Dry::Validation.Schema do
29
40
  # ...
30
41
  end
31
42
  result = schema.(input)
32
- error = Glia::Errors.from_dry_validation_result(result)
33
- response = error.to_h
43
+ glia_error = Glia::Errors.from_dry_validation_result(result)
34
44
  ```
35
45
 
36
- #### Specifying mapping for custom error message
46
+ #### Create Glia error from `dry-validation` that contains custom errors
37
47
 
38
48
  If you have custom `dry-validation` predicates and error messages you can specify a custom error map.
39
49
  Custom error map takes priority over standard error mapping.
@@ -46,8 +56,15 @@ ERROR_MAP = {
46
56
  Glia::Errors::InvalidFormatError.new(field: field)
47
57
  end
48
58
  }
49
- error = Glia::Errors.from_dry_validation_result(result, ERROR_MAP)
50
- response = error.to_h
59
+ glia_error = Glia::Errors.from_dry_validation_result(result, ERROR_MAP)
60
+ ```
61
+
62
+ #### Convert Glia error to a hash
63
+
64
+ You can use this to convert Glia error to hash, so that later it can be converted to JSON.
65
+
66
+ ```ruby
67
+ glia_error.to_h
51
68
  ```
52
69
 
53
70
  ### For REST API integrators
@@ -62,7 +79,6 @@ url = URI('https://api.glia.com/engagement_requests')
62
79
 
63
80
  http = Net::HTTP.new(url.host, url.port)
64
81
  http.use_ssl = true
65
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
66
82
  request = Net::HTTP::Post.new(url)
67
83
  request["Accept"] = 'application/json'
68
84
  request["Content-Type"] = 'application/json'
data/glia-errors.gemspec CHANGED
@@ -5,7 +5,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = 'glia-errors'
8
- spec.version = '0.3.0'
8
+ spec.version = '0.7.0'
9
9
  spec.authors = ['Glia TechMovers']
10
10
  spec.email = ['techmovers@glia.com']
11
11
 
data/lib/glia/errors.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require_relative './errors/error'
4
4
  require_relative './errors/error_types'
5
5
  require_relative './errors/client_errors'
6
+ require_relative './errors/server_errors'
6
7
  require_relative './errors/mapper'
7
8
 
8
9
  module Glia
@@ -7,8 +7,8 @@ module Glia
7
7
  def initialize(error_details:, message: nil)
8
8
  super(
9
9
  type: INPUT_VALIDATION_ERROR,
10
- ref: "https://example.com/errors/#{INPUT_VALIDATION_ERROR}.html",
11
- message: message,
10
+ ref: create_ref(INPUT_VALIDATION_ERROR),
11
+ message: message || 'Input is invalid',
12
12
  error_details: error_details
13
13
  )
14
14
  end
@@ -18,8 +18,8 @@ module Glia
18
18
  def initialize(field:, message: nil)
19
19
  super(
20
20
  type: INVALID_NUMBER_ERROR,
21
- ref: "https://example.com/errors/#{INVALID_NUMBER_ERROR}.html",
22
- message: "#{humanize(field)} " + (message || 'is invalid')
21
+ ref: create_ref(INVALID_NUMBER_ERROR),
22
+ message: message || "#{humanize(field)} value is invalid"
23
23
  )
24
24
  end
25
25
  end
@@ -28,8 +28,8 @@ module Glia
28
28
  def initialize(field:, message: nil)
29
29
  super(
30
30
  type: INVALID_VALUE_ERROR,
31
- ref: "https://example.com/errors/#{INVALID_VALUE_ERROR}.html",
32
- message: "#{humanize(field)} " + (message || 'is invalid')
31
+ ref: create_ref(INVALID_VALUE_ERROR),
32
+ message: message || "#{humanize(field)} value is invalid"
33
33
  )
34
34
  end
35
35
  end
@@ -38,8 +38,8 @@ module Glia
38
38
  def initialize(field:, message: nil)
39
39
  super(
40
40
  type: INVALID_LENGTH_ERROR,
41
- ref: "https://example.com/errors/#{INVALID_LENGTH_ERROR}.html",
42
- message: "#{humanize(field)} " + (message || 'length is invalid')
41
+ ref: create_ref(INVALID_LENGTH_ERROR),
42
+ message: message || "#{humanize(field)} length is invalid"
43
43
  )
44
44
  end
45
45
  end
@@ -57,8 +57,8 @@ module Glia
57
57
  format ? "has invalid format, required format is #{format}" : 'has invalid format'
58
58
  super(
59
59
  type: INVALID_FORMAT_ERROR,
60
- ref: "https://example.com/errors/#{INVALID_FORMAT_ERROR}.html",
61
- message: "#{humanize(field)} " + (message || default_message)
60
+ ref: create_ref(INVALID_FORMAT_ERROR),
61
+ message: message || "#{humanize(field)} #{default_message}"
62
62
  )
63
63
  end
64
64
  end
@@ -76,8 +76,9 @@ module Glia
76
76
  def initialize(field:, type:, message: nil)
77
77
  super(
78
78
  type: INVALID_TYPE_ERROR,
79
- ref: "https://example.com/errors/#{INVALID_TYPE_ERROR}.html",
80
- message: "#{humanize(field)} " + (message || "must be of type #{type}")
79
+ ref: create_ref(INVALID_TYPE_ERROR),
80
+ message: message || "#{humanize(field)} must be of type #{type}",
81
+ error_details: { type: type }
81
82
  )
82
83
  end
83
84
  end
@@ -86,8 +87,8 @@ module Glia
86
87
  def initialize(field:, message: nil)
87
88
  super(
88
89
  type: MISSING_VALUE_ERROR,
89
- ref: "https://example.com/errors/#{MISSING_VALUE_ERROR}.html",
90
- message: "#{humanize(field)} " + (message || 'is missing')
90
+ ref: create_ref(MISSING_VALUE_ERROR),
91
+ message: message || "#{humanize(field)} is missing"
91
92
  )
92
93
  end
93
94
  end
@@ -96,18 +97,20 @@ module Glia
96
97
  def initialize(field:, message: nil)
97
98
  super(
98
99
  type: UNKNOWN_ERROR,
99
- ref: "https://example.com/errors/#{UNKNOWN_ERROR}.html",
100
- message: "#{humanize(field)} " + (message || 'validation failed with unknown error')
100
+ ref: create_ref(UNKNOWN_ERROR),
101
+ message: message || "#{humanize(field)} validation failed with unknown error"
101
102
  )
102
103
  end
103
104
  end
104
105
 
105
106
  class ResourceNotFoundError < Error
106
107
  def initialize(resource:, message: nil)
108
+ assert_snake_case(resource)
109
+
107
110
  super(
108
111
  type: RESOURCE_NOT_FOUND_ERROR,
109
- ref: "https://example.com/errors/#{RESOURCE_NOT_FOUND_ERROR}.html",
110
- message: "#{humanize(resource)} " + (message || 'not found'),
112
+ ref: create_ref(RESOURCE_NOT_FOUND_ERROR),
113
+ message: message || "#{humanize(resource)} not found",
111
114
  error_details: { resource: resource }
112
115
  )
113
116
  end
@@ -115,10 +118,12 @@ module Glia
115
118
 
116
119
  class NotVerifiedError < Error
117
120
  def initialize(resource:, message: nil)
121
+ assert_snake_case(resource)
122
+
118
123
  super(
119
124
  type: NOT_VERIFIED_ERROR,
120
- ref: "https://example.com/errors/#{NOT_VERIFIED_ERROR}.html",
121
- message: "#{humanize(resource)} " + (message || 'is not verified'),
125
+ ref: create_ref(NOT_VERIFIED_ERROR),
126
+ message: message || "#{humanize(resource)} is not verified",
122
127
  error_details: { resource: resource }
123
128
  )
124
129
  end
@@ -126,14 +131,17 @@ module Glia
126
131
 
127
132
  class RemainingAssociationError < Error
128
133
  def initialize(resource:, associated_resource:, message: nil)
134
+ assert_snake_case(resource)
135
+ assert_snake_case(associated_resource)
136
+
129
137
  default_message =
130
138
  "cannot be modified/deleted because it is associated to one or more #{
131
139
  humanize(associated_resource)
132
140
  }(s)"
133
141
  super(
134
142
  type: REMAINING_ASSOCIATION_ERROR,
135
- ref: "https://example.com/errors/#{REMAINING_ASSOCIATION_ERROR}.html",
136
- message: "#{humanize(resource)} " + (message || default_message),
143
+ ref: create_ref(REMAINING_ASSOCIATION_ERROR),
144
+ message: message || "#{humanize(resource)} #{default_message}",
137
145
  error_details: { resource: resource, associated_resource: associated_resource }
138
146
  )
139
147
  end
@@ -141,10 +149,12 @@ module Glia
141
149
 
142
150
  class LimitExceededError < Error
143
151
  def initialize(resource:, max:, message: nil)
152
+ assert_snake_case(resource)
153
+
144
154
  super(
145
155
  type: LIMIT_EXCEEDED_ERROR,
146
- ref: "https://example.com/errors/#{LIMIT_EXCEEDED_ERROR}.html",
147
- message: "#{humanize(resource)} " + (message || "count must not exceed #{max}"),
156
+ ref: create_ref(LIMIT_EXCEEDED_ERROR),
157
+ message: message || "#{humanize(resource)} count must not exceed #{max}",
148
158
  error_details: { resource: resource, max: max }
149
159
  )
150
160
  end
@@ -152,10 +162,12 @@ module Glia
152
162
 
153
163
  class ResourceAlreadyExistsError < Error
154
164
  def initialize(resource:, message: nil)
165
+ assert_snake_case(resource)
166
+
155
167
  super(
156
168
  type: RESOURCE_ALREADY_EXISTS_ERROR,
157
- ref: "https://example.com/errors/#{RESOURCE_ALREADY_EXISTS_ERROR}.html",
158
- message: "#{humanize(resource)} " + (message || 'already exists'),
169
+ ref: create_ref(RESOURCE_ALREADY_EXISTS_ERROR),
170
+ message: message || "#{humanize(resource)} already exists",
159
171
  error_details: { resource: resource }
160
172
  )
161
173
  end
@@ -163,14 +175,37 @@ module Glia
163
175
 
164
176
  class InvalidResourceStateError < Error
165
177
  def initialize(resource:, state:, message: nil)
178
+ assert_snake_case(resource)
179
+ assert_snake_case(state)
180
+
166
181
  super(
167
182
  type: INVALID_RESOURCE_STATE_ERROR,
168
- ref: "https://example.com/errors/#{INVALID_RESOURCE_STATE_ERROR}.html",
169
- message: "#{humanize(resource)} " + (message || "is in invalid state: #{state}"),
183
+ ref: create_ref(INVALID_RESOURCE_STATE_ERROR),
184
+ message: message || "#{humanize(resource)} is in invalid state: #{state}",
170
185
  error_details: { resource: resource, state: state }
171
186
  )
172
187
  end
173
188
  end
189
+
190
+ class AuthorizationError < Error
191
+ def initialize(message: nil)
192
+ super(
193
+ type: AUTHORIZATION_ERROR,
194
+ ref: create_ref(AUTHORIZATION_ERROR),
195
+ message: message || 'You do not have permissions to perform the action'
196
+ )
197
+ end
198
+ end
199
+
200
+ class RecipientOptedOutError < Error
201
+ def initialize(message: nil)
202
+ super(
203
+ type: RECIPIENT_OPTED_OUT_ERROR,
204
+ ref: create_ref(RECIPIENT_OPTED_OUT_ERROR),
205
+ message: message || 'Recipient has opted out'
206
+ )
207
+ end
208
+ end
174
209
  # rubocop:enable Style/Documentation
175
210
  end
176
211
  end
@@ -4,6 +4,7 @@ module Glia
4
4
  module Errors
5
5
  # Base error
6
6
  class Error
7
+ SNAKE_CASE_REGEX = /^[a-z0-9]+(_[a-z0-9]+)*$/.freeze
7
8
  attr_reader :type, :ref, :message, :error_details
8
9
 
9
10
  def initialize(type:, ref:, message: nil, error_details: nil)
@@ -38,7 +39,8 @@ module Glia
38
39
  end
39
40
 
40
41
  def primitive?(details)
41
- details.nil? || [TrueClass, FalseClass, String, Integer, Float].include?(details.class)
42
+ details.nil? ||
43
+ [TrueClass, FalseClass, String, Integer, Float, Symbol].include?(details.class)
42
44
  end
43
45
 
44
46
  # Converts from camel_case to capitalized more human readable value
@@ -46,6 +48,17 @@ module Glia
46
48
  def humanize(value)
47
49
  value.to_s.capitalize.gsub('_', ' ')
48
50
  end
51
+
52
+ def assert_snake_case(value)
53
+ return if value.to_s.match(SNAKE_CASE_REGEX)
54
+
55
+ raise ArgumentError, "Expected '#{value}' to be in snake case"
56
+ end
57
+
58
+ def create_ref(type)
59
+ fragment = type.gsub('_', '-')
60
+ "https://docs.glia.com/glia-dev/reference/errors##{fragment}"
61
+ end
49
62
  end
50
63
  end
51
64
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  module Glia
4
4
  module Errors
5
+ # Client errors
5
6
  INPUT_VALIDATION_ERROR = 'input_validation_error'
6
7
  INVALID_TYPE_ERROR = 'invalid_type_error'
7
8
  INVALID_NUMBER_ERROR = 'invalid_number_error'
@@ -16,5 +17,11 @@ module Glia
16
17
  LIMIT_EXCEEDED_ERROR = 'limit_exceeded_error'
17
18
  RESOURCE_ALREADY_EXISTS_ERROR = 'resource_already_exists_error'
18
19
  INVALID_RESOURCE_STATE_ERROR = 'invalid_resource_state_error'
20
+ AUTHORIZATION_ERROR = 'authorization_error'
21
+ RECIPIENT_OPTED_OUT_ERROR = 'recipient_opted_out_error'
22
+
23
+ # Server errors
24
+ INTERNAL_SERVER_ERROR = 'internal_server_error'
25
+ SERVICE_UNAVAILABLE_ERROR = 'service_unavailable_error'
19
26
  end
20
27
  end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Glia
4
+ module Errors
5
+ # rubocop:disable Style/Documentation
6
+ class InternalServerError < Error
7
+ def initialize(message: nil)
8
+ super(
9
+ type: INTERNAL_SERVER_ERROR,
10
+ ref: create_ref(INTERNAL_SERVER_ERROR),
11
+ message: message || 'Internal server error'
12
+ )
13
+ end
14
+ end
15
+
16
+ class ServiceUnavailableError < Error
17
+ def initialize(message: nil)
18
+ super(
19
+ type: SERVICE_UNAVAILABLE_ERROR,
20
+ ref: create_ref(SERVICE_UNAVAILABLE_ERROR),
21
+ message: message || 'Service unavailable'
22
+ )
23
+ end
24
+ end
25
+ # rubocop:enable Style/Documentation
26
+ end
27
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: glia-errors
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Glia TechMovers
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-02 00:00:00.000000000 Z
11
+ date: 2021-03-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: ''
14
14
  email:
@@ -22,6 +22,7 @@ files:
22
22
  - ".prettierrc"
23
23
  - ".rspec"
24
24
  - ".rubocop.yml"
25
+ - ".ruby-version"
25
26
  - ".travis.yml"
26
27
  - Appraisals
27
28
  - Gemfile
@@ -36,6 +37,7 @@ files:
36
37
  - lib/glia/errors/error.rb
37
38
  - lib/glia/errors/error_types.rb
38
39
  - lib/glia/errors/mapper.rb
40
+ - lib/glia/errors/server_errors.rb
39
41
  homepage:
40
42
  licenses:
41
43
  - MIT
@@ -55,7 +57,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
55
57
  - !ruby/object:Gem::Version
56
58
  version: '0'
57
59
  requirements: []
58
- rubygems_version: 3.1.2
60
+ rubygems_version: 3.1.4
59
61
  signing_key:
60
62
  specification_version: 4
61
63
  summary: Glia REST API errors