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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use crate::language_storage::ModuleId;
use anyhow::{bail, Result};
use serde::{Deserialize, Serialize};
use std::{
collections::BTreeMap,
fs::File,
io::{Read, Write},
path::Path,
};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ErrorDescription {
pub code_name: String,
pub code_description: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ErrorContext {
pub category: ErrorDescription,
pub reason: ErrorDescription,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ErrorMapping {
pub error_categories: BTreeMap<u64, ErrorDescription>,
pub module_error_maps: BTreeMap<ModuleId, BTreeMap<u64, ErrorDescription>>,
}
impl Default for ErrorMapping {
fn default() -> Self {
Self {
error_categories: BTreeMap::new(),
module_error_maps: BTreeMap::new(),
}
}
}
impl ErrorMapping {
pub fn add_error_category(
&mut self,
category_id: u64,
description: ErrorDescription,
) -> Result<()> {
if let Some(previous_entry) = self.error_categories.insert(category_id, description) {
bail!(format!(
"Entry for category {} already taken by: {:#?}",
category_id, previous_entry
))
}
Ok(())
}
pub fn add_module_error(
&mut self,
module_id: ModuleId,
abort_code: u64,
description: ErrorDescription,
) -> Result<()> {
let module_error_map = self.module_error_maps.entry(module_id.clone()).or_default();
if let Some(previous_entry) = module_error_map.insert(abort_code, description) {
bail!(format!(
"Duplicate entry for abort code {} found in {}, previous entry: {:#?}",
abort_code, module_id, previous_entry
))
}
Ok(())
}
pub fn from_file<P: AsRef<Path>>(path: P) -> Self {
let mut bytes = Vec::new();
File::open(path).unwrap().read_to_end(&mut bytes).unwrap();
bcs::from_bytes(&bytes).unwrap()
}
pub fn to_file<P: AsRef<Path>>(&self, path: P) {
let bytes = bcs::to_bytes(self).unwrap();
let mut file = File::create(path).unwrap();
file.write_all(&bytes).unwrap();
}
pub fn get_explanation(&self, module: &ModuleId, output_code: u64) -> Option<ErrorContext> {
let category = output_code & 0xFFu64;
let reason_code = output_code >> 8;
self.error_categories.get(&category).and_then(|category| {
self.module_error_maps.get(module).and_then(|module_map| {
module_map.get(&reason_code).map(|reason| ErrorContext {
category: category.clone(),
reason: reason.clone(),
})
})
})
}
}