1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
 * AquaVM Workflow Engine
 *
 * Copyright (C) 2024 Fluence DAO
 *
 * 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.
 *
 * 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/>.
 */

use air_interpreter_data::Provenance;

use super::instruction_error_definition::error_from_raw_fields;
use super::instruction_error_definition::error_from_raw_fields_w_peerid;
use super::InstructionError;
use crate::execution_step::ErrorAffectable;
use crate::execution_step::RcSecurityTetraplet;
use crate::JValue;
use crate::ToErrorCode;

pub(crate) fn get_instruction_error_from_exec_error(
    error: &(impl ErrorAffectable + ToErrorCode + ToString),
    instruction: &str,
    peer_id_option: Option<&str>,
    tetraplet: Option<RcSecurityTetraplet>,
) -> InstructionError {
    // it is not a call result, but generated from a limited set of unjoinable errors
    let provenance = Provenance::literal();

    get_instruction_error_from_ingredients(
        error.to_error_code(),
        &error.to_string(),
        instruction,
        peer_id_option,
        tetraplet,
        provenance,
    )
}

pub(crate) fn get_instruction_error_from_ingredients(
    error_code: i64,
    error_message: &str,
    instruction: &str,
    peer_id_option: Option<&str>,
    tetraplet: Option<RcSecurityTetraplet>,
    provenance: Provenance,
) -> InstructionError {
    let error_object = match peer_id_option {
        Some(peer_id) => error_from_raw_fields_w_peerid(error_code, error_message, instruction, peer_id),
        None => error_from_raw_fields(error_code, error_message, instruction),
    };
    get_instruction_error_from_error_object(error_object, tetraplet, provenance)
}

pub(crate) fn get_instruction_error_from_error_object(
    error: JValue,
    tetraplet: Option<RcSecurityTetraplet>,
    provenance: Provenance,
) -> InstructionError {
    let orig_catchable = None;
    InstructionError {
        error,
        tetraplet,
        provenance,
        orig_catchable,
    }
}