• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

payjoin / rust-payjoin / 12954910137

24 Jan 2025 05:55PM UTC coverage: 78.385% (-0.2%) from 78.541%
12954910137

Pull #506

github

web-flow
Merge bbc06bbb6 into 3511be3c2
Pull Request #506: Produce `receive::JsonError` accurately so that `send` can properly handle it

22 of 91 new or added lines in 5 files covered. (24.18%)

3 existing lines in 1 file now uncovered.

3641 of 4645 relevant lines covered (78.39%)

989.58 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

15.79
/payjoin/src/receive/v2/error.rs
1
use core::fmt;
2
use std::error;
3

4
use super::Error;
5
use crate::hpke::HpkeError;
6
use crate::ohttp::OhttpEncapsulationError;
7

8
/// Error that may occur during a v2 session typestate change
9
///
10
/// This is currently opaque type because we aren't sure which variants will stay.
11
/// You can only display it.
12
#[derive(Debug)]
13
pub struct SessionError(InternalSessionError);
14

15
impl From<InternalSessionError> for SessionError {
16
    fn from(value: InternalSessionError) -> Self { SessionError(value) }
1✔
17
}
18

19
impl From<InternalSessionError> for super::Error {
20
    fn from(e: InternalSessionError) -> Self { super::Error::Validation(e.into()) }
1✔
21
}
22

23
#[derive(Debug)]
24
pub(crate) enum InternalSessionError {
25
    /// The session has expired
26
    Expired(std::time::SystemTime),
27
    /// OHTTP Encapsulation failed
28
    OhttpEncapsulation(OhttpEncapsulationError),
29
    /// Hybrid Public Key Encryption failed
30
    Hpke(HpkeError),
31
    /// Unexpected response size
32
    UnexpectedResponseSize(usize),
33
    /// Unexpected status code
34
    UnexpectedStatusCode(http::StatusCode),
35
}
36

37
impl From<std::time::SystemTime> for Error {
38
    fn from(e: std::time::SystemTime) -> Self { InternalSessionError::Expired(e).into() }
×
39
}
40

41
impl From<OhttpEncapsulationError> for Error {
42
    fn from(e: OhttpEncapsulationError) -> Self {
×
43
        InternalSessionError::OhttpEncapsulation(e).into()
×
44
    }
×
45
}
46

47
impl From<HpkeError> for Error {
48
    fn from(e: HpkeError) -> Self { InternalSessionError::Hpke(e).into() }
×
49
}
50

51
impl SessionError {
NEW
52
    pub fn to_json(&self) -> String {
×
53
        use InternalSessionError::*;
54

55
        use crate::receive::error::serialize_json_error;
NEW
56
        match &self.0 {
×
NEW
57
            Expired(_) => serialize_json_error("session-expired", self),
×
NEW
58
            OhttpEncapsulation(_) => serialize_json_error("ohttp-encapsulation-error", self),
×
NEW
59
            Hpke(_) => serialize_json_error("hpke-error", self),
×
NEW
60
            UnexpectedResponseSize(_) => serialize_json_error("unexpected-response-size", self),
×
NEW
61
            UnexpectedStatusCode(_) => serialize_json_error("unexpected-status-code", self),
×
62
        }
NEW
63
    }
×
64
}
65

66
impl fmt::Display for SessionError {
67
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1✔
68
        match &self.0 {
1✔
69
            InternalSessionError::Expired(expiry) => write!(f, "Session expired at {:?}", expiry),
1✔
70
            InternalSessionError::OhttpEncapsulation(e) =>
×
71
                write!(f, "OHTTP Encapsulation Error: {}", e),
×
72
            InternalSessionError::Hpke(e) => write!(f, "Hpke decryption failed: {}", e),
×
73
            InternalSessionError::UnexpectedResponseSize(size) => write!(
×
74
                f,
×
75
                "Unexpected response size {}, expected {} bytes",
×
76
                size,
×
77
                crate::directory::ENCAPSULATED_MESSAGE_BYTES
×
78
            ),
×
79
            InternalSessionError::UnexpectedStatusCode(status) =>
×
80
                write!(f, "Unexpected status code: {}", status),
×
81
        }
82
    }
1✔
83
}
84

85
impl error::Error for SessionError {
86
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
×
87
        match &self.0 {
×
88
            InternalSessionError::Expired(_) => None,
×
89
            InternalSessionError::OhttpEncapsulation(e) => Some(e),
×
90
            InternalSessionError::Hpke(e) => Some(e),
×
91
            InternalSessionError::UnexpectedResponseSize(_) => None,
×
92
            InternalSessionError::UnexpectedStatusCode(_) => None,
×
93
        }
94
    }
×
95
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc