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

Create Glia error manually

  1. Select error from the error list
    • Documentation is for Elixir, however, the errors are the same and their initiation is very similar
  2. Initialize the error according to the error documentation

Example:

glia_error = Glia::Errors::ResourceNotFoundError.new(resource: :engagement)

Create Glia error from dry-validation result

Supports dry-validation from 1.6.x to 1.8.x

schema = Dry::Validation.Schema do
  # ...
end
result = schema.(input)
glia_error = Glia::Errors.from_dry_validation_result(result)

Create Glia error from dry-validation that contains custom errors

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, error_message|
    Glia::Errors::InvalidFormatError.new(field: field)
  end
}
glia_error = Glia::Errors.from_dry_validation_result(result, ERROR_MAP)

Convert Glia error to a hash

You can use this to convert Glia error to hash, so that later it can be converted to JSON.

glia_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
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

Releasing a new version

A new version is created when a change is merged into the master branch that changes the version number in glia-errors.gemspec. A Github Action will push the .gem file to rubygems.org

Contributing

Testing

bundle exec rake spec

Formatting

bundle exec rake format