From 9c5aa63e4bb74e5890b8ffeee49bf8888d2dff5b Mon Sep 17 00:00:00 2001 From: Yuniru Yuni Date: Tue, 18 Apr 2023 15:42:05 +0900 Subject: [PATCH] impl Error to improve compatibility impl std::error::Error for WhisperError to improve error compatibility. std::error::Error is trait for erorrs and it's standard. and some of error handling libraries requires Error implemented struct for Result. for examples: error-stack --- src/error.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/error.rs b/src/error.rs index e92841f..3623e89 100644 --- a/src/error.rs +++ b/src/error.rs @@ -61,3 +61,59 @@ impl From for WhisperError { } } } + +impl std::fmt::Display for WhisperError { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + use WhisperError::*; + match self { + InitError => write!(f, "Failed to create a new whisper context."), + SpectrogramNotInitialized => write!(f, "User didn't initialize spectrogram."), + EncodeNotComplete => write!(f, "Encode was not called."), + DecodeNotComplete => write!(f, "Decode was not called."), + UnableToCalculateSpectrogram => { + write!(f, "Failed to calculate the spectrogram for some reason.") + } + UnableToCalculateEvaluation => write!(f, "Failed to evaluate model."), + FailedToEncode => write!(f, "Failed to run the encoder."), + FailedToDecode => write!(f, "Failed to run the decoder."), + InvalidMelBands => write!(f, "Invalid number of mel bands."), + InvalidThreadCount => write!(f, "Invalid thread count."), + InvalidUtf8 { + valid_up_to, + error_len: Some(len), + } => write!( + f, + "Invalid UTF-8 detected in a string from Whisper. Index: {}, Length: {}.", + valid_up_to, len + ), + InvalidUtf8 { + valid_up_to, + error_len: None, + } => write!( + f, + "Invalid UTF-8 detected in a string from Whisper. Index: {}.", + valid_up_to + ), + NullByteInString { idx } => write!( + f, + "A null byte was detected in a user-provided string. Index: {}", + idx + ), + NullPointer => write!(f, "Whisper returned a null pointer."), + InvalidText => write!( + f, + "Whisper failed to convert the provided text into tokens." + ), + FailedToCreateState => write!(f, "Creating a state pointer failed."), + StateIdAlreadyExists => write!(f, "State pointer ID already exists."), + StateIdDoesNotExist => write!(f, "State pointer ID does not exist."), + GenericError(c_int) => write!( + f, + "Generic whisper error. Varies depending on the function. Error code: {}", + c_int + ), + } + } +} + +impl std::error::Error for WhisperError {}