Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require "json"
- class ZAPIException < Exception
- property code : Int32, data : String?
- def initialize( jsrpc_error_h )
- if jsrpc_error_h.is_a?(NamedTuple) || jsrpc_error_h.is_a?(Hash)
- raise "Received invalid JSRPC error object" unless (msg=jsrpc_error_h["message"]?) && (code=jsrpc_error_h["code"]?)
- @message = msg.to_s
- if code.is_a?(Int32)
- @code = code
- else
- @code = -999
- end
- data = jsrpc_error_h["data"]?
- @data = (data && data.is_a?(String)) ? data : nil
- elsif jsrpc_error_h.is_a?(Array) && jsrpc_error_h.size>=2
- @code = 2
- @message = jsrpc_error_h
- @data = jsrpc_error_h[2]?
- end
- end
- end
- begin
- raise ZAPIException.new({"message"=>"somewhere in the darkness", "code": 789, "data": "Very long text....." })
- rescue ex : ZAPIException
- puts "Error #{ex.message}, code ##{ex.code}, additional data: #{ex.data.to_json}"
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement