Commit fe3ee552 by Francis Zhou

raise error when errcode in response is not equal 0

parent 1e31ec26
...@@ -10,7 +10,14 @@ require "dingtalk/access_token" ...@@ -10,7 +10,14 @@ require "dingtalk/access_token"
require "dingtalk/corp_conversation" require "dingtalk/corp_conversation"
module Dingtalk module Dingtalk
class Error < StandardError class Error < StandardError; end
class RequestFailedError < Error
attr_reader :code, :message
def initialize(code:, message:)
@code, @message, = code, message
end
end end
class Signature class Signature
......
...@@ -122,8 +122,16 @@ module Dingtalk ...@@ -122,8 +122,16 @@ module Dingtalk
end end
response = HTTParty.send(method, url, **request_options) response = HTTParty.send(method, url, **request_options)
symbolized_response = JSON.parse(response.body).deep_symbolize_keys
JSON.parse(response.body).deep_symbolize_keys if symbolized_response[:errcode] != 0
raise Dingtalk::RequestFailedError.new(
code: symbolized_response[:errcode],
message: symbolized_response[:errmsg],
)
end
symbolized_response
end end
end end
end end
......
...@@ -27,6 +27,8 @@ RSpec.describe Dingtalk::Auth do ...@@ -27,6 +27,8 @@ RSpec.describe Dingtalk::Auth do
# 企业内部免登 响应结果 # 企业内部免登 响应结果
get_user_info_response_body = {}.tap do |h| get_user_info_response_body = {}.tap do |h|
h[:errcode] = 0
h[:errmsg] = "ok"
h[:userid] = @int_user_id h[:userid] = @int_user_id
h[:is_sys] = false h[:is_sys] = false
h[:sys_level] = 0 h[:sys_level] = 0
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment