<commit_before>// Copyright (c) Aptos
// SPDX-License-Identifier: Apache-2.0

//! A module for looking up the human-readable explanation of a Aptos Move
//! transaction abort code.
//!
//! Note that the ~13 KiB error descriptions will be inlined into the final binary.

use move_deps::move_core_types::errmap::ErrorDescription;
use move_deps::move_core_types::{errmap::ErrorMapping, language_storage::ModuleId};
use once_cell::sync::Lazy;

static RELEASE_ERRMAP: Lazy<Vec<ErrorMapping>> = Lazy::new(|| {
    let error_map = cached_framework_packages::error_map();
    error_map
        .iter()
        .map(|e| bcs::from_bytes(e).expect("Failed to deserialize static error descriptions"))
        .collect()
});

/// Given the module ID and the abort code raised from that module, returns the
/// human-readable explanation of that abort if possible.
pub fn get_explanation(module_id: &ModuleId, abort_code: u64) -> Option<ErrorDescription> {
    let errmap = &*RELEASE_ERRMAP;
    errmap
        .iter()
        .find_map(|e| e.get_explanation(module_id, abort_code))
}

#[cfg(test)]
mod tests {
    use super::*;
    use move_deps::move_core_types::{account_address::AccountAddress, ident_str};

    #[test]
    fn test_get_explanation() {
        let module_id = ModuleId::new(AccountAddress::ZERO, ident_str!("TESTTEST").to_owned());
        // We don't care about the result, just that the errmap deserializes without panicking.
        let _ = get_explanation(&module_id, 1234);
    }
}<commit_msg>[sdk] bring back error codes in particular for the web interface<commit_after>// Copyright (c) Aptos
// SPDX-License-Identifier: Apache-2.0

//! A module for looking up the human-readable explanation of a Aptos Move
//! transaction abort code.
//!
//! Note that the ~13 KiB error descriptions will be inlined into the final binary.

use move_deps::move_core_types::errmap::ErrorDescription;
use move_deps::move_core_types::{errmap::ErrorMapping, language_storage::ModuleId};
use once_cell::sync::Lazy;

static RELEASE_ERRMAP: Lazy<Vec<ErrorMapping>> = Lazy::new(|| {
    let error_map = cached_framework_packages::error_map();
    error_map
        .iter()
        .map(|e| bcs::from_bytes(e).expect("Failed to deserialize static error descriptions"))
        .collect()
});

/// Given the module ID and the abort code raised from that module, returns the
/// human-readable explanation of that abort if possible.
pub fn get_explanation(module_id: &ModuleId, abort_code: u64) -> Option<ErrorDescription> {
    let errmap = &*RELEASE_ERRMAP;
    errmap
        .iter()
        .find_map(|e| e.get_explanation(module_id, abort_code))
        .or_else(|| {
            errmap
                .iter()
                .find_map(|e| e.get_explanation(module_id, abort_code & 0xffff))
        })
}

#[cfg(test)]
mod tests {
    use super::*;
    use move_deps::move_core_types::{account_address::AccountAddress, ident_str};

    #[test]
    fn test_get_explanation() {
        let module_id = ModuleId::new(AccountAddress::ONE, ident_str!("coin").to_owned());
        // If this breaks, well then someone adjusted the error codes...
        let no_category = get_explanation(&module_id, 4).unwrap();
        let category = get_explanation(&module_id, 65540).unwrap();
        // bcs because ErrorDescription doesn't support `==`
        assert_eq!(
            bcs::to_bytes(&no_category).unwrap(),
            bcs::to_bytes(&category).unwrap()
        );
    }
}
{'fraction_non_alphanumeric': 0.126878612716763, 'fraction_numerical': 0.006647398843930636, 'mean_word_length': 3.627005347593583, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7239642', 'n_tokens_mistral': 1084, 'n_tokens_neox': 1053, 'n_words': 279}