Skip to content

Commit

Permalink
[draft] tinyjson
Browse files Browse the repository at this point in the history
  • Loading branch information
DanGould committed Oct 20, 2023
1 parent f535666 commit 210ca40
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 36 deletions.
9 changes: 7 additions & 2 deletions payjoin-cli/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions payjoin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ bitcoin = { version = "0.30.0", features = ["base64"] }
bip21 = "0.3.1"
log = { version = "0.4.14"}
rand = { version = "0.8.4", optional = true }
serde = { version = "1.0.107", features = ["derive"] }
serde_json = "1.0"
tinyjson = "2"
url = "2.2.2"

[dev-dependencies]
Expand Down
50 changes: 24 additions & 26 deletions payjoin/src/send/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,6 @@ impl From<InternalCreateRequestError> for CreateRequestError {
fn from(value: InternalCreateRequestError) -> Self { CreateRequestError(value) }
}

use serde::Deserialize;

pub enum ResponseError {
// Well known errors with internal message for logs as String
WellKnown(WellKnownError, String),
Expand All @@ -222,6 +220,30 @@ pub enum ResponseError {
Validation(ValidationError),
}

impl ResponseError {
pub fn from_json(json: &str) -> Self {
use std::convert::TryInto;

use tinyjson::{JsonParser, JsonValue};

let parsed: JsonValue = json.parse().unwrap();
//.unwrap_or_else( |_| ResponseError::Validation(InternalValidationError::Parse.into()));
let maybe_code = parsed["errorCode"].get();
let maybe_message = parsed["message"].get();
if let (Some(error_code), Some(message)) = (maybe_code, maybe_message) {
let well_known_error = WellKnownError::from_str(&error_code);

if let Some(wk_error) = well_known_error {
ResponseError::WellKnown(wk_error, message.to_string())
} else {
ResponseError::Unrecognized(error_code.to_string(), message.to_string())
}
} else {
ResponseError::Validation(InternalValidationError::Parse.into())
}
}
}

impl From<InternalValidationError> for ResponseError {
fn from(value: InternalValidationError) -> Self {
Self::Validation(ValidationError { internal: value })
Expand Down Expand Up @@ -255,32 +277,8 @@ impl fmt::Debug for ResponseError {
}
}

impl<'de> Deserialize<'de> for ResponseError {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let res = RawResponseError::deserialize(deserializer)?;

let well_known_error = WellKnownError::from_str(&res.error_code);

if let Some(wk_error) = well_known_error {
Ok(ResponseError::WellKnown(wk_error, res.message))
} else {
Ok(ResponseError::Unrecognized(res.error_code, res.message))
}
}
}

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

#[derive(Debug, Deserialize)]
struct RawResponseError {
#[serde(rename = "errorCode")]
error_code: String,
message: String,
}

#[derive(Debug)]
pub enum WellKnownError {
Unavailable,
Expand Down
8 changes: 2 additions & 6 deletions payjoin/src/send/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,13 +354,9 @@ impl Context {
) -> Result<Psbt, ResponseError> {
let mut res_str = String::new();
response.read_to_string(&mut res_str).map_err(InternalValidationError::Io)?;
let proposal = Psbt::from_str(&res_str).or_else(|_| {
let proposal = Psbt::from_str(&res_str).map_err(|_| {
// That wasn't a valid PSBT. Maybe it's a valid error response?
serde_json::from_str::<ResponseError>(&res_str)
// which isn't the Ok result, it's actually an error.
.map(|e| Err(e))
// if not, we have an invalid response
.unwrap_or_else(|_| Err(InternalValidationError::Parse.into()))
ResponseError::from_json(&res_str)
})?;
self.process_proposal(proposal).map(Into::into).map_err(Into::into)
}
Expand Down

0 comments on commit 210ca40

Please sign in to comment.