require "savon/error"
require "savon/soap/xml"
module Savon
module SOAP
class Fault < Error
def initialize(http)
self.http = http
end
attr_accessor :http
def present?
@present ||= http.body.include?("Fault>") && (soap1_fault? || soap2_fault?)
end
def to_s
return "" unless present?
@message ||= message_by_version to_hash[:fault]
end
def to_hash
@hash ||= Nori.parse(http.body)[:envelope][:body]
end
private
def soap1_fault?
http.body.include?("faultcode>") && http.body.include?("faultstring>")
end
def soap2_fault?
http.body.include?("Code>") && http.body.include?("Reason>")
end
def message_by_version(fault)
if fault[:faultcode]
"(#{fault[:faultcode]}) #{fault[:faultstring]}"
elsif fault[:code]
"(#{fault[:code][:value]}) #{fault[:reason][:text]}"
end
end
end
end
end