Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/lib/selenium/webdriver/remote/response.rb
1865 views
1
# frozen_string_literal: true
2
3
# Licensed to the Software Freedom Conservancy (SFC) under one
4
# or more contributor license agreements. See the NOTICE file
5
# distributed with this work for additional information
6
# regarding copyright ownership. The SFC licenses this file
7
# to you under the Apache License, Version 2.0 (the
8
# "License"); you may not use this file except in compliance
9
# with the License. You may obtain a copy of the License at
10
#
11
# http://www.apache.org/licenses/LICENSE-2.0
12
#
13
# Unless required by applicable law or agreed to in writing,
14
# software distributed under the License is distributed on an
15
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
# KIND, either express or implied. See the License for the
17
# specific language governing permissions and limitations
18
# under the License.
19
20
module Selenium
21
module WebDriver
22
module Remote
23
#
24
# @api private
25
#
26
27
class Response
28
attr_reader :code, :payload
29
30
def initialize(code, payload = nil)
31
@code = code
32
@payload = payload || {}
33
34
assert_ok
35
end
36
37
def error
38
error, message, backtrace = process_error
39
klass = Error.for_error(error) || return
40
ex = klass.new(message)
41
add_cause(ex, error, backtrace)
42
ex
43
end
44
45
def [](key)
46
@payload[key]
47
end
48
49
private
50
51
def assert_ok
52
e = error
53
raise e if e
54
return unless @code.nil? || @code >= 400
55
56
raise Error::ServerError, self
57
end
58
59
def add_cause(ex, error, backtrace)
60
cause = Error::WebDriverError.new
61
backtrace = backtrace_from_remote(backtrace) if backtrace.is_a?(Array)
62
cause.set_backtrace(backtrace)
63
raise ex, cause: cause
64
rescue Error.for_error(error)
65
ex
66
end
67
68
def backtrace_from_remote(server_trace)
69
server_trace.filter_map do |frame|
70
next unless frame.is_a?(Hash)
71
72
file = frame['fileName']
73
line = frame['lineNumber']
74
method = frame['methodName']
75
76
class_name = frame['className']
77
file = "#{class_name}(#{file})" if class_name
78
79
method = 'unknown' if method.nil? || method.empty?
80
81
"[remote server] #{file}:#{line}:in `#{method}'"
82
end
83
end
84
85
def process_error
86
return unless self['value'].is_a?(Hash)
87
88
[
89
self['value']['error'],
90
self['value']['message'],
91
self['value']['stacktrace']
92
]
93
end
94
end # Response
95
end # Remote
96
end # WebDriver
97
end # Selenium
98
99