Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Commit

Permalink
Add standard TbdexError (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
KendallWeihe authored Sep 5, 2024
1 parent e48b64f commit a0f0bad
Show file tree
Hide file tree
Showing 48 changed files with 1,203 additions and 1,119 deletions.
57 changes: 13 additions & 44 deletions bindings/tbdex_uniffi/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
use serde_json::Error as SerdeJsonError;
use std::fmt::Debug;
use std::sync::PoisonError;
use std::{any::type_name, fmt::Debug};
use tbdex::http_client::HttpClientError;
use tbdex::json::JsonError;
use tbdex::messages::MessageError;
use tbdex::resources::ResourceError;
use tbdex::errors::TbdexError as InnerTbdexError;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum TbdexSdkError {
pub enum TbdexError {
#[error("{msg}")]
Error {
r#type: String,
variant: String,
msg: String,
},
Error { variant: String, msg: String },
}

impl TbdexSdkError {
pub fn from_poison_error<T>(error: PoisonError<T>, error_type: &str) -> Self {
TbdexSdkError::Error {
r#type: error_type.to_string(),
impl TbdexError {
pub fn from_poison_error<T>(error: PoisonError<T>) -> Self {
TbdexError::Error {
variant: "PoisonError".to_string(),
msg: error.to_string(),
}
Expand All @@ -30,17 +22,12 @@ impl TbdexSdkError {
T: std::error::Error + 'static,
{
Self::Error {
r#type: type_of(&error).to_string(),
variant: variant_name(&error),
msg: error.to_string(),
}
}
}

fn type_of<T>(_: &T) -> &'static str {
type_name::<T>()
}

fn variant_name<T>(error: &T) -> String
where
T: Debug,
Expand All @@ -50,34 +37,16 @@ where
variant_name.to_string()
}

impl From<ResourceError> for TbdexSdkError {
fn from(error: ResourceError) -> Self {
TbdexSdkError::new(error)
}
}

impl From<MessageError> for TbdexSdkError {
fn from(error: MessageError) -> Self {
TbdexSdkError::new(error)
}
}

impl From<HttpClientError> for TbdexSdkError {
fn from(error: HttpClientError) -> Self {
TbdexSdkError::new(error)
}
}

impl From<JsonError> for TbdexSdkError {
fn from(error: JsonError) -> Self {
TbdexSdkError::new(error)
impl From<InnerTbdexError> for TbdexError {
fn from(error: InnerTbdexError) -> Self {
TbdexError::new(error)
}
}

impl From<SerdeJsonError> for TbdexSdkError {
impl From<SerdeJsonError> for TbdexError {
fn from(error: SerdeJsonError) -> Self {
TbdexSdkError::new(error)
TbdexError::new(error)
}
}

pub type Result<T> = std::result::Result<T, TbdexSdkError>;
pub type Result<T> = std::result::Result<T, TbdexError>;
4 changes: 2 additions & 2 deletions bindings/tbdex_uniffi/src/http/exchanges.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
errors::{Result, TbdexSdkError},
errors::{Result, TbdexError},
messages::rfq::Rfq,
};
use std::sync::Arc;
Expand Down Expand Up @@ -64,7 +64,7 @@ impl GetExchangeResponseBody {
.0
.data
.iter()
.map(|i| Message::from_json_string(&i.json_serialized).map_err(TbdexSdkError::from))
.map(|i| Message::from_json_string(&i.json_serialized).map_err(TbdexError::from))
.collect::<Result<Vec<Message>>>()?,
};
Ok(inner.to_json_string()?)
Expand Down
2 changes: 1 addition & 1 deletion bindings/tbdex_uniffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod resources;
mod errors;

use crate::{
errors::TbdexSdkError,
errors::TbdexError,
http::{
balances::{GetBalancesResponseBody, GetBalancesResponseBodyData},
exchanges::{
Expand Down
22 changes: 5 additions & 17 deletions bindings/tbdex_uniffi/src/messages/cancel.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::errors::{Result, TbdexSdkError};
use crate::errors::{Result, TbdexError};
use std::sync::{Arc, RwLock};
use tbdex::{
json::{FromJson, ToJson},
Expand All @@ -23,10 +23,7 @@ impl Cancel {
}

pub fn sign(&self, bearer_did: Arc<BearerDid>) -> Result<()> {
let mut inner_close = self
.0
.write()
.map_err(|e| TbdexSdkError::from_poison_error(e, "RwLockWriteError"))?;
let mut inner_close = self.0.write().map_err(TbdexError::from_poison_error)?;
inner_close.sign(&bearer_did.0.clone())?;
Ok(())
}
Expand All @@ -38,28 +35,19 @@ impl Cancel {
}

pub fn to_json_string(&self) -> Result<String> {
let inner_close = self
.0
.read()
.map_err(|e| TbdexSdkError::from_poison_error(e, "RwLockReadError"))?;
let inner_close = self.0.read().map_err(TbdexError::from_poison_error)?;

Ok(inner_close.to_json_string()?)
}

pub fn get_data(&self) -> Result<InnerCancel> {
let close = self
.0
.read()
.map_err(|e| TbdexSdkError::from_poison_error(e, "RwLockReadError"))?;
let close = self.0.read().map_err(TbdexError::from_poison_error)?;

Ok(close.clone())
}

pub fn verify(&self) -> Result<()> {
let cancel = self
.0
.read()
.map_err(|e| TbdexSdkError::from_poison_error(e, "RwLockReadError"))?;
let cancel = self.0.read().map_err(TbdexError::from_poison_error)?;

Ok(cancel.verify()?)
}
Expand Down
22 changes: 5 additions & 17 deletions bindings/tbdex_uniffi/src/messages/close.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::errors::{Result, TbdexSdkError};
use crate::errors::{Result, TbdexError};
use std::sync::{Arc, RwLock};
use tbdex::{
json::{FromJson, ToJson},
Expand All @@ -23,10 +23,7 @@ impl Close {
}

pub fn sign(&self, bearer_did: Arc<BearerDid>) -> Result<()> {
let mut inner_close = self
.0
.write()
.map_err(|e| TbdexSdkError::from_poison_error(e, "RwLockWriteError"))?;
let mut inner_close = self.0.write().map_err(TbdexError::from_poison_error)?;
inner_close.sign(&bearer_did.0.clone())?;
Ok(())
}
Expand All @@ -38,28 +35,19 @@ impl Close {
}

pub fn to_json_string(&self) -> Result<String> {
let inner_close = self
.0
.read()
.map_err(|e| TbdexSdkError::from_poison_error(e, "RwLockReadError"))?;
let inner_close = self.0.read().map_err(TbdexError::from_poison_error)?;

Ok(inner_close.to_json_string()?)
}

pub fn get_data(&self) -> Result<InnerClose> {
let close = self
.0
.read()
.map_err(|e| TbdexSdkError::from_poison_error(e, "RwLockReadError"))?;
let close = self.0.read().map_err(TbdexError::from_poison_error)?;

Ok(close.clone())
}

pub fn verify(&self) -> Result<()> {
let close = self
.0
.read()
.map_err(|e| TbdexSdkError::from_poison_error(e, "RwLockReadError"))?;
let close = self.0.read().map_err(TbdexError::from_poison_error)?;

Ok(close.verify()?)
}
Expand Down
22 changes: 5 additions & 17 deletions bindings/tbdex_uniffi/src/messages/order.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::errors::{Result, TbdexSdkError};
use crate::errors::{Result, TbdexError};
use std::sync::{Arc, RwLock};
use tbdex::{
json::{FromJson, ToJson},
Expand All @@ -22,10 +22,7 @@ impl Order {
}

pub fn sign(&self, bearer_did: Arc<BearerDid>) -> Result<()> {
let mut inner_order = self
.0
.write()
.map_err(|e| TbdexSdkError::from_poison_error(e, "RwLockWriteError"))?;
let mut inner_order = self.0.write().map_err(TbdexError::from_poison_error)?;
inner_order.sign(&bearer_did.0.clone())?;
Ok(())
}
Expand All @@ -37,28 +34,19 @@ impl Order {
}

pub fn to_json_string(&self) -> Result<String> {
let inner_order = self
.0
.read()
.map_err(|e| TbdexSdkError::from_poison_error(e, "RwLockReadError"))?;
let inner_order = self.0.read().map_err(TbdexError::from_poison_error)?;

Ok(inner_order.to_json_string()?)
}

pub fn get_data(&self) -> Result<InnerOrder> {
let order = self
.0
.read()
.map_err(|e| TbdexSdkError::from_poison_error(e, "RwLockReadError"))?;
let order = self.0.read().map_err(TbdexError::from_poison_error)?;

Ok(order.clone())
}

pub fn verify(&self) -> Result<()> {
let order = self
.0
.read()
.map_err(|e| TbdexSdkError::from_poison_error(e, "RwLockReadError"))?;
let order = self.0.read().map_err(TbdexError::from_poison_error)?;

Ok(order.verify()?)
}
Expand Down
22 changes: 5 additions & 17 deletions bindings/tbdex_uniffi/src/messages/order_instructions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::errors::{Result, TbdexSdkError};
use crate::errors::{Result, TbdexError};
use std::sync::{Arc, RwLock};
use tbdex::{
json::{FromJson, ToJson},
Expand Down Expand Up @@ -26,10 +26,7 @@ impl OrderInstructions {
}

pub fn sign(&self, bearer_did: Arc<BearerDid>) -> Result<()> {
let mut inner_order_instructions = self
.0
.write()
.map_err(|e| TbdexSdkError::from_poison_error(e, "RwLockWriteError"))?;
let mut inner_order_instructions = self.0.write().map_err(TbdexError::from_poison_error)?;
inner_order_instructions.sign(&bearer_did.0.clone())?;
Ok(())
}
Expand All @@ -41,28 +38,19 @@ impl OrderInstructions {
}

pub fn to_json_string(&self) -> Result<String> {
let inner_order_instructions = self
.0
.read()
.map_err(|e| TbdexSdkError::from_poison_error(e, "RwLockReadError"))?;
let inner_order_instructions = self.0.read().map_err(TbdexError::from_poison_error)?;

Ok(inner_order_instructions.to_json_string()?)
}

pub fn get_data(&self) -> Result<InnerOrderInstructions> {
let order_instructions = self
.0
.read()
.map_err(|e| TbdexSdkError::from_poison_error(e, "RwLockReadError"))?;
let order_instructions = self.0.read().map_err(TbdexError::from_poison_error)?;

Ok(order_instructions.clone())
}

pub fn verify(&self) -> Result<()> {
let order_instructions = self
.0
.read()
.map_err(|e| TbdexSdkError::from_poison_error(e, "RwLockReadError"))?;
let order_instructions = self.0.read().map_err(TbdexError::from_poison_error)?;

Ok(order_instructions.verify()?)
}
Expand Down
22 changes: 5 additions & 17 deletions bindings/tbdex_uniffi/src/messages/order_status.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::errors::{Result, TbdexSdkError};
use crate::errors::{Result, TbdexError};
use std::sync::{Arc, RwLock};
use tbdex::{
json::{FromJson, ToJson},
Expand All @@ -24,10 +24,7 @@ impl OrderStatus {
}

pub fn sign(&self, bearer_did: Arc<BearerDid>) -> Result<()> {
let mut inner_order_status = self
.0
.write()
.map_err(|e| TbdexSdkError::from_poison_error(e, "RwLockWriteError"))?;
let mut inner_order_status = self.0.write().map_err(TbdexError::from_poison_error)?;
inner_order_status.sign(&bearer_did.0.clone())?;
Ok(())
}
Expand All @@ -39,28 +36,19 @@ impl OrderStatus {
}

pub fn to_json_string(&self) -> Result<String> {
let inner_order_status = self
.0
.read()
.map_err(|e| TbdexSdkError::from_poison_error(e, "RwLockReadError"))?;
let inner_order_status = self.0.read().map_err(TbdexError::from_poison_error)?;

Ok(inner_order_status.to_json_string()?)
}

pub fn get_data(&self) -> Result<InnerOrderStatus> {
let order_status = self
.0
.read()
.map_err(|e| TbdexSdkError::from_poison_error(e, "RwLockReadError"))?;
let order_status = self.0.read().map_err(TbdexError::from_poison_error)?;

Ok(order_status.clone())
}

pub fn verify(&self) -> Result<()> {
let order_status = self
.0
.read()
.map_err(|e| TbdexSdkError::from_poison_error(e, "RwLockReadError"))?;
let order_status = self.0.read().map_err(TbdexError::from_poison_error)?;

Ok(order_status.verify()?)
}
Expand Down
Loading

0 comments on commit a0f0bad

Please sign in to comment.