Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace error strings with enums #417

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benchmark/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ fn try_main() -> ::capnp::Result<()> {
return Err(::capnp::Error::failed(format!(
"Could not parse a u64 from: {}",
args[5]
)))
)));
};

let mode = Mode::parse(&args[2])?;
Expand Down
2 changes: 1 addition & 1 deletion capnp-futures/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ where
R: AsyncRead + Unpin,
{
let Some(segment_lengths_builder) = read_segment_table(&mut reader, options).await? else {
return Ok(None)
return Ok(None);
};
Ok(Some(
read_segments(
Expand Down
14 changes: 9 additions & 5 deletions capnp-rpc/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,12 +355,16 @@ fn to_pipeline_ops(
}

fn from_error(error: &Error, mut builder: exception::Builder) {
builder.set_reason(&error.description);
builder.set_reason(&error.to_string());
let typ = match error.kind {
::capnp::ErrorKind::Failed => exception::Type::Failed,
::capnp::ErrorKind::Overloaded => exception::Type::Overloaded,
::capnp::ErrorKind::Disconnected => exception::Type::Disconnected,
::capnp::ErrorKind::Unimplemented => exception::Type::Unimplemented,
::capnp::ErrorKind::SettingDynamicCapabilitiesIsUnsupported => {
exception::Type::Unimplemented
}
_ => exception::Type::Failed,
};
builder.set_type(typ);
}
Expand All @@ -378,7 +382,7 @@ fn remote_exception_to_error(exception: exception::Reader) -> Error {
_ => (::capnp::ErrorKind::Failed, "(malformed error)"),
};
Error {
description: format!("remote exception: {reason}"),
extra: format!("remote exception: {reason}"),
kind,
}
}
Expand Down Expand Up @@ -647,7 +651,7 @@ impl<VatId> ConnectionState<VatId> {
let Some(state) = weak_state.upgrade() else {
return Promise::err(Error::disconnected(
"message loop cannot continue without a connection".into(),
))
));
};

let promise = match *state.connection.borrow_mut() {
Expand Down Expand Up @@ -894,7 +898,7 @@ impl<VatId> ConnectionState<VatId> {
let Some(connection_state) = weak_state.upgrade() else {
return Err(Error::disconnected(
"handle_message() cannot continue without a connection".into(),
))
));
};

let reader = message.get_body()?.get_as::<message::Reader>()?;
Expand Down Expand Up @@ -2041,7 +2045,7 @@ impl<VatId> Pipeline<VatId> {
let resolve_self_promise =
connection_state.eagerly_evaluate(fork.clone().then(move |response| {
let Some(state) = this.upgrade() else {
return Promise::err(Error::failed("dangling reference to this".into()))
return Promise::err(Error::failed("dangling reference to this".into()));
};
PipelineState::resolve(&state, response);
Promise::ok(())
Expand Down
4 changes: 2 additions & 2 deletions capnp-rpc/test/reconnect_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ macro_rules! assert_err {
let e1 = $e1;
let e2 = $e2;
assert_eq!(e1.kind, e2.kind);
if !e1.description.ends_with(&e2.description) {
assert_eq!(e1.description, e2.description);
if !e1.extra.ends_with(&e2.extra) {
assert_eq!(e1.extra, e2.extra);
}
};
}
Expand Down
4 changes: 1 addition & 3 deletions capnp-rpc/test/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,7 @@ fn pipelining_return_null() {
let cap = request.send().pipeline.get_cap();
match cap.foo_request().send().promise.await {
Err(ref e) => {
if e.description
.contains("Message contains null capability pointer")
{
if e.extra.contains("Message contains null capability pointer") {
Ok(())
} else {
Err(Error::failed(format!(
Expand Down
3 changes: 2 additions & 1 deletion capnp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ quickcheck = { version = "1", optional = true }
quickcheck = "1"

[features]
default = ["std"]
alloc = []
default = ["std", "alloc"]

rpc_try = []

Expand Down
18 changes: 16 additions & 2 deletions capnp/src/any_pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@

//! Untyped pointer that can be cast to any struct, list, or capability type.

use alloc::boxed::Box;
use alloc::vec::Vec;
#[cfg(feature = "alloc")]
use alloc::{boxed::Box, vec::Vec};

#[cfg(feature = "alloc")]
use crate::capability::FromClientHook;
#[cfg(feature = "alloc")]
use crate::private::capability::{ClientHook, PipelineHook, PipelineOp};
use crate::private::layout::{PointerBuilder, PointerReader};
use crate::traits::{FromPointerBuilder, FromPointerReader, SetPointerBuilder};
Expand All @@ -44,6 +46,7 @@ impl crate::introspect::Introspect for Owned {
}
}

#[cfg(feature = "alloc")]
impl crate::traits::Pipelined for Owned {
type Pipeline = Pipeline;
}
Expand Down Expand Up @@ -73,12 +76,14 @@ impl<'a> Reader<'a> {
FromPointerReader::get_from_pointer(&self.reader, None)
}

#[cfg(feature = "alloc")]
pub fn get_as_capability<T: FromClientHook>(&self) -> Result<T> {
Ok(FromClientHook::new(self.reader.get_capability()?))
}

//# Used by RPC system to implement pipelining. Applications
//# generally shouldn't use this directly.
#[cfg(feature = "alloc")]
pub fn get_pipelined_cap(&self, ops: &[PipelineOp]) -> Result<Box<dyn ClientHook>> {
let mut pointer = self.reader;

Expand Down Expand Up @@ -117,6 +122,7 @@ impl<'a> crate::traits::SetPointerBuilder for Reader<'a> {
}
}

#[cfg(feature = "alloc")]
impl<'a> crate::traits::Imbue<'a> for Reader<'a> {
fn imbue(&mut self, cap_table: &'a crate::private::layout::CapTable) {
self.reader
Expand Down Expand Up @@ -165,6 +171,7 @@ impl<'a> Builder<'a> {
}

// XXX value should be a user client.
#[cfg(feature = "alloc")]
pub fn set_as_capability(&mut self, value: Box<dyn ClientHook>) {
self.builder.set_capability(value);
}
Expand Down Expand Up @@ -199,20 +206,23 @@ impl<'a> FromPointerBuilder<'a> for Builder<'a> {
}
}

#[cfg(feature = "alloc")]
impl<'a> crate::traits::ImbueMut<'a> for Builder<'a> {
fn imbue_mut(&mut self, cap_table: &'a mut crate::private::layout::CapTable) {
self.builder
.imbue(crate::private::layout::CapTableBuilder::Plain(cap_table));
}
}

#[cfg(feature = "alloc")]
pub struct Pipeline {
// XXX this should not be public
pub hook: Box<dyn PipelineHook>,

ops: Vec<PipelineOp>,
}

#[cfg(feature = "alloc")]
impl Pipeline {
pub fn new(hook: Box<dyn PipelineHook>) -> Self {
Self {
Expand Down Expand Up @@ -245,24 +255,28 @@ impl Pipeline {
}
}

#[cfg(feature = "alloc")]
impl crate::capability::FromTypelessPipeline for Pipeline {
fn new(typeless: Pipeline) -> Self {
typeless
}
}

#[cfg(feature = "alloc")]
impl<'a> From<Reader<'a>> for crate::dynamic_value::Reader<'a> {
fn from(a: Reader<'a>) -> crate::dynamic_value::Reader<'a> {
crate::dynamic_value::Reader::AnyPointer(a)
}
}

#[cfg(feature = "alloc")]
impl<'a> From<Builder<'a>> for crate::dynamic_value::Builder<'a> {
fn from(a: Builder<'a>) -> crate::dynamic_value::Builder<'a> {
crate::dynamic_value::Builder::AnyPointer(a)
}
}

#[cfg(feature = "alloc")]
#[test]
fn init_clears_value() {
let mut message = crate::message::Builder::new_default();
Expand Down
2 changes: 2 additions & 0 deletions capnp/src/any_pointer_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ impl<'a> core::iter::IntoIterator for Reader<'a> {
}
}

#[cfg(feature = "alloc")]
impl<'a> From<Reader<'a>> for crate::dynamic_value::Reader<'a> {
fn from(t: Reader<'a>) -> crate::dynamic_value::Reader<'a> {
crate::dynamic_value::Reader::List(crate::dynamic_list::Reader::new(
Expand All @@ -198,6 +199,7 @@ impl<'a> From<Reader<'a>> for crate::dynamic_value::Reader<'a> {
}
}

#[cfg(feature = "alloc")]
impl<'a> From<Builder<'a>> for crate::dynamic_value::Builder<'a> {
fn from(t: Builder<'a>) -> crate::dynamic_value::Builder<'a> {
crate::dynamic_value::Builder::List(crate::dynamic_list::Builder::new(
Expand Down
1 change: 1 addition & 0 deletions capnp/src/capability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
//! Hooks for for the RPC system.
//!
//! Roughly corresponds to capability.h in the C++ implementation.
#![cfg(feature = "alloc")]

use alloc::boxed::Box;
use core::future::Future;
Expand Down
1 change: 1 addition & 0 deletions capnp/src/capability_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// THE SOFTWARE.

//! List of capabilities.
#![cfg(feature = "alloc")]

use alloc::boxed::Box;
use core::marker::PhantomData;
Expand Down
2 changes: 2 additions & 0 deletions capnp/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,14 @@ impl<'a> crate::traits::SetPointerBuilder for Reader<'a> {
}
}

#[cfg(feature = "alloc")]
impl<'a> From<Reader<'a>> for crate::dynamic_value::Reader<'a> {
fn from(d: Reader<'a>) -> crate::dynamic_value::Reader<'a> {
crate::dynamic_value::Reader::Data(d)
}
}

#[cfg(feature = "alloc")]
impl<'a> From<Builder<'a>> for crate::dynamic_value::Builder<'a> {
fn from(d: Builder<'a>) -> crate::dynamic_value::Builder<'a> {
crate::dynamic_value::Builder::Data(d)
Expand Down
2 changes: 2 additions & 0 deletions capnp/src/data_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ impl<'a> ::core::iter::IntoIterator for Reader<'a> {
}
}

#[cfg(feature = "alloc")]
impl<'a> From<Reader<'a>> for crate::dynamic_value::Reader<'a> {
fn from(t: Reader<'a>) -> crate::dynamic_value::Reader<'a> {
crate::dynamic_value::Reader::List(crate::dynamic_list::Reader {
Expand All @@ -208,6 +209,7 @@ impl<'a> From<Reader<'a>> for crate::dynamic_value::Reader<'a> {
}
}

#[cfg(feature = "alloc")]
impl<'a> From<Builder<'a>> for crate::dynamic_value::Builder<'a> {
fn from(t: Builder<'a>) -> crate::dynamic_value::Builder<'a> {
crate::dynamic_value::Builder::List(crate::dynamic_list::Builder {
Expand Down
25 changes: 11 additions & 14 deletions capnp/src/dynamic_list.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
//! Dynamically-typed lists.

#![cfg(feature = "alloc")]
use crate::dynamic_value;
use crate::introspect::{Type, TypeVariant};
use crate::private::layout::{self, PrimitiveElement};
use crate::traits::{IndexMove, ListIter};
use crate::Result;
use crate::{Error, ErrorKind, Result};

/// A read-only dynamically-typed list.
#[derive(Copy, Clone)]
Expand Down Expand Up @@ -321,13 +322,13 @@ impl<'a> Builder<'a> {
.reborrow()
.get_pointer_element(index)
.set_list(&list.reader, false),
(TypeVariant::AnyPointer, _) => Err(crate::Error::failed(
"List(AnyPointer) not supported".into(),
)),
(TypeVariant::Capability, dynamic_value::Reader::Capability(_)) => Err(
crate::Error::failed("List(Capability) not supported".into()),
),
(_, _) => Err(crate::Error::failed("Type mismatch".into())),
(TypeVariant::AnyPointer, _) => {
Err(Error::from_kind(ErrorKind::ListAnyPointerNotSupported))
}
(TypeVariant::Capability, dynamic_value::Reader::Capability(_)) => {
Err(Error::from_kind(ErrorKind::ListCapabilityNotSupported))
}
(_, _) => Err(Error::from_kind(ErrorKind::TypeMismatch)),
}
}

Expand All @@ -348,9 +349,7 @@ impl<'a> Builder<'a> {
| TypeVariant::Float64
| TypeVariant::Enum(_)
| TypeVariant::Struct(_)
| TypeVariant::Capability => {
Err(crate::Error::failed("Expected a list or blob.".into()))
}
| TypeVariant::Capability => Err(Error::from_kind(ErrorKind::ExpectedAListOrBlob)),
TypeVariant::Text => Ok(self
.builder
.get_pointer_element(index)
Expand Down Expand Up @@ -378,9 +377,7 @@ impl<'a> Builder<'a> {
)
.into()),
},
TypeVariant::AnyPointer => Err(crate::Error::failed(
"List(AnyPointer) not supported.".into(),
)),
TypeVariant::AnyPointer => Err(Error::from_kind(ErrorKind::ListAnyPointerNotSupported)),
}
}
}
Expand Down
Loading