Skip to content

Commit

Permalink
fix: Handling for Provider Error for generic (#615)
Browse files Browse the repository at this point in the history
* fix: Handling for Provider Error for generic

* fix: PR comments

* fix: PR comments

* fix: signed integer support for Provider Errors

* fix: For no response when null
  • Loading branch information
satlead authored Sep 4, 2024
1 parent 3fc9f71 commit 478587b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 27 deletions.
60 changes: 46 additions & 14 deletions core/main/src/firebolt/handlers/provider_registrar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,25 +119,31 @@ impl ProviderRegistrar {
});
}
}
ProviderResponsePayloadType::ChallengeError => {
ProviderResponsePayloadType::GenericError => {
let external_provider_error: Result<ExternalProviderError, CallError> =
params_sequence.next();

if let Ok(r) = external_provider_error {
return Some(ProviderResponse {
correlation_id: r.correlation_id,
result: ProviderResponsePayload::ChallengeError(r.error),
});
match external_provider_error {
Ok(r) => {
return Some(ProviderResponse {
correlation_id: r.correlation_id,
result: ProviderResponsePayload::GenericError(r.error),
});
}
Err(e) => error!("get provide response Error {:?}", e),
}
}
ProviderResponsePayloadType::Generic => {
let external_provider_response: Result<ExternalProviderResponse<Value>, CallError> =
params_sequence.next();
ProviderResponsePayloadType::GenericResponse => {
let external_provider_response: Result<
ExternalProviderResponse<Option<Value>>,
CallError,
> = params_sequence.next();

if let Ok(r) = external_provider_response {
return Some(ProviderResponse {
correlation_id: r.correlation_id,
result: ProviderResponsePayload::Generic(r.result),
result: ProviderResponsePayload::GenericResponse(
r.result.unwrap_or(Value::Null),
),
});
}
}
Expand Down Expand Up @@ -319,9 +325,14 @@ impl ProviderRegistrar {
) {
ProviderBroker::provider_response(&context.platform_state, provider_response).await;
}
} else if let Some(provider_response) = ProviderRegistrar::get_provider_response(
ProviderResponsePayloadType::GenericError,
params_sequence,
) {
ProviderBroker::provider_response(&context.platform_state, provider_response).await;
} else {
error!(
"callback_error: NO ATTRIBUTES: context.method={}",
"callback_error: NO Valid ATTRIBUTES: context.method={}",
context.method
);
return Err(Error::Custom(String::from("Missing provider attributes")));
Expand Down Expand Up @@ -461,7 +472,7 @@ impl ProviderRegistrar {

let response_payload_type = match &context.provider_relation_set.attributes {
Some(attributes) => attributes.response_payload_type.clone(),
None => ProviderResponsePayloadType::Generic,
None => ProviderResponsePayloadType::GenericResponse,
};

if let Some(provider_response) =
Expand Down Expand Up @@ -571,7 +582,7 @@ mod tests {

use super::*;
use jsonrpsee::core::server::rpc_module::Methods;
use ripple_sdk::tokio;
use ripple_sdk::{tokio, Mockable};

#[tokio::test]
async fn test_register_methods() {
Expand Down Expand Up @@ -833,4 +844,25 @@ mod tests {

assert!(!result);
}

#[test]
fn test_generic_error() {
let ctx = CallContext::mock();
let p = format!(
r#"[{},{{"correlationId":"someid","error":{{"code":-60001,"message":"The Player with 'ipa' id does not exist"}}}}]"#,
serde_json::to_string(&ctx).unwrap()
);
let params = Params::new(Some(&p));
let params_sequence = params.sequence();
let result = ProviderRegistrar::get_provider_response(
ProviderResponsePayloadType::GenericError,
params_sequence,
)
.unwrap();
assert!(result.correlation_id.eq("someid"));
if let ProviderResponsePayload::GenericError(c) = result.result {
assert!(c.code == -60001);
assert!(c.message.eq("The Player with 'ipa' id does not exist"))
}
}
}
26 changes: 13 additions & 13 deletions core/sdk/src/api/firebolt/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,26 @@ pub enum ProviderRequestPayload {
#[derive(Debug, Clone, Serialize)]
pub enum ProviderResponsePayloadType {
ChallengeResponse,
ChallengeError,
PinChallengeResponse,
KeyboardResult,
EntityInfoResponse,
PurchasedContentResponse,
Generic,
GenericResponse,
GenericError,
}

impl ToString for ProviderResponsePayloadType {
fn to_string(&self) -> String {
match self {
ProviderResponsePayloadType::ChallengeResponse => "ChallengeResponse".into(),
ProviderResponsePayloadType::ChallengeError => "ChallengeError".into(),
ProviderResponsePayloadType::PinChallengeResponse => "PinChallengeResponse".into(),
ProviderResponsePayloadType::KeyboardResult => "KeyboardResult".into(),
ProviderResponsePayloadType::EntityInfoResponse => "EntityInfoResponse".into(),
ProviderResponsePayloadType::PurchasedContentResponse => {
"PurchasedContentResponse".into()
}
ProviderResponsePayloadType::Generic => "GenericResponse".into(),
ProviderResponsePayloadType::GenericResponse => "GenericResponse".into(),
ProviderResponsePayloadType::GenericError => "GenericError".into(),
}
}
}
Expand All @@ -72,12 +72,12 @@ impl ToString for ProviderResponsePayloadType {
#[serde(untagged)]
pub enum ProviderResponsePayload {
ChallengeResponse(ChallengeResponse),
ChallengeError(ChallengeError),
GenericError(GenericProviderError),
PinChallengeResponse(PinChallengeResponse),
KeyboardResult(KeyboardSessionResponse),
EntityInfoResponse(Option<EntityInfoResult>),
PurchasedContentResponse(PurchasedContentResult),
Generic(serde_json::Value),
GenericResponse(serde_json::Value),
}

impl ProviderResponsePayload {
Expand Down Expand Up @@ -124,7 +124,7 @@ impl ProviderResponsePayload {
pub fn as_value(&self) -> serde_json::Value {
match self {
ProviderResponsePayload::ChallengeResponse(res) => serde_json::to_value(res).unwrap(),
ProviderResponsePayload::ChallengeError(res) => serde_json::to_value(res).unwrap(),
ProviderResponsePayload::GenericError(res) => serde_json::to_value(res).unwrap(),
ProviderResponsePayload::PinChallengeResponse(res) => {
serde_json::to_value(res).unwrap()
}
Expand All @@ -133,7 +133,7 @@ impl ProviderResponsePayload {
ProviderResponsePayload::PurchasedContentResponse(res) => {
serde_json::to_value(res).unwrap()
}
ProviderResponsePayload::Generic(res) => res.clone(),
ProviderResponsePayload::GenericResponse(res) => res.clone(),
}
}
}
Expand Down Expand Up @@ -170,7 +170,7 @@ pub struct ExternalProviderResponse<T> {
#[serde(rename_all = "camelCase")]
pub struct ExternalProviderError {
pub correlation_id: String,
pub error: ChallengeError,
pub error: GenericProviderError,
}

#[derive(Debug, Clone, Serialize)]
Expand All @@ -192,12 +192,12 @@ impl ProviderAttributes {

pub const ACKNOWLEDGE_CHALLENGE_ATTRIBS: ProviderAttributes = ProviderAttributes {
response_payload_type: ProviderResponsePayloadType::ChallengeResponse,
error_payload_type: ProviderResponsePayloadType::ChallengeError,
error_payload_type: ProviderResponsePayloadType::GenericError,
};

pub const PIN_CHALLENGE_ATTRIBS: ProviderAttributes = ProviderAttributes {
response_payload_type: ProviderResponsePayloadType::PinChallengeResponse,
error_payload_type: ProviderResponsePayloadType::ChallengeError,
error_payload_type: ProviderResponsePayloadType::GenericError,
};

#[derive(Debug, Serialize, Deserialize, Clone)]
Expand All @@ -212,8 +212,8 @@ pub struct DataObject {}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[cfg_attr(test, derive(PartialEq))]
pub struct ChallengeError {
pub code: u32,
pub struct GenericProviderError {
pub code: i32,
pub message: String,
pub data: Option<DataObject>,
}
Expand Down

0 comments on commit 478587b

Please sign in to comment.