Skip to main content

stackless_laravel_cloud/
error.rs

1//! Laravel Cloud substrate errors (codes in this crate's `laravel_cloud.*` registry).
2
3use stackless_core::fault::{ErrorContext, Fault};
4
5use crate::codes;
6
7#[derive(Debug, thiserror::Error)]
8pub enum LaravelCloudError {
9    #[error("[{location}] is invalid: {detail}")]
10    ConfigInvalid { location: String, detail: String },
11
12    #[error("creating paid Laravel Cloud resources requires explicit consent")]
13    PaymentNotConfirmed { resource: String },
14
15    #[error("provisioning {resource:?} on Laravel Cloud did not complete: {detail}")]
16    ProvisionFailed { resource: String, detail: String },
17
18    #[error("prepare for {service:?} failed: {message}")]
19    PrepareFailed {
20        service: String,
21        command: Option<String>,
22        message: String,
23        log_tail: Option<String>,
24    },
25}
26
27impl Fault for LaravelCloudError {
28    fn code(&self) -> &'static str {
29        match self {
30            Self::ConfigInvalid { .. } => codes::LARAVEL_CLOUD_CONFIG_INVALID,
31            Self::PaymentNotConfirmed { .. } => codes::LARAVEL_CLOUD_PAYMENT_NOT_CONFIRMED,
32            Self::ProvisionFailed { .. } => codes::LARAVEL_CLOUD_PROVISION_FAILED,
33            Self::PrepareFailed { .. } => codes::LARAVEL_CLOUD_PREPARE_FAILED,
34        }
35    }
36
37    fn remediation(&self) -> String {
38        match self {
39            Self::ConfigInvalid { location, .. } => {
40                format!(
41                    "fix the [{location}] block; see ARCHITECTURE.md ยง1 for the laravel-cloud schema"
42                )
43            }
44            Self::PaymentNotConfirmed { .. } => {
45                "re-run with --confirm-paid to consent to Laravel Cloud charges (bounded by the \
46                 project's hard spend cap)"
47                    .into()
48            }
49            Self::ProvisionFailed { .. } => {
50                "wait a moment for Laravel Cloud to finish provisioning and re-run `up` to resume"
51                    .into()
52            }
53            Self::PrepareFailed { service, .. } => format!(
54                "inspect context.log_tail; run the {service:?} prepare command by hand; re-run \
55                 `stackless up <name>`"
56            ),
57        }
58    }
59
60    fn context(&self) -> ErrorContext {
61        match self {
62            Self::PrepareFailed {
63                service,
64                command,
65                log_tail,
66                ..
67            } => ErrorContext {
68                service: Some(service.clone()),
69                command: command.clone(),
70                log_hint: Some(format!("stackless logs <name> {service}")),
71                log_tail: log_tail.clone(),
72                ..ErrorContext::default()
73            },
74            _ => ErrorContext::default(),
75        }
76    }
77}