feat: complete Epic 1 production foundation
This commit is contained in:
1 parent
767428436d
commit
182bde8ac0
298 files changed
+35711
-5291
No files matched your search
@@ -0,0 +1,234 @@
|
||||
use crank_core::{
|
||||
CorrelationContext, DispatchEvidence, ExecutionErrorCode, ExecutionFailure,
|
||||
ProtocolAdapterError,
|
||||
};
|
||||
|
||||
use crate::{RuntimeError, RuntimeOperation};
|
||||
|
||||
pub fn normalize_runtime_error(
|
||||
error: &RuntimeError,
|
||||
correlation: &CorrelationContext,
|
||||
) -> ExecutionFailure {
|
||||
normalize_runtime_error_with_effect(error, correlation, false)
|
||||
}
|
||||
|
||||
pub fn normalize_runtime_error_for_operation(
|
||||
error: &RuntimeError,
|
||||
correlation: &CorrelationContext,
|
||||
operation: &RuntimeOperation,
|
||||
) -> ExecutionFailure {
|
||||
let may_mutate = matches!(
|
||||
&operation.target,
|
||||
crank_core::Target::Rest(target)
|
||||
if matches!(
|
||||
target.method,
|
||||
crank_core::HttpMethod::Post
|
||||
| crank_core::HttpMethod::Put
|
||||
| crank_core::HttpMethod::Patch
|
||||
| crank_core::HttpMethod::Delete
|
||||
)
|
||||
);
|
||||
normalize_runtime_error_with_effect(error, correlation, may_mutate)
|
||||
}
|
||||
|
||||
fn normalize_runtime_error_with_effect(
|
||||
error: &RuntimeError,
|
||||
correlation: &CorrelationContext,
|
||||
may_mutate: bool,
|
||||
) -> ExecutionFailure {
|
||||
let mut failure = ExecutionFailure::new(error_code(error), correlation.clone());
|
||||
match error {
|
||||
RuntimeError::ProtocolAdapter(ProtocolAdapterError::UnexpectedStatus {
|
||||
status, ..
|
||||
}) => {
|
||||
failure = failure.with_upstream_status(*status);
|
||||
}
|
||||
RuntimeError::ProtocolAdapter(adapter)
|
||||
if may_mutate && ambiguous_after_dispatch(adapter) =>
|
||||
{
|
||||
failure = failure.with_dispatch_uncertainty();
|
||||
}
|
||||
RuntimeError::ExecutionDeadlineElapsed {
|
||||
may_have_dispatched: true,
|
||||
}
|
||||
| RuntimeError::IdempotencyCompletionUnknown { .. } => {
|
||||
failure = failure.with_dispatch_uncertainty();
|
||||
}
|
||||
RuntimeError::ConfirmationRequired {
|
||||
confirmation_token,
|
||||
expires_in_ms,
|
||||
..
|
||||
} => {
|
||||
if let Ok(with_confirmation) = failure
|
||||
.clone()
|
||||
.try_with_confirmation(confirmation_token, *expires_in_ms)
|
||||
{
|
||||
failure = with_confirmation;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
failure
|
||||
}
|
||||
|
||||
fn ambiguous_after_dispatch(error: &ProtocolAdapterError) -> bool {
|
||||
match error {
|
||||
ProtocolAdapterError::Transport {
|
||||
dispatch: DispatchEvidence::MayHaveDispatched,
|
||||
}
|
||||
| ProtocolAdapterError::Timeout {
|
||||
dispatch: DispatchEvidence::MayHaveDispatched,
|
||||
}
|
||||
| ProtocolAdapterError::ResponseTooLarge {
|
||||
dispatch: DispatchEvidence::MayHaveDispatched,
|
||||
}
|
||||
| ProtocolAdapterError::InvalidResponse {
|
||||
dispatch: DispatchEvidence::MayHaveDispatched,
|
||||
} => true,
|
||||
ProtocolAdapterError::UnexpectedStatus {
|
||||
status,
|
||||
dispatch: DispatchEvidence::MayHaveDispatched,
|
||||
} => matches!(status, 408 | 429 | 500..=599),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn error_code(error: &RuntimeError) -> ExecutionErrorCode {
|
||||
match error {
|
||||
RuntimeError::InvalidExecutionRequest => ExecutionErrorCode::RuntimeInternal,
|
||||
RuntimeError::ExecutionDeadlineElapsed { .. } => ExecutionErrorCode::UpstreamTimeout,
|
||||
RuntimeError::Schema(_) | RuntimeError::InputSchema(_) => {
|
||||
ExecutionErrorCode::InputSchemaInvalid
|
||||
}
|
||||
RuntimeError::OutputSchema(_) => ExecutionErrorCode::OutputSchemaInvalid,
|
||||
RuntimeError::Mapping(_) | RuntimeError::InputMapping(_) => {
|
||||
ExecutionErrorCode::InputMappingInvalid
|
||||
}
|
||||
RuntimeError::OutputMapping(_) => ExecutionErrorCode::OutputMappingInvalid,
|
||||
RuntimeError::ProtocolAdapter(adapter) => protocol_error_code(adapter),
|
||||
RuntimeError::UnsupportedProtocol { .. } => ExecutionErrorCode::ProtocolUnsupported,
|
||||
RuntimeError::UnsupportedExecutionMode { .. } => {
|
||||
ExecutionErrorCode::ExecutionModeUnsupported
|
||||
}
|
||||
RuntimeError::ConcurrencyLimitExceeded { .. } => ExecutionErrorCode::ExecutionOverloaded,
|
||||
RuntimeError::InvalidPreparedRequest { .. } => ExecutionErrorCode::PreparedRequestInvalid,
|
||||
RuntimeError::ConfirmationRequired { .. } => ExecutionErrorCode::ConfirmationRequired,
|
||||
RuntimeError::InvalidConfirmationToken { .. } => ExecutionErrorCode::ConfirmationInvalid,
|
||||
RuntimeError::ConfirmationStoreUnavailable { .. }
|
||||
| RuntimeError::IdempotencyStoreUnavailable { .. } => {
|
||||
ExecutionErrorCode::SafetyStoreUnavailable
|
||||
}
|
||||
RuntimeError::IdempotencyInProgress { .. } => ExecutionErrorCode::IdempotencyInProgress,
|
||||
RuntimeError::IdempotencyConflict { .. } => ExecutionErrorCode::IdempotencyConflict,
|
||||
RuntimeError::IdempotencyOutcomeUnknown { .. } => {
|
||||
ExecutionErrorCode::IdempotencyOutcomeUnknown
|
||||
}
|
||||
RuntimeError::IdempotencyCompletionUnknown { .. } => {
|
||||
ExecutionErrorCode::IdempotencyOutcomeUnknown
|
||||
}
|
||||
RuntimeError::MissingAuthProfile { .. } => ExecutionErrorCode::AuthProfileNotFound,
|
||||
RuntimeError::InvalidAuthProfileConfig { .. } => ExecutionErrorCode::SecretInvalid,
|
||||
RuntimeError::AuthorizationStoreUnavailable => ExecutionErrorCode::SafetyStoreUnavailable,
|
||||
RuntimeError::MissingSecret { .. } | RuntimeError::MissingSecretVersion { .. } => {
|
||||
ExecutionErrorCode::SecretNotFound
|
||||
}
|
||||
RuntimeError::InvalidAuthSecretValue { .. } | RuntimeError::SecretCrypto { .. } => {
|
||||
ExecutionErrorCode::SecretInvalid
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn protocol_error_code(error: &ProtocolAdapterError) -> ExecutionErrorCode {
|
||||
match error {
|
||||
ProtocolAdapterError::UnsupportedMode { .. } => {
|
||||
ExecutionErrorCode::ExecutionModeUnsupported
|
||||
}
|
||||
ProtocolAdapterError::InvalidConfiguration => {
|
||||
ExecutionErrorCode::AdapterConfigurationInvalid
|
||||
}
|
||||
ProtocolAdapterError::InvalidPreparedRequest => ExecutionErrorCode::PreparedRequestInvalid,
|
||||
ProtocolAdapterError::RequestTooLarge => ExecutionErrorCode::UpstreamRequestTooLarge,
|
||||
ProtocolAdapterError::TargetRejected => ExecutionErrorCode::OutboundTargetRejected,
|
||||
ProtocolAdapterError::Transport { .. } => ExecutionErrorCode::UpstreamTransportError,
|
||||
ProtocolAdapterError::Timeout { .. } => ExecutionErrorCode::UpstreamTimeout,
|
||||
ProtocolAdapterError::ResponseTooLarge { .. } => {
|
||||
ExecutionErrorCode::UpstreamResponseTooLarge
|
||||
}
|
||||
ProtocolAdapterError::UnexpectedStatus { status, .. } => match status {
|
||||
401 | 403 => ExecutionErrorCode::UpstreamAuthError,
|
||||
404 => ExecutionErrorCode::UpstreamNotFound,
|
||||
408 | 429 => ExecutionErrorCode::UpstreamRateLimited,
|
||||
500..=599 => ExecutionErrorCode::UpstreamServerError,
|
||||
_ => ExecutionErrorCode::UpstreamStatusError,
|
||||
},
|
||||
ProtocolAdapterError::InvalidResponse { .. } => ExecutionErrorCode::UpstreamStatusError,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crank_core::{
|
||||
CorrelationContext, DispatchEvidence, ExecutionErrorCode, OutcomeCertainty,
|
||||
ProtocolAdapterError, Retryability,
|
||||
};
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn status_and_dispatch_evidence_drive_closed_semantics() {
|
||||
let correlation = CorrelationContext::generate();
|
||||
let rate_limit = normalize_runtime_error(
|
||||
&RuntimeError::ProtocolAdapter(ProtocolAdapterError::UnexpectedStatus {
|
||||
status: 429,
|
||||
dispatch: DispatchEvidence::MayHaveDispatched,
|
||||
}),
|
||||
&correlation,
|
||||
);
|
||||
assert_eq!(
|
||||
rate_limit.error_code(),
|
||||
ExecutionErrorCode::UpstreamRateLimited
|
||||
);
|
||||
assert_eq!(rate_limit.upstream_status(), Some(429));
|
||||
assert_eq!(rate_limit.retryability(), Retryability::AfterDelay);
|
||||
assert_eq!(rate_limit.outcome_certainty(), OutcomeCertainty::Certain);
|
||||
|
||||
let ambiguous = normalize_runtime_error(
|
||||
&RuntimeError::ProtocolAdapter(ProtocolAdapterError::Timeout {
|
||||
dispatch: DispatchEvidence::MayHaveDispatched,
|
||||
}),
|
||||
&correlation,
|
||||
);
|
||||
assert_eq!(ambiguous.error_code(), ExecutionErrorCode::UpstreamTimeout);
|
||||
assert_eq!(ambiguous.retryability(), Retryability::AfterDelay);
|
||||
assert_eq!(ambiguous.outcome_certainty(), OutcomeCertainty::Certain);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn raw_internal_details_never_enter_normalized_failure() {
|
||||
let canary = "story18-secret-url-canary";
|
||||
let failure = normalize_runtime_error(
|
||||
&RuntimeError::SecretCrypto {
|
||||
operation: "decrypt",
|
||||
details: canary.to_owned(),
|
||||
},
|
||||
&CorrelationContext::generate(),
|
||||
);
|
||||
assert_eq!(failure.error_code(), ExecutionErrorCode::SecretInvalid);
|
||||
assert!(!format!("{failure:?}").contains(canary));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn authorization_store_failures_use_typed_safety_store_outcome() {
|
||||
let failure = normalize_runtime_error(
|
||||
&RuntimeError::AuthorizationStoreUnavailable,
|
||||
&CorrelationContext::generate(),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
failure.error_code(),
|
||||
ExecutionErrorCode::SafetyStoreUnavailable
|
||||
);
|
||||
assert_eq!(failure.retryability(), Retryability::AfterDelay);
|
||||
assert_eq!(failure.outcome_certainty(), OutcomeCertainty::Certain);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user