aquavm/air/src/execution_step/errors/catchable_errors.rs

160 lines
5.5 KiB
Rust
Raw Normal View History

2021-01-15 00:38:58 +03:00
/*
2024-06-26 12:34:36 +03:00
* AquaVM Workflow Engine
2021-01-15 00:38:58 +03:00
*
2024-06-26 12:34:36 +03:00
* Copyright (C) 2024 Fluence DAO
2021-01-15 00:38:58 +03:00
*
2024-06-26 12:34:36 +03:00
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation version 3 of the
* License.
2021-01-15 00:38:58 +03:00
*
2024-06-26 12:34:36 +03:00
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2021-01-15 00:38:58 +03:00
*/
use super::ErrorAffectable;
2021-12-28 16:59:55 +03:00
use super::Joinable;
use crate::execution_step::execution_context::errors::StreamMapError;
use crate::execution_step::execution_context::ErrorObjectError;
use crate::execution_step::lambda_applier::LambdaError;
2021-01-15 00:38:58 +03:00
use crate::JValue;
2021-12-17 22:02:16 +03:00
use crate::ToErrorCode;
2021-01-15 00:38:58 +03:00
use strum::IntoEnumIterator;
use strum_macros::EnumDiscriminants;
use strum_macros::EnumIter;
2021-01-15 00:38:58 +03:00
use thiserror::Error as ThisError;
2021-02-11 15:39:37 +03:00
use std::rc::Rc;
2021-12-21 11:37:35 +03:00
/// Catchable errors arisen during AIR script execution. Catchable here means that these errors
/// could be handled by a xor instruction and their error_code could be used in a match
/// instruction.
#[derive(ThisError, EnumDiscriminants, Debug, Clone)]
#[strum_discriminants(derive(EnumIter))]
2021-12-21 11:37:35 +03:00
pub enum CatchableError {
2021-01-15 00:38:58 +03:00
/// An error is occurred while calling local service via call_service.
#[error("Local service error, ret_code is {0}, error message is '{1}'")]
2021-02-11 15:39:37 +03:00
LocalServiceError(i32, Rc<String>),
2021-01-15 00:38:58 +03:00
/// This error type is produced by a match to notify xor that compared values aren't equal.
#[error("compared values do not match")]
MatchValuesNotEqual,
/// This error type is produced by a mismatch to notify xor that compared values aren't equal.
#[error("compared values do not mismatch")]
MismatchValuesEqual,
/// Variable with such a name wasn't defined during AIR script execution.
2021-12-21 11:37:35 +03:00
/// This error type is used in order to support the join behaviour and
/// it's ok if some variable hasn't been defined yet, due to the par nature of AIR.
#[error("variable with name '{0}' wasn't defined during script execution")]
2021-01-15 00:38:58 +03:00
VariableNotFound(String),
/// Provided JValue has incompatible type with a requested one.
#[error(
"expected JValue type '{expected_value_type}' for the variable `{variable_name}`, but got '{actual_value}'"
)]
IncompatibleJValueType {
variable_name: String,
actual_value: JValue,
expected_value_type: &'static str,
},
2021-01-15 00:38:58 +03:00
/// A fold instruction must iterate over array value.
#[error("expression '{1}' returned non-array value '{0}' for fold iterable")]
FoldIteratesOverNonArray(JValue, String),
2021-12-16 21:34:27 +03:00
/// This error type is produced by a fail instruction.
2021-12-28 16:59:55 +03:00
#[error("fail with '{error}' is used without corresponding xor")]
UserError { error: JValue },
2021-12-16 21:34:27 +03:00
2021-12-21 11:37:35 +03:00
/// An error occurred while trying to apply lambda to a value.
#[error(transparent)]
2021-12-21 11:37:35 +03:00
LambdaApplierError(#[from] LambdaError),
2021-08-24 16:14:15 +03:00
2021-12-29 19:51:18 +03:00
/// This error type is produced by a fail instruction that tries to throw a scalar that have inappropriate type.
#[error(transparent)]
InvalidErrorObjectError(#[from] ErrorObjectError),
/// A new with this variable name was met and right after that it was accessed
/// that is prohibited.
#[error("variable with name '{0}' was cleared by new and then wasn't set")]
VariableWasNotInitializedAfterNew(String),
2022-08-26 00:43:43 +03:00
2022-09-08 16:58:04 +03:00
/// This error type is occurred when the length functor applied to a value of non-array type.
#[error("the length functor could applied only to an array-like value, but it's applied to '{0}'")]
LengthFunctorAppliedToNotArray(JValue),
/// Call gets non-string JValue resolving triplet parts.
#[error("call cannot resolve non-String triplet variable part `{variable_name}` with value '{actual_value}'")]
NonStringValueInTripletResolution {
variable_name: String,
actual_value: JValue,
},
/// Stream map related errors.
#[error(transparent)]
StreamMapError(#[from] StreamMapError),
2021-08-24 16:14:15 +03:00
}
2021-12-21 11:37:35 +03:00
impl From<LambdaError> for Rc<CatchableError> {
fn from(e: LambdaError) -> Self {
2021-12-21 11:37:35 +03:00
Rc::new(CatchableError::LambdaApplierError(e))
}
}
2021-12-21 11:37:35 +03:00
impl ToErrorCode for Rc<CatchableError> {
2021-12-17 22:02:16 +03:00
fn to_error_code(&self) -> i64 {
2021-12-21 11:37:35 +03:00
self.as_ref().to_error_code()
2021-12-17 22:02:16 +03:00
}
}
2021-12-21 11:37:35 +03:00
impl ToErrorCode for CatchableError {
2021-12-17 22:02:16 +03:00
fn to_error_code(&self) -> i64 {
2021-12-21 11:37:35 +03:00
use crate::utils::CATCHABLE_ERRORS_START_ID;
crate::generate_to_error_code!(self, CatchableError, CATCHABLE_ERRORS_START_ID)
2021-01-15 00:38:58 +03:00
}
}
impl ErrorAffectable for CatchableError {
2021-12-28 16:59:55 +03:00
fn affects_last_error(&self) -> bool {
!matches!(
self,
CatchableError::MatchValuesNotEqual | CatchableError::MismatchValuesEqual
)
}
fn affects_error(&self) -> bool {
true
}
2021-12-28 16:59:55 +03:00
}
macro_rules! log_join {
($($args:tt)*) => {
log::trace!(target: air_log_targets::JOIN_BEHAVIOUR, $($args)*)
}
}
#[rustfmt::skip::macros(log_join)]
2021-12-21 11:37:35 +03:00
impl Joinable for CatchableError {
/// Returns true, if supplied error is related to variable not found errors type.
/// Print log if this is joinable error type.
fn is_joinable(&self) -> bool {
2021-12-21 11:37:35 +03:00
use CatchableError::*;
match self {
VariableNotFound(var_name) => {
log_join!(" waiting for an argument with name '{}'", var_name);
true
}
_ => false,
}
}
}