use ave_actors::ActorError;
use ave_common::identity::DigestIdentifier;
use thiserror::Error;
use crate::{governance::error::GovernanceError, model::event::ProtocolsError};
#[derive(Debug, Error, Clone)]
pub enum RequestHandlerError {
#[error("helpers are not initialized")]
HelpersNotInitialized,
#[error("the payload cannot be deserialized as a governance event")]
GovFactInvalidEvent,
#[error("a user cannot mark a request approval as obsolete")]
ObsoleteApproval,
#[error(
"no approval found for subject '{0}', node likely no longer has approver role"
)]
ApprovalNotFound(String),
#[error("failed to change approval state")]
ApprovalChangeFailed,
#[error("failed to get approval state")]
ApprovalGetFailed,
#[error("not the owner of subject '{0}'")]
NotOwner(String),
#[error("subject '{0}' has a pending new owner")]
PendingNewOwner(String),
#[error("signer is the owner of subject '{0}', cannot confirm/reject")]
IsOwner(String),
#[error("signer is not the new owner of subject '{0}'")]
NotNewOwner(String),
#[error("no new owner pending for subject '{0}'")]
NoNewOwnerPending(String),
#[error("subject name must be between 1 and 100 characters")]
InvalidName,
#[error("subject description must be between 1 and 200 characters")]
InvalidDescription,
#[error("invalid schema_id in request")]
InvalidSchemaId,
#[error("governance creation must have empty governance_id")]
GovernanceIdMustBeEmpty,
#[error("governance creation must have empty namespace")]
NamespaceMustBeEmpty,
#[error("non-governance creation must have a governance_id")]
GovernanceIdRequired,
#[error("transfer event must have a non-empty new_owner")]
TransferNewOwnerEmpty,
#[error(
"governance confirm event name_old_owner cannot be empty if present"
)]
ConfirmNameOldOwnerEmpty,
#[error("tracker confirm event must not have name_old_owner")]
ConfirmTrackerNameOldOwner,
#[error("subject data not found for subject '{0}'")]
SubjectDataNotFound(String),
#[error("subject '{0}' is not active")]
SubjectNotActive(String),
#[error("creation events cannot be queued")]
CreationNotQueued,
#[error("failed to compute request_id: {0}")]
RequestIdHash(String),
#[error("failed to compute subject_id: {0}")]
SubjectIdHash(String),
#[error("request signature verification failed: {0}")]
SignatureVerification(String),
#[error("actor error: {0}")]
Actor(#[from] ActorError),
}
impl From<RequestHandlerError> for ActorError {
fn from(err: RequestHandlerError) -> Self {
match err {
RequestHandlerError::HelpersNotInitialized => {
Self::FunctionalCritical {
description: err.to_string(),
}
}
RequestHandlerError::Actor(e) => e,
_ => Self::Functional {
description: err.to_string(),
},
}
}
}
#[derive(Debug, Error, Clone)]
pub enum RequestManagerError {
#[error(
"the subject could not be created; the maximum limit has been reached."
)]
CheckLimit,
#[error("request is not set")]
RequestNotSet,
#[error("helpers (hash algorithm and network sender) are not initialized")]
HelpersNotInitialized,
#[error("invalid request state: expected {expected}, got {got}")]
InvalidRequestState {
expected: &'static str,
got: &'static str,
},
#[error("only Fact, Transfer and Confirm requests can be evaluated")]
InvalidEventRequestForEvaluation,
#[error("Confirm events on tracker subjects cannot be evaluated")]
ConfirmNotEvaluableForTracker,
#[error("no evaluators available for schema '{schema_id}'")]
NoEvaluatorsAvailable {
schema_id: String,
governance_id: DigestIdentifier,
},
#[error("no approvers available for schema '{schema_id}'")]
NoApproversAvailable {
schema_id: String,
governance_id: DigestIdentifier,
},
#[error("no validators available for schema '{schema_id}'")]
NoValidatorsAvailable {
schema_id: String,
governance_id: DigestIdentifier,
},
#[error("governance error: {0}")]
Governance(#[from] GovernanceError),
#[error("subject data not found for subject '{subject_id}'")]
SubjectDataNotFound { subject_id: String },
#[error("last ledger event not found for subject")]
LastLedgerEventNotFound,
#[error("failed to compute ledger hash: {details}")]
LedgerHashFailed { details: String },
#[error("failed to build protocols: {0}")]
ProtocolsBuild(#[from] ProtocolsError),
#[error("actor error: {0}")]
ActorError(#[from] ActorError),
#[error("Can not obtain SubjectData, is None")]
SubjecData,
#[error("In fact events, the signer has to be an issuer")]
NotIssuer,
}