|
| 1 | +use thiserror::Error; |
| 2 | +pub type Result<T> = std::result::Result<T, Error>; |
| 3 | + |
| 4 | +#[derive(Error, Debug)] |
| 5 | +/// Defines all possible errors that can occur in this library |
| 6 | +pub enum Error { |
| 7 | + #[error("Assertion {reason}")] |
| 8 | + Assertion { reason: String }, |
| 9 | + #[error("AssertionNotFound {reason}")] |
| 10 | + AssertionNotFound { reason: String }, |
| 11 | + #[error("Decoding {reason}")] |
| 12 | + Decoding{ reason: String }, |
| 13 | + #[error("Encoding {reason}")] |
| 14 | + Encoding { reason: String }, |
| 15 | + #[error("FileNotFound{reason}")] |
| 16 | + FileNotFound { reason: String }, |
| 17 | + #[error("Io {reason}")] |
| 18 | + Io { reason: String }, |
| 19 | + #[error("Json {reason}")] |
| 20 | + Json { reason: String }, |
| 21 | + #[error("Manifest {reason}")] |
| 22 | + Manifest { reason: String }, |
| 23 | + #[error("ManifestNotFound {reason}")] |
| 24 | + ManifestNotFound { reason: String }, |
| 25 | + #[error("NotSupported {reason}")] |
| 26 | + NotSupported { reason: String }, |
| 27 | + #[error("Other {reason}")] |
| 28 | + Other { reason: String }, |
| 29 | + #[error("Remote {reason}")] |
| 30 | + RemoteManifest { reason: String }, |
| 31 | + #[error("ResourceNotFound {reason}")] |
| 32 | + ResourceNotFound { reason: String }, |
| 33 | + #[error("RwLock")] |
| 34 | + RwLock, |
| 35 | + #[error("Signature {reason}")] |
| 36 | + Signature { reason: String }, |
| 37 | + #[error("Verify{reason}")] |
| 38 | + Verify { reason: String }, |
| 39 | +} |
| 40 | + |
| 41 | +impl Error { |
| 42 | + // Convert c2pa errors to published API errors |
| 43 | + #[allow(unused_variables)] |
| 44 | + pub(crate) fn from_c2pa_error(err: c2pa::Error) -> Self { |
| 45 | + use c2pa::Error::*; |
| 46 | + let err_str = err.to_string(); |
| 47 | + match err { |
| 48 | + c2pa::Error::AssertionMissing { url } => Self::AssertionNotFound{ reason: "".to_string()}, |
| 49 | + AssertionInvalidRedaction |
| 50 | + | AssertionRedactionNotFound |
| 51 | + | AssertionUnsupportedVersion => Self::Assertion{ reason: err_str}, |
| 52 | + ClaimAlreadySigned |
| 53 | + | ClaimUnsigned |
| 54 | + | ClaimMissingSignatureBox |
| 55 | + | ClaimMissingIdentity |
| 56 | + | ClaimVersion |
| 57 | + | ClaimInvalidContent |
| 58 | + | ClaimMissingHardBinding |
| 59 | + | ClaimSelfRedact |
| 60 | + | ClaimDisallowedRedaction |
| 61 | + | UpdateManifestInvalid |
| 62 | + | TooManyManifestStores => Self::Manifest{ reason: err_str}, |
| 63 | + ClaimMissing { label } => Self::ManifestNotFound{ reason: err_str}, |
| 64 | + AssertionDecoding(_) | ClaimDecoding => Self::Decoding{ reason: err_str}, |
| 65 | + AssertionEncoding | XmlWriteError | ClaimEncoding => Self::Encoding{ reason: err_str}, |
| 66 | + InvalidCoseSignature { coset_error } => Self::Signature{ reason: err_str}, |
| 67 | + CoseSignatureAlgorithmNotSupported |
| 68 | + | CoseMissingKey |
| 69 | + | CoseX5ChainMissing |
| 70 | + | CoseInvalidCert |
| 71 | + | CoseSignature |
| 72 | + | CoseVerifier |
| 73 | + | CoseCertExpiration |
| 74 | + | CoseCertRevoked |
| 75 | + | CoseInvalidTimeStamp |
| 76 | + | CoseTimeStampValidity |
| 77 | + | CoseTimeStampMismatch |
| 78 | + | CoseTimeStampGeneration |
| 79 | + | CoseTimeStampAuthority |
| 80 | + | CoseSigboxTooSmall |
| 81 | + | InvalidEcdsaSignature => Self::Signature{ reason: err_str}, |
| 82 | + RemoteManifestFetch(_) | RemoteManifestUrl(_) => Self::RemoteManifest{ reason: err_str}, |
| 83 | + JumbfNotFound => Self::ManifestNotFound{ reason: err_str}, |
| 84 | + BadParam(_) | MissingFeature(_) => Self::Other{ reason: err_str}, |
| 85 | + IoError(_) => Self::Io{ reason: err_str}, |
| 86 | + JsonError(e) => Self::Json{ reason: err_str}, |
| 87 | + NotFound | ResourceNotFound(_) | MissingDataBox => Self::ResourceNotFound{ reason: err_str}, |
| 88 | + FileNotFound(_) => Self::FileNotFound{ reason: err_str}, |
| 89 | + UnsupportedType => Self::NotSupported{ reason: err_str}, |
| 90 | + ClaimVerification(_) | InvalidClaim(_) | JumbfParseError(_) => Self::Verify{ reason: err_str}, |
| 91 | + #[cfg(feature = "add_thumbnails")] |
| 92 | + ImageError => Self::ImageError(err_str), |
| 93 | + _ => Self::Other{ reason: err_str}, |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +impl From<uniffi::UnexpectedUniFFICallbackError> for Error { |
| 99 | + fn from(err: uniffi::UnexpectedUniFFICallbackError) -> Self { |
| 100 | + Self::Other { |
| 101 | + reason: err.reason.clone(), |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +impl From<c2pa::Error> for Error { |
| 107 | + fn from(err: c2pa::Error) -> Self { |
| 108 | + Self::from_c2pa_error(err) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +impl From<std::io::Error> for Error { |
| 113 | + fn from(err: std::io::Error) -> Self { |
| 114 | + Self::Io { reason: err.to_string()} |
| 115 | + } |
| 116 | +} |
| 117 | + |
0 commit comments