Path: blob/trunk/rb/lib/selenium/webdriver/remote/response.rb
1865 views
# frozen_string_literal: true12# Licensed to the Software Freedom Conservancy (SFC) under one3# or more contributor license agreements. See the NOTICE file4# distributed with this work for additional information5# regarding copyright ownership. The SFC licenses this file6# to you under the Apache License, Version 2.0 (the7# "License"); you may not use this file except in compliance8# with the License. You may obtain a copy of the License at9#10# http://www.apache.org/licenses/LICENSE-2.011#12# Unless required by applicable law or agreed to in writing,13# software distributed under the License is distributed on an14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15# KIND, either express or implied. See the License for the16# specific language governing permissions and limitations17# under the License.1819module Selenium20module WebDriver21module Remote22#23# @api private24#2526class Response27attr_reader :code, :payload2829def initialize(code, payload = nil)30@code = code31@payload = payload || {}3233assert_ok34end3536def error37error, message, backtrace = process_error38klass = Error.for_error(error) || return39ex = klass.new(message)40add_cause(ex, error, backtrace)41ex42end4344def [](key)45@payload[key]46end4748private4950def assert_ok51e = error52raise e if e53return unless @code.nil? || @code >= 4005455raise Error::ServerError, self56end5758def add_cause(ex, error, backtrace)59cause = Error::WebDriverError.new60backtrace = backtrace_from_remote(backtrace) if backtrace.is_a?(Array)61cause.set_backtrace(backtrace)62raise ex, cause: cause63rescue Error.for_error(error)64ex65end6667def backtrace_from_remote(server_trace)68server_trace.filter_map do |frame|69next unless frame.is_a?(Hash)7071file = frame['fileName']72line = frame['lineNumber']73method = frame['methodName']7475class_name = frame['className']76file = "#{class_name}(#{file})" if class_name7778method = 'unknown' if method.nil? || method.empty?7980"[remote server] #{file}:#{line}:in `#{method}'"81end82end8384def process_error85return unless self['value'].is_a?(Hash)8687[88self['value']['error'],89self['value']['message'],90self['value']['stacktrace']91]92end93end # Response94end # Remote95end # WebDriver96end # Selenium979899