/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ pub type Result = std::result::Result; // Functions which are part of the public API should use this Result. pub type ApiResult = std::result::Result; pub use error_support::{breadcrumb, handle_error, report_error}; pub use error_support::{debug, error, info, trace, warn}; use error_support::{ErrorHandling, GetErrorHandling}; use jwcrypto::JwCryptoError; // Errors we return via the public interface. #[derive(Debug, thiserror::Error)] pub enum DbCryptoApiError { #[error("NSS not initialized")] NSSUninitialized, #[error("NSS error during authentication: {reason}")] NSSAuthenticationError { reason: String }, #[error("error during authentication: {reason}")] AuthenticationError { reason: String }, #[error("authentication cancelled")] AuthenticationCanceled, #[error("Encryption key is missing.")] MissingKey, #[error("Encryption key is not valid.")] InvalidKey, #[error("encryption failed: {reason}")] EncryptionFailed { reason: String }, #[error("decryption failed: {reason}")] DecryptionFailed { reason: String }, #[error("{reason}")] Interrupted { reason: String }, #[error("Unexpected Error: {reason}")] UnexpectedDbCryptoApiError { reason: String }, } /// Encryption error type /// These are "internal" errors used by the implementation. This error type /// is never returned to the consumer. #[derive(Debug, thiserror::Error)] pub enum Error { #[error("encryption failed: {0:?}")] EncryptionFailed(String), #[error("decryption failed: {0:?}")] DecryptionFailed(String), #[error("CryptoError({0})")] CryptoError(#[from] JwCryptoError), #[error("IOError: {0}")] IOError(#[from] std::io::Error), } // Define how our internal errors are handled and converted to external errors // See `support/error/README.md` for how this works, especially the warning about PII. impl GetErrorHandling for Error { type ExternalError = DbCryptoApiError; fn get_error_handling(&self) -> ErrorHandling { // Unexpected errors that we report to Sentry. We should watch the reports for these // and do one or more of these things if we see them: // - Fix the underlying issue // - Add breadcrumbs or other context to help uncover the issue // - Decide that these are expected errors and create a case above to handle them ErrorHandling::convert(DbCryptoApiError::UnexpectedDbCryptoApiError { reason: self.to_string(), }) .report_error("encdec-unexpected") } } impl From for DbCryptoApiError { fn from(error: uniffi::UnexpectedUniFFICallbackError) -> Self { DbCryptoApiError::UnexpectedDbCryptoApiError { reason: error.to_string(), } } }