Move shared response processing code to ohttp.rs#660
Move shared response processing code to ohttp.rs#660spacebear21 merged 2 commits intopayjoin:masterfrom
Conversation
|
Leaving in draft for now because some follow-up items need to be addressed:
|
Pull Request Test Coverage Report for Build 15500115610Details
💛 - Coveralls |
d72153b to
141b559
Compare
|
are there other from conversions that can be deleted? |
|
The error audit I mentioned in my other comment can be addressed later in a follow-up, I'll make a new issue.
I had a second commit locally that I hadn't pushed which simplifies the From mappings. The PR is still a net addition but I think the simplification is significant. |
There was a problem hiding this comment.
1 request and 1 concern
- I think there's a more concise and semantically correct way to write
process_get_reswhich might solve a mutant. See below. - I think some of the
Fromimpls forDirectoryResponseErrormay be even more verbose than changes to makemap_errwork while expanding our public API and should be reconsidered
Fighting a head cold so warning these comments might be less sane than usual.
payjoin/src/ohttp.rs
Outdated
| let body = match response.status() { | ||
| http::StatusCode::OK => response.body().to_vec(), | ||
| http::StatusCode::ACCEPTED => return Ok(None), | ||
| _ => return Err(DirectoryResponseError::UnexpectedStatusCode(response.status())), | ||
| }; | ||
| Ok(Some(body)) |
There was a problem hiding this comment.
I know this was carried over, but I think it's more appropriately written as the following, which might address @benalleng's mutant concern as well
| let body = match response.status() { | |
| http::StatusCode::OK => response.body().to_vec(), | |
| http::StatusCode::ACCEPTED => return Ok(None), | |
| _ => return Err(DirectoryResponseError::UnexpectedStatusCode(response.status())), | |
| }; | |
| Ok(Some(body)) | |
| match response.status() { | |
| http::StatusCode::OK => Ok(Some(response.body().to_vec())), | |
| http::StatusCode::ACCEPTED => Ok(None), | |
| code => Err(DirectoryResponseError::UnexpectedStatusCode(code)), | |
| } |
this addresses 2 separate issues: 1. replying with Some(body) outside of the match and using the single response.status() call as the context for the UnexpectedStatusCode error.
payjoin/src/receive/v2/error.rs
Outdated
| DirectoryResponseError::InvalidSize(e) => | ||
| InternalSessionError::UnexpectedResponseSize(e), | ||
| DirectoryResponseError::OhttpDecapsulation(e) => | ||
| InternalSessionError::OhttpEncapsulation(e), | ||
| DirectoryResponseError::UnexpectedStatusCode(e) => | ||
| InternalSessionError::UnexpectedStatusCode(e), |
There was a problem hiding this comment.
Why should InternalSessionError not have a BadResponse variant that encapsulates DirectoryResponseError?
There was a problem hiding this comment.
I believe this is addressed in the second commit.
payjoin/src/receive/v2/error.rs
Outdated
| impl From<DirectoryResponseError> for Error { | ||
| fn from(value: DirectoryResponseError) -> Self { V2(value.into()) } | ||
| } |
There was a problem hiding this comment.
It doesn't seem to me like this should necessarily be coerced into a V2 error. It could be a multiparty or later error, and so should be explicitly mapped at the call site.
payjoin/src/send/v2/error.rs
Outdated
|
|
||
| impl From<DirectoryResponseError> for EncapsulationError { | ||
| fn from(value: DirectoryResponseError) -> Self { | ||
| match value { | ||
| DirectoryResponseError::InvalidSize(e) => InternalEncapsulationError::InvalidSize(e), | ||
| DirectoryResponseError::OhttpDecapsulation(e) => InternalEncapsulationError::Ohttp(e), | ||
| DirectoryResponseError::UnexpectedStatusCode(e) => | ||
| InternalEncapsulationError::UnexpectedStatusCode(e), | ||
| } | ||
| .into() | ||
| } | ||
| } | ||
|
|
||
| impl From<DirectoryResponseError> for ResponseError { | ||
| fn from(value: DirectoryResponseError) -> Self { | ||
| ResponseError::Validation( | ||
| super::InternalValidationError::V2Encapsulation(value.into()).into(), | ||
| ) | ||
| } | ||
| } |
There was a problem hiding this comment.
Same as above, it seems like this conversion is only done once (which could be done with map_err or an Err(e) match arm) making the From unnecessarily verbose, and the EncapsulationError seems like it could just have a DirectoryResponseError unless these InvalidSize, OhttpDecapsulation, UnexpectedStatusCode variants crop up outside of the context of a directory response.
payjoin/src/receive/v2/error.rs
Outdated
| impl From<DirectoryResponseError> for Error { | ||
| fn from(e: DirectoryResponseError) -> Self { InternalSessionError::DirectoryResponse(e).into() } | ||
| } | ||
|
|
||
| impl From<DirectoryResponseError> for SessionError { | ||
| fn from(e: DirectoryResponseError) -> Self { InternalSessionError::DirectoryResponse(e).into() } | ||
| } | ||
|
|
There was a problem hiding this comment.
Ah, I see this commit obviates my earlier comments. However, it still seems like it depends on context which one of these it gets converted to, which suggests to me you still want a map instead of the conversion (which happens on only few occasions)
daef0ac to
3b2dc34
Compare
|
The latest push fixes |
The `process_res` methods across send and receive have a lot of shared code that can be extracted into reusable functions. `process_get_res` and `process_post_res` essentially do the same thing, and could conceivably be combined into one function. They were kept separate because "202 Accepted" is not a valid response for POST requests.
Instead of mapping DirectoryResponseError to the corresponding variants on InternalSessionError and InternalCreateRequestError, nest those variants by replacing them with DirectoryResponseError.
3b2dc34 to
ec4b67e
Compare
|
ec4b67e only applies your suggestions re: |
In payjoin#660 we added a process_get_res and process_post_res methods in ohttps to pull away some of the logic from our send module but multiparty remained the same. This PR attempts pull sync the multiparty sender error handling with this new approach. As well this change gets rid of the remaining mutant on this status code match block.
In payjoin#660 we added a process_get_res and process_post_res methods in ohttps to pull away some of the logic from our send module but multiparty remained the same. This PR attempts pull sync the multiparty sender error handling with this new approach. As well this change gets rid of the remaining mutant on this status code match block.
In payjoin#660 we added a process_get_res and process_post_res methods in ohttps to pull away some of the logic from our send module but multiparty remained the same. This PR attempts pull sync the multiparty sender error handling with this new approach. As well this change gets rid of the remaining mutant on this status code match block.
In payjoin#660 we added a process_get_res and process_post_res methods in ohttps to pull away some of the logic from our send module but multiparty remained the same. This PR attempts pull sync the multiparty sender error handling with this new approach. As well this change gets rid of the remaining mutant on this status code match block.
In payjoin#660 we added a process_get_res and process_post_res methods in ohttps to pull away some of the logic from our send module but multiparty remained the same. This PR attempts pull sync the multiparty sender error handling with this new approach. As well this change gets rid of the remaining mutant on this status code match block.
In payjoin#660 we added a process_get_res and process_post_res methods in ohttps to pull away some of the logic from our send module but multiparty remained the same. This PR attempts pull sync the multiparty sender error handling with this new approach. As well this change gets rid of the remaining mutant on this status code match block.
The
process_resmethods across send and receive have a lot of shared code that can be extracted into reusable functions.process_get_resandprocess_post_resessentially do the same thing, and could conceivably be combined into one function. They were kept separate because "202 Accepted" is not a valid response for POST requests.