handling errors on intrustion execution and data parsing

This commit is contained in:
vms 2020-09-30 15:00:43 +03:00
parent 94e8351294
commit 09750d1060
8 changed files with 69 additions and 39 deletions

View File

@ -14,8 +14,9 @@
* limitations under the License.
*/
use super::StepperOutcome;
use crate::StepperOutcome;
use serde_json::Error as SerdeJsonError;
use serde_sexpr::Error as SExprError;
use std::convert::Into;
@ -23,8 +24,11 @@ use std::error::Error;
#[derive(Debug)]
pub enum AquamarineError {
/// FaaS errors.
ParseError(SExprError),
/// Errors occurred while parsing aqua script in the form of S expressions.
SExprParseError(SExprError),
/// Errors occurred while parsing supplied data.
DataParseError(SerdeJsonError),
/// Aquamarine result deserialization errors.
ExecutionError(String),
@ -35,7 +39,8 @@ impl Error for AquamarineError {}
impl std::fmt::Display for AquamarineError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
match self {
AquamarineError::ParseError(err) => write!(f, "{}", err),
AquamarineError::SExprParseError(err) => write!(f, "{}", err),
AquamarineError::DataParseError(err) => write!(f, "{}", err),
AquamarineError::ExecutionError(err_msg) => write!(f, "{}", err_msg),
}
}
@ -43,7 +48,13 @@ impl std::fmt::Display for AquamarineError {
impl From<SExprError> for AquamarineError {
fn from(err: SExprError) -> Self {
AquamarineError::ParseError(err)
AquamarineError::SExprParseError(err)
}
}
impl From<SerdeJsonError> for AquamarineError {
fn from(err: SerdeJsonError) -> Self {
AquamarineError::DataParseError(err)
}
}
@ -55,17 +66,16 @@ impl From<std::convert::Infallible> for AquamarineError {
impl Into<StepperOutcome> for AquamarineError {
fn into(self) -> StepperOutcome {
match self {
AquamarineError::ParseError(err) => StepperOutcome {
ret_code: 1,
data: format!("{}", err),
next_peer_pks: vec![],
},
AquamarineError::ExecutionError(err) => StepperOutcome {
ret_code: 2,
data: err,
next_peer_pks: vec![],
},
let ret_code = match self {
AquamarineError::SExprParseError(_) => 1,
AquamarineError::DataParseError(_) => 2,
AquamarineError::ExecutionError(_) => 3,
};
StepperOutcome {
ret_code,
data: format!("{}", self),
next_peer_pks: vec![],
}
}
}

View File

@ -15,11 +15,16 @@
*/
mod air;
mod errors;
mod instructions;
mod stepper;
pub(crate) type Result<T> = std::result::Result<T, AquamarineError>;
pub(crate) type AquaData = serde_json::Value;
pub(crate) use crate::stepper::StepperOutcome;
pub(crate) use errors::AquamarineError;
use crate::stepper::execute_aqua;
use crate::stepper::StepperOutcome;
use fluence::fce;
pub fn main() {

View File

@ -14,8 +14,11 @@
* limitations under the License.
*/
use serde_derive::{Deserialize, Serialize};
use std::collections::HashMap;
use crate::AquaData;
use crate::Result;
use serde_derive::Deserialize;
use serde_derive::Serialize;
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
pub(crate) struct Call {
@ -26,7 +29,7 @@ pub(crate) struct Call {
}
impl super::ExecutableInstruction for Call {
fn execute(self, _data: &mut HashMap<String, Vec<u8>>) {
fn execute(self, _data: &mut AquaData) -> Result<()> {
let service_id = match (self.peer_part.1, self.fn_part.0) {
(Some(service_id), None) => service_id,
(None, Some(service_id)) => service_id,
@ -34,5 +37,7 @@ impl super::ExecutableInstruction for Call {
};
let _result = unsafe { crate::call_service(service_id, self.fn_part.1, self.args) };
Ok(())
}
}

View File

@ -16,14 +16,13 @@
mod call;
pub(crate) use call::Call;
pub(self) use crate::stepper::ExecutableInstruction;
use crate::AquaData;
use crate::Result;
pub(crate) use call::Call;
use serde_derive::Deserialize;
use serde_derive::Serialize;
use std::collections::hash_map::RandomState;
use std::collections::HashMap;
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
@ -38,9 +37,9 @@ pub(crate) enum Instruction {
}
impl ExecutableInstruction for Instruction {
fn execute(self, data: &mut HashMap<String, Vec<u8>, RandomState>) {
fn execute(self, data: &mut AquaData) -> Result<()> {
match self {
Instruction::Null => {}
Instruction::Null => Ok(()),
Instruction::Call(call) => call.execute(data),
}
}

View File

@ -14,9 +14,10 @@
* limitations under the License.
*/
use super::Result;
use super::StepperOutcome;
use crate::instructions::Instruction;
use crate::AquaData;
use crate::Result;
pub(crate) fn execute_aqua(init_user_id: String, aqua: String, data: String) -> StepperOutcome {
log::info!(
@ -30,10 +31,17 @@ pub(crate) fn execute_aqua(init_user_id: String, aqua: String, data: String) ->
}
fn execute_aqua_impl(init_user_id: String, aqua: String, data: String) -> Result<StepperOutcome> {
let mut parsed_data: AquaData = serde_json::from_str(&data)?;
let parsed_aqua = serde_sexpr::from_str::<Vec<Instruction>>(&aqua)?;
log::info!("parsed_aqua: {:?}", parsed_aqua);
super::stepper::execute(parsed_aqua);
log::info!(
"parsed_aqua: {:?}\nparsed_data: {:?}",
parsed_aqua,
parsed_data
);
let data = super::stepper::execute(parsed_aqua, &mut parsed_data)?;
let data = serde_json::to_string(&data)?;
Ok(StepperOutcome {
ret_code: 0,

View File

@ -14,15 +14,11 @@
* limitations under the License.
*/
mod errors;
mod execution;
mod stepper;
mod stepper_outcome;
pub use stepper_outcome::StepperOutcome;
pub(crate) use errors::AquamarineError;
pub(crate) use execution::execute_aqua;
pub(crate) use stepper::ExecutableInstruction;
pub(self) type Result<T> = std::result::Result<T, AquamarineError>;

View File

@ -15,16 +15,17 @@
*/
use crate::instructions::Instruction;
use std::collections::HashMap;
use crate::AquaData;
use crate::Result;
pub(crate) trait ExecutableInstruction {
fn execute(self, data: &mut HashMap<String, Vec<u8>>);
fn execute(self, data: &mut AquaData) -> Result<()>;
}
pub(crate) fn execute(instructions: Vec<Instruction>) {
let mut data = HashMap::new();
pub(crate) fn execute(instructions: Vec<Instruction>, data: &mut AquaData) -> Result<()> {
for instruction in instructions {
instruction.execute(&mut data);
instruction.execute(data)?;
}
Ok(())
}

View File

@ -13,10 +13,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
mod air;
mod errors;
mod instructions;
mod stepper;
pub(crate) type Result<T> = std::result::Result<T, errors::AquamarineError>;
pub(crate) type AquaData = std::collections::HashMap<String, Vec<u8>>;
pub(crate) use crate::stepper::StepperOutcome;
use crate::stepper::execute_aqua;
use wasm_bindgen::prelude::*;