Sha256: 7807795342658f6bb29585b207a5f88b6dc86c6cb1c39b602554b6be543e9d17

Contents?: true

Size: 1.15 KB

Versions: 5

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true
module Savon
  class SOAPFault < Error

    def self.present?(http, xml = nil)
      xml ||= http.body
      fault_node  = xml.include?("Fault>")
      soap1_fault = xml.match(/faultcode\/?\>/) && xml.match(/faultstring\/?\>/)
      soap2_fault = xml.include?("Code>") && xml.include?("Reason>")

      fault_node && (soap1_fault || soap2_fault)
    end

    def initialize(http, nori, xml = nil)
      @xml = xml
      @http = http
      @nori = nori
    end

    attr_reader :http, :nori, :xml

    def to_s
      fault = nori.find(to_hash, 'Fault') || nori.find(to_hash, 'ServiceFault')
      message_by_version(fault)
    end

    def to_hash
      parsed = nori.parse(xml || http.body)
      nori.find(parsed, 'Envelope', 'Body') || {}
    end

    private

    def message_by_version(fault)
      if nori.find(fault, 'faultcode')
        code = nori.find(fault, 'faultcode')
        text = nori.find(fault, 'faultstring')

        "(#{code}) #{text}"
      elsif nori.find(fault, 'Code')
        code = nori.find(fault, 'Code', 'Value')
        text = nori.find(fault, 'Reason', 'Text')

        "(#{code}) #{text}"
      end
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
savon-2.15.1 lib/savon/soap_fault.rb
savon-2.15.0 lib/savon/soap_fault.rb
savon-2.14.0 lib/savon/soap_fault.rb
savon-2.13.1 lib/savon/soap_fault.rb
savon-2.13.0 lib/savon/soap_fault.rb