glia-errors
Implements Glia errors in Ruby and provides utilities to easily construct them.
Installation
$ gem install glia-errors
Usage
Require library
require 'glia/errors'
For Glia developers
Map from dry-validation result
Currently 2 dry-validation versions are supported:
v0up to0.13v1up to1.6
schema = Dry::Validation.Schema do
# ...
end
result = schema.(input)
error = Glia::Errors.from_dry_validation_result(result)
response = error.to_h
Specifying mapping for custom error message
If you have custom dry-validation predicates and error messages you can specify a custom error map.
Custom error map takes priority over standard error mapping.
When custom error appears to be quite common consider adding it to this library instead.
ERROR_MAP = {
'must be custom format' => lambda do |field, value, |
Glia::Errors::InvalidFormatError.new(field: field)
end
}
error = Glia::Errors.from_dry_validation_result(result, ERROR_MAP)
response = error.to_h
For REST API integrators
require 'uri'
require 'net/http'
require 'openssl'
require 'json'
url = URI('https://api.glia.com/engagement_requests')
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(url)
request["Accept"] = 'application/json'
request["Content-Type"] = 'application/json'
request.body = '{}'
response = http.request(request)
code = response.code.to_i
if code >= 200 || code <= 299
# Success
elsif response.header['Content-Type'] == 'application/json'
# Glia error
error = JSON.parse(response.read_body)
case error['type']
when Glia::Errors::INPUT_VALIDATION_ERROR
# ...
when Glia::Errors::UNKNOWN_ERROR
# ...
else
# ...
end
end
else
# Other error
end
Contributing
Testing
Glia errors support multiple versions of dry-validation and tests are run against each supported major version.
Under the hood we use Appraisal gem which generals additional gemfiles for each of the versions.
To run all tests use:
bundle exec rake test
To run tests only for dry-validation v1:
bundle exec rake test_dry_validation_v1
To run tests only for dry-validation v0:
bundle exec rake test_dry_validation_v0
Formatting
bundle exec rake format