Skip to main content

midnight_onchain_runtime/
error.rs

1// This file is part of midnight-ledger.
2// Copyright (C) 2025 Midnight Foundation
3// SPDX-License-Identifier: Apache-2.0
4// Licensed under the Apache License, Version 2.0 (the "License");
5// You may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7// http://www.apache.org/licenses/LICENSE-2.0
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14use base_crypto::fab::InvalidBuiltinDecode;
15use storage::db::DB;
16
17use crate::vm_error::OnchainProgramError;
18use std::error::Error;
19use std::fmt::{self, Display, Formatter};
20
21#[derive(Debug, Clone)]
22pub enum TranscriptRejected<D: DB> {
23    Execution(OnchainProgramError<D>),
24    Decode(InvalidBuiltinDecode),
25    FinalStackWrongLength,
26    WeakStateReturned,
27    EffectDecodeError,
28}
29
30impl<D: DB> From<OnchainProgramError<D>> for TranscriptRejected<D> {
31    fn from(err: OnchainProgramError<D>) -> Self {
32        TranscriptRejected::Execution(err)
33    }
34}
35
36impl<D: DB> From<InvalidBuiltinDecode> for TranscriptRejected<D> {
37    fn from(err: InvalidBuiltinDecode) -> Self {
38        TranscriptRejected::Decode(err)
39    }
40}
41
42impl<D: DB> Display for TranscriptRejected<D> {
43    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
44        use TranscriptRejected::*;
45        match self {
46            FinalStackWrongLength => write!(formatter, "final stack must be of length 3"),
47            WeakStateReturned => write!(
48                formatter,
49                "result state is was weak, and cannot be persisted"
50            ),
51            EffectDecodeError => write!(formatter, "failed to decode effect object"),
52            Execution(err) => err.fmt(formatter),
53            Decode(err) => err.fmt(formatter),
54        }
55    }
56}
57
58impl<D: DB> Error for TranscriptRejected<D> {}