diff --git a/shared/comm-opaque2/src/error.rs b/shared/comm-opaque2/src/error.rs index b303a4589..1a0157148 100644 --- a/shared/comm-opaque2/src/error.rs +++ b/shared/comm-opaque2/src/error.rs @@ -1,41 +1,47 @@ use opaque_ke::errors::ProtocolError; use std::ops::Deref; use wasm_bindgen::{JsError, JsValue}; /// Due to Rust's orphan rules, we cannot directly bridge /// opaque_ke::errors::ProtocolError to wasm_bindgen::JsValue. Instead we /// must define our own type, and add the impl's ourselves #[derive(Debug)] pub struct OpaqueError(ProtocolError); impl Into for OpaqueError { fn into(self) -> JsValue { JsValue::from(protocol_error_to_js_error(self.0)) } } +impl Into for OpaqueError { + fn into(self) -> ProtocolError { + self.0 + } +} + impl From for OpaqueError { fn from(error: ProtocolError) -> OpaqueError { OpaqueError(error) } } impl Deref for OpaqueError { type Target = ProtocolError; fn deref(&self) -> &Self::Target { &self.0 } } fn protocol_error_to_js_error(error: ProtocolError) -> JsError { match error { ProtocolError::IdentityGroupElementError => JsError::new("server error"), ProtocolError::InvalidLoginError => JsError::new("login failed"), ProtocolError::LibraryError(_) => JsError::new("internal error"), ProtocolError::ReflectedValueError => { JsError::new("invalid server response") } ProtocolError::SerializationError => JsError::new("invalid argument"), } } diff --git a/shared/comm-opaque2/src/grpc.rs b/shared/comm-opaque2/src/grpc.rs index 7b68369aa..5e8a72efc 100644 --- a/shared/comm-opaque2/src/grpc.rs +++ b/shared/comm-opaque2/src/grpc.rs @@ -1,28 +1,33 @@ +use crate::error::OpaqueError; use log::info; use opaque_ke::errors::ProtocolError; use tonic::Status; +pub fn opaque_error_to_grpc_status(error: OpaqueError) -> tonic::Status { + protocol_error_to_grpc_status(error.into()) +} + pub fn protocol_error_to_grpc_status(error: ProtocolError) -> tonic::Status { match error { ProtocolError::IdentityGroupElementError => { info!("Failed to validate password"); Status::aborted("server error") } ProtocolError::InvalidLoginError => { info!("Failed to login"); Status::aborted("login failed") } ProtocolError::LibraryError(_) => { info!("Opaque error"); Status::invalid_argument("internal error") } ProtocolError::ReflectedValueError => { info!("OPRF value was reflected"); Status::invalid_argument("invalid server response") } ProtocolError::SerializationError => { info!("Invalid argument"); Status::invalid_argument("invalid argument") } } }