Sha256: 96782f0a87d50ff913bf962596c9911a8a3d309923ee8069138476f604480657

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

module JsChat
  class Error < RuntimeError
    def initialize(code_key, message)
      @message = message
      @code = JsChat::Errors::Codes.invert[code_key]
    end

    # Note: This shouldn't really include 'display' directives
    def to_json(*a)
      { 'display' => 'error', 'error' => { 'message' => @message, 'code' => @code } }.to_json(*a)
    end
  end

  module Errors
    class InvalidName < JsChat::Error ; end
    class MessageTooLong < JsChat::Error ; end
    class InvalidCookie < JsChat::Error ; end

    Codes = {
      # 1xx: User errors
      100 => :name_taken,
      101 => :invalid_name,
      104 => :not_online,
      105 => :identity_required,
      106 => :already_identified,
      107 => :invalid_cookie,
      # 2xx: Room errors
      200 => :already_joined,
      201 => :invalid_room,
      202 => :not_in_room,
      204 => :room_not_available,
      # 3xx: Message errors
      300 => :to_required,
      301 => :message_too_long,
      # 5xx: Other errors
      500 => :invalid_request,
      501 => :flooding,
      502 => :ping_out
    }
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jschat-0.3.7 lib/jschat/errors.rb