Skip to content

Commit

Permalink
client: Include version info and interface name in BindError
Browse files Browse the repository at this point in the history
When `BindError` is returned (and bubbled up by callers) it is no longer
clear what interface or version was requested (nor what is the highest
this server might be able to provide), leaving users to wonder what the
context for the error is.

Add this information to the object to elaborate in error messages and
allow callers to read the values after the fact.
  • Loading branch information
MarijnS95 committed Dec 18, 2023
1 parent 7e98c79 commit 068d582
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
5 changes: 5 additions & 0 deletions wayland-client/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
## Unreleased

#### Additions

- Implement `Eq` for `Connection`

#### Breaking changes

- `BindError` now includes the requested and available version, or interface name that failed to bind.

## 0.31.1 -- 2023-09-19

#### Additions
Expand Down
45 changes: 24 additions & 21 deletions wayland-client/src/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl GlobalList {
/// version of the returned protocol object is the lower of the maximum requested version and the advertised
/// version.
///
/// If the lower bound of the `version` is less than the version advertised by the server, then
/// If the lower bound of the `version` is greater than the version advertised by the server, then
/// [`BindError::UnsupportedVersion`] is returned.
///
/// ## Multi-instance/Device globals.
Expand Down Expand Up @@ -152,23 +152,19 @@ impl GlobalList {

let globals = &self.registry.data::<GlobalListContents>().unwrap().contents;
let guard = globals.lock().unwrap();
let (name, version) = guard
let &Global { name, version, .. } = guard
.iter()
// Find the with the correct interface
.filter_map(|Global { name, interface: interface_name, version }| {
// TODO: then_some
if interface.name == &interface_name[..] {
Some((*name, *version))
} else {
None
}
})
.next()
.ok_or(BindError::NotPresent)?;
// Find the global with the correct interface
.find(|Global { interface: interface_name, .. }| interface.name == interface_name)
.ok_or(BindError::NotPresent(interface.name))?;

// Test version requirements
if version < version_start {
return Err(BindError::UnsupportedVersion);
if version_start > version {
return Err(BindError::UnsupportedVersion {
interface: interface.name,
requested: version_start,
available: version,
});
}

// To get the version to bind, take the lower of the version advertised by the server and the maximum
Expand Down Expand Up @@ -232,22 +228,29 @@ impl From<InvalidId> for GlobalError {
#[derive(Debug)]
pub enum BindError {
/// The requested version of the global is not supported.
UnsupportedVersion,
UnsupportedVersion {
/// The name of the global for which the server provides a too low value.
interface: &'static str,
/// The lowest version that was requested by the caller, must be greater than [`Self::UnsupportedVersion::requested`].
requested: u32,
/// The actual verison that was available on the server, must be less than [`Self::UnsupportedVersion::requested`].
available: u32,
},

/// The requested global was not found in the registry.
NotPresent,
NotPresent(&'static str),
}

impl std::error::Error for BindError {}

impl fmt::Display for BindError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
BindError::UnsupportedVersion {} => {
write!(f, "the requested version of the global is not supported")
BindError::UnsupportedVersion { interface, requested, available } => {
write!(f, "the requested version `{requested}` of the global `{interface}` is not supported, only `{available}` is available")

Check warning on line 250 in wayland-client/src/globals.rs

View check run for this annotation

Codecov / codecov/patch

wayland-client/src/globals.rs#L249-L250

Added lines #L249 - L250 were not covered by tests
}
BindError::NotPresent {} => {
write!(f, "the requested global was not found in the registry")
BindError::NotPresent(name) => {
write!(f, "the requested global `{name}` was not found in the registry")

Check warning on line 253 in wayland-client/src/globals.rs

View check run for this annotation

Codecov / codecov/patch

wayland-client/src/globals.rs#L252-L253

Added lines #L252 - L253 were not covered by tests
}
}
}
Expand Down

0 comments on commit 068d582

Please sign in to comment.