Advertisement
DRVTiny

ZAPIException

Apr 28th, 2018
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.02 KB | None | 0 0
  1. require "json"
  2.  
  3. class ZAPIException < Exception
  4.     property code : Int32, data : String?
  5.  
  6.     def initialize( jsrpc_error_h )
  7.         if jsrpc_error_h.is_a?(NamedTuple) || jsrpc_error_h.is_a?(Hash)
  8.             raise "Received invalid JSRPC error object" unless (msg=jsrpc_error_h["message"]?) && (code=jsrpc_error_h["code"]?)
  9.             @message = msg.to_s
  10.             if code.is_a?(Int32)
  11.                 @code = code
  12.             else
  13.                 @code = -999
  14.             end
  15.             data = jsrpc_error_h["data"]?
  16.             @data = (data && data.is_a?(String)) ? data : nil
  17.         elsif jsrpc_error_h.is_a?(Array) && jsrpc_error_h.size>=2
  18.             @code = 2
  19.             @message = jsrpc_error_h
  20.             @data = jsrpc_error_h[2]?
  21.         end
  22.     end
  23. end
  24.  
  25. begin
  26.     raise ZAPIException.new({"message"=>"somewhere in the darkness", "code": 789, "data": "Very long text....." })
  27. rescue ex : ZAPIException
  28.     puts "Error #{ex.message}, code ##{ex.code}, additional data: #{ex.data.to_json}"
  29. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement