sugar_cli/
errors.rs

1use std::{
2    path::PathBuf,
3    sync::{Arc, Mutex},
4};
5
6use serde::Serialize;
7use thiserror::Error;
8
9use crate::common::*;
10
11#[derive(Debug, Error)]
12pub enum SetupError {
13    #[error("Error setting up sugar: {0}")]
14    SugarSetupError(String),
15}
16
17#[derive(Debug, Error)]
18pub enum CacheError {
19    #[error("Cache file '{0}' not found. Run `sugar upload` to create it or provide it with the --cache option.")]
20    CacheFileNotFound(String),
21
22    #[error("Invalid candy machine address: '{0}'. Check your cache file or run deploy to ensure your candy machine was created.")]
23    InvalidCandyMachineAddress(String),
24
25    #[error("Failed to open cache file: {0} with error: {1}")]
26    FailedToOpenCacheFile(String, String),
27
28    #[error("Failed to parse cache file with error: {0}")]
29    CacheFileWrongFormat(String),
30
31    #[error("Invalid cache state found.")]
32    InvalidState,
33}
34
35#[derive(Debug, Error)]
36pub enum CustomCandyError {
37    #[error("Payer key '{0}' does not equal the Candy Machine authority pubkey '{1}'")]
38    AuthorityMismatch(String, String),
39}
40
41#[derive(Debug, Error)]
42pub enum FloatConversionError {
43    #[error("Conversion failed with an overflow")]
44    Overflow,
45
46    #[error("Conversion failed because of a fractional component")]
47    Fractional,
48}
49
50#[derive(Debug, Serialize)]
51pub struct ValidateError<'a> {
52    pub path: &'a PathBuf,
53    pub error: String,
54}
55
56pub fn log_errors<T: std::fmt::Debug + Serialize>(
57    error_type: &str,
58    errors: Arc<Mutex<Vec<T>>>,
59) -> Result<()> {
60    let errors = &*errors.lock().unwrap();
61    error!("{error_type}: {errors:?}");
62    let f = File::create("validate_errors.json")?;
63    serde_json::to_writer_pretty(f, &errors)?;
64
65    Ok(())
66}