Bug 1508670 - Convert usages of try macro to ? for webdriver and geckodriver. r=ato

Differential Revision: https://phabricator.services.mozilla.com/D13395

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Henrik Skupin 2018-11-29 20:58:36 +00:00
Родитель ddde052ada
Коммит 9a86603828
3 изменённых файлов: 76 добавлений и 78 удалений

Просмотреть файл

@ -136,7 +136,10 @@ impl<'a> BrowserCapabilities for FirefoxCapabilities<'a> {
fn accept_insecure_certs(&mut self, _: &Capabilities) -> WebDriverResult<bool> {
let version_str = self.version();
if let Some(x) = version_str {
Ok(try!(Version::from_str(&*x).or_else(|x| Err(convert_version_error(x)))).major >= 52)
Ok(Version::from_str(&*x)
.or_else(|x| Err(convert_version_error(x)))?
.major
>= 52)
} else {
Ok(false)
}
@ -151,7 +154,8 @@ impl<'a> BrowserCapabilities for FirefoxCapabilities<'a> {
version: &str,
comparison: &str,
) -> WebDriverResult<bool> {
try!(Version::from_str(version).or_else(|x| Err(convert_version_error(x))))
Version::from_str(version)
.or_else(|x| Err(convert_version_error(x)))?
.matches(comparison)
.or_else(|x| Err(convert_version_error(x)))
}
@ -320,16 +324,16 @@ impl FirefoxOptions {
rv.binary = binary_path;
if let Some(json) = matched.remove("moz:firefoxOptions") {
let options = try!(json.as_object().ok_or(WebDriverError::new(
let options = json.as_object().ok_or(WebDriverError::new(
ErrorStatus::InvalidArgument,
"'moz:firefoxOptions' \
capability is not an object"
)));
))?;
rv.profile = try!(FirefoxOptions::load_profile(&options));
rv.args = try!(FirefoxOptions::load_args(&options));
rv.log = try!(FirefoxOptions::load_log(&options));
rv.prefs = try!(FirefoxOptions::load_prefs(&options));
rv.profile = FirefoxOptions::load_profile(&options)?;
rv.args = FirefoxOptions::load_args(&options)?;
rv.log = FirefoxOptions::load_log(&options)?;
rv.prefs = FirefoxOptions::load_prefs(&options)?;
}
Ok(rv)
@ -337,22 +341,22 @@ impl FirefoxOptions {
fn load_profile(options: &Capabilities) -> WebDriverResult<Option<Profile>> {
if let Some(profile_json) = options.get("profile") {
let profile_base64 = try!(profile_json.as_str().ok_or(WebDriverError::new(
let profile_base64 = profile_json.as_str().ok_or(WebDriverError::new(
ErrorStatus::UnknownError,
"Profile is not a string"
)));
let profile_zip = &*try!(base64::decode(profile_base64));
))?;
let profile_zip = &*base64::decode(profile_base64)?;
// Create an emtpy profile directory
let profile = try!(Profile::new(None));
try!(unzip_buffer(
let profile = Profile::new(None)?;
unzip_buffer(
profile_zip,
profile
.temp_dir
.as_ref()
.expect("Profile doesn't have a path")
.path()
));
)?;
Ok(Some(profile))
} else {
@ -362,22 +366,20 @@ impl FirefoxOptions {
fn load_args(options: &Capabilities) -> WebDriverResult<Option<Vec<String>>> {
if let Some(args_json) = options.get("args") {
let args_array = try!(args_json.as_array().ok_or(WebDriverError::new(
let args_array = args_json.as_array().ok_or(WebDriverError::new(
ErrorStatus::UnknownError,
"Arguments were not an \
array"
)));
let args = try!(
args_array
.iter()
.map(|x| x.as_str().map(|x| x.to_owned()))
.collect::<Option<Vec<String>>>()
.ok_or(WebDriverError::new(
ErrorStatus::UnknownError,
"Arguments entries were not all \
strings"
))
);
))?;
let args = args_array
.iter()
.map(|x| x.as_str().map(|x| x.to_owned()))
.collect::<Option<Vec<String>>>()
.ok_or(WebDriverError::new(
ErrorStatus::UnknownError,
"Arguments entries were not all \
strings"
))?;
Ok(Some(args))
} else {
Ok(None)
@ -413,13 +415,13 @@ impl FirefoxOptions {
pub fn load_prefs(options: &Capabilities) -> WebDriverResult<Vec<(String, Pref)>> {
if let Some(prefs_data) = options.get("prefs") {
let prefs = try!(prefs_data.as_object().ok_or(WebDriverError::new(
let prefs = prefs_data.as_object().ok_or(WebDriverError::new(
ErrorStatus::UnknownError,
"Prefs were not an object"
)));
))?;
let mut rv = Vec::with_capacity(prefs.len());
for (key, value) in prefs.iter() {
rv.push((key.clone(), try!(pref_from_json(value))));
rv.push((key.clone(), pref_from_json(value)?));
}
Ok(rv)
} else {
@ -442,16 +444,16 @@ fn pref_from_json(value: &Value) -> WebDriverResult<Pref> {
fn unzip_buffer(buf: &[u8], dest_dir: &Path) -> WebDriverResult<()> {
let reader = Cursor::new(buf);
let mut zip = try!(
zip::ZipArchive::new(reader)
.map_err(|_| WebDriverError::new(ErrorStatus::UnknownError, "Failed to unzip profile"))
);
let mut zip = zip::ZipArchive::new(reader)
.map_err(|_| WebDriverError::new(ErrorStatus::UnknownError, "Failed to unzip profile"))?;
for i in 0..zip.len() {
let mut file = try!(zip.by_index(i).map_err(|_| WebDriverError::new(
ErrorStatus::UnknownError,
"Processing profile zip file failed"
)));
let mut file = zip.by_index(i).map_err(|_| {
WebDriverError::new(
ErrorStatus::UnknownError,
"Processing profile zip file failed",
)
})?;
let unzip_path = {
let name = file.name();
let is_dir = name.ends_with("/");
@ -467,7 +469,7 @@ fn unzip_buffer(buf: &[u8], dest_dir: &Path) -> WebDriverResult<()> {
if let Some(dir) = create_dir {
if !dir.exists() {
debug!("Creating profile directory tree {}", dir.to_string_lossy());
try!(fs::create_dir_all(dir));
fs::create_dir_all(dir)?;
}
}
}
@ -481,10 +483,10 @@ fn unzip_buffer(buf: &[u8], dest_dir: &Path) -> WebDriverResult<()> {
if let Some(unzip_path) = unzip_path {
debug!("Extracting profile to {}", unzip_path.to_string_lossy());
let dest = try!(fs::File::create(unzip_path));
let dest = fs::File::create(unzip_path)?;
if file.size() > 0 {
let mut writer = BufWriter::new(dest);
try!(io::copy(&mut file, &mut writer));
io::copy(&mut file, &mut writer)?;
}
}
}

Просмотреть файл

@ -439,7 +439,7 @@ impl MarionetteSession {
return Err(error.into());
}
try!(self.update(msg, &resp));
self.update(msg, &resp)?;
Ok(match msg.command {
// Everything that doesn't have a response value
@ -525,17 +525,15 @@ impl MarionetteSession {
ErrorStatus::UnknownError,
"Failed to interpret value as array"
);
let handles = try!(
data.iter()
.map(|x| {
Ok(try_opt!(
x.as_str(),
ErrorStatus::UnknownError,
"Failed to interpret window handle as string"
).to_owned())
})
.collect()
);
let handles = data
.iter()
.map(|x| {
Ok(try_opt!(
x.as_str(),
ErrorStatus::UnknownError,
"Failed to interpret window handle as string"
).to_owned())
}).collect::<Result<Vec<_>, _>>()?;
WebDriverResponse::CloseWindow(CloseWindowResponse(handles))
}
GetElementRect(_) => {
@ -652,11 +650,11 @@ impl MarionetteSession {
WebDriverResponse::Cookie(CookieResponse(cookie))
}
FindElement(_) | FindElementElement(_, _) => {
let element = try!(self.to_web_element(try_opt!(
let element = self.to_web_element(try_opt!(
resp.result.get("value"),
ErrorStatus::UnknownError,
"Failed to find value field"
)));
))?;
WebDriverResponse::Generic(ValueResponse(serde_json::to_value(element)?))
}
FindElements(_) | FindElementElements(_, _) => {
@ -665,12 +663,11 @@ impl MarionetteSession {
ErrorStatus::UnknownError,
"Failed to interpret value as array"
);
let elements = try!(
element_vec
.iter()
.map(|x| self.to_web_element(x))
.collect::<Result<Vec<_>, _>>()
);
let elements = element_vec
.iter()
.map(|x| self.to_web_element(x))
.collect::<Result<Vec<_>, _>>()?;
// TODO(Henrik): How to remove unwrap?
WebDriverResponse::Generic(ValueResponse(Value::Array(
elements
@ -680,11 +677,11 @@ impl MarionetteSession {
)))
}
GetActiveElement => {
let element = try!(self.to_web_element(try_opt!(
let element = self.to_web_element(try_opt!(
resp.result.get("value"),
ErrorStatus::UnknownError,
"Failed to find value field"
)));
))?;
WebDriverResponse::Generic(ValueResponse(serde_json::to_value(element)?))
}
NewSession(_) => {
@ -725,20 +722,19 @@ impl MarionetteSession {
ErrorStatus::UnknownError,
"Failed to interpret body as array"
);
let els = try!(
els_vec
.iter()
.map(|x| self.to_web_element(x))
.collect::<Result<Vec<_>, _>>()
);
let els = els_vec
.iter()
.map(|x| self.to_web_element(x))
.collect::<Result<Vec<_>, _>>()?;
WebDriverResponse::Generic(ValueResponse(serde_json::to_value(els)?))
}
XblAnonymousByAttribute(_, _) => {
let el = try!(self.to_web_element(try_opt!(
let el = self.to_web_element(try_opt!(
resp.result.get("value"),
ErrorStatus::UnknownError,
"Failed to find value field"
)));
))?;
WebDriverResponse::Generic(ValueResponse(serde_json::to_value(el)?))
}
InstallAddon(_) => WebDriverResponse::Generic(resp.to_value_response(true)?),
@ -829,13 +825,13 @@ impl MarionetteCommand {
ExecuteScript(ref x) => (Some("WebDriver:ExecuteScript"), Some(x.to_marionette())),
FindElement(ref x) => (Some("WebDriver:FindElement"), Some(x.to_marionette())),
FindElementElement(ref e, ref x) => {
let mut data = try!(x.to_marionette());
let mut data = x.to_marionette()?;
data.insert("element".to_string(), Value::String(e.id.clone()));
(Some("WebDriver:FindElement"), Some(Ok(data)))
}
FindElements(ref x) => (Some("WebDriver:FindElements"), Some(x.to_marionette())),
FindElementElements(ref e, ref x) => {
let mut data = try!(x.to_marionette());
let mut data = x.to_marionette()?;
data.insert("element".to_string(), Value::String(e.id.clone()));
(Some("WebDriver:FindElements"), Some(Ok(data)))
}
@ -969,7 +965,7 @@ impl MarionetteCommand {
ErrorStatus::UnsupportedOperation,
"Operation not supported"
);
let parameters = try!(opt_parameters.unwrap_or(Ok(Map::new())));
let parameters = opt_parameters.unwrap_or(Ok(Map::new()))?;
Ok(MarionetteCommand::new(id, name.into(), parameters))
}
@ -1244,7 +1240,7 @@ impl MarionetteConnection {
let stream = self.stream.as_mut().unwrap();
loop {
let buf = &mut [0 as u8];
let num_read = try!(stream.read(buf));
let num_read = stream.read(buf)?;
let byte = match num_read {
0 => {
return Err(IoError::new(
@ -1269,7 +1265,7 @@ impl MarionetteConnection {
let mut payload = Vec::with_capacity(bytes);
let mut total_read = 0;
while total_read < bytes {
let num_read = try!(stream.read(buf));
let num_read = stream.read(buf)?;
if num_read == 0 {
return Err(IoError::new(
ErrorKind::Other,
@ -1376,7 +1372,7 @@ impl ToMarionette for FrameId {
FrameId::Short(x) => data.insert("id".to_string(), serde_json::to_value(x)?),
FrameId::Element(ref x) => data.insert(
"element".to_string(),
Value::Object(try!(x.to_marionette())),
Value::Object(x.to_marionette()?),
),
};
Ok(data)

Просмотреть файл

@ -329,7 +329,7 @@ impl<U: WebDriverExtensionRoute> WebDriverMessage<U> {
WebDriverCommand::TakeElementScreenshot(element)
}
Route::Status => WebDriverCommand::Status,
Route::Extension(ref extension) => try!(extension.command(params, &body_data)),
Route::Extension(ref extension) => extension.command(params, &body_data)?,
};
Ok(WebDriverMessage::new(session_id, command))
}