Sha256: 2cefaae0640ed756970eed2bcbb414955c086f5ae80a3e905cd473e4a3124db7

Contents?: true

Size: 1.33 KB

Versions: 8

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true


# Definitions
# =======================================================================

module Locd
  
  # Classes
  # ----------------------------------------------------------------------------
  
  # Base class for errors that are to be expected and should be handled
  # gracefully.
  # 
  class HandledError < StandardError; end
  
  
  # Base class for {HandledError} that happen during request processing which
  # should result in useful error responses to the user.
  # 
  class RequestError < HandledError
    STATUS_CODE = 400
    STATUS_MESSAGE = "Bad Request"
    
    def self.status
      "#{ self::STATUS_CODE } #{ self::STATUS_MESSAGE }"
    end
    
    
    # TODO document `text` attribute.
    # 
    # @return [attr_type]
    #     
    attr_reader :text
    
    
    def initialize text = self.class::STATUS_MESSAGE
      @text = text
      super "#{ self.class.status }\n\n#{ text }"
    end
    
    def to_http_response
      Locd.http_response_for self.class.status, text
    end
    
    def to_proxy_machine_cmd
      { close: to_http_response }
    end
  end
  
  
  # Raised when we couldn't find what the request seems to be looking for
  # (`404 Not Found` in HTTP).
  # 
  class NotFoundError < RequestError
    STATUS_CODE = 404
    STATUS_MESSAGE = "Not Found"
  end
  
end # module Locd

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
locd-0.1.13 lib/locd/errors.rb
locd-0.1.12 lib/locd/errors.rb
locd-0.1.11 lib/locd/errors.rb
locd-0.1.10 lib/locd/errors.rb
locd-0.1.9 lib/locd/errors.rb
locd-0.1.8 lib/locd/errors.rb
locd-0.1.7 lib/locd/errors.rb
locd-0.1.6 lib/locd/errors.rb