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("no Laravel Cloud API token found")]
13    ApiKeyMissing { key_file: String },
14
15    #[error("Laravel Cloud API {method} {path} failed: {detail}")]
16    ApiFailed {
17        method: String,
18        path: String,
19        detail: String,
20    },
21
22    #[error("creating paid Laravel Cloud resources requires explicit consent")]
23    PaymentNotConfirmed { resource: String },
24
25    #[error("provisioning {resource:?} on Laravel Cloud did not complete: {detail}")]
26    ProvisionFailed { resource: String, detail: String },
27
28    #[error("deploy of {service:?} ended {state}")]
29    DeployFailed { service: String, state: String },
30
31    #[error(
32        "deploy of {service:?} did not reach deployment.succeeded within {budget_secs}s (last state: {last_state})"
33    )]
34    DeployTimeout {
35        service: String,
36        budget_secs: u64,
37        last_state: String,
38    },
39
40    #[error("{service:?} failed its health contract ({detail}) within {budget_secs}s at {url}")]
41    HealthFailed {
42        service: String,
43        url: String,
44        detail: String,
45        budget_secs: u64,
46    },
47
48    #[error("prepare for {service:?} failed: {message}")]
49    PrepareFailed {
50        service: String,
51        command: Option<String>,
52        message: String,
53        log_tail: Option<String>,
54    },
55}
56
57impl Fault for LaravelCloudError {
58    fn code(&self) -> &'static str {
59        match self {
60            Self::ConfigInvalid { .. } => codes::LARAVEL_CLOUD_CONFIG_INVALID,
61            Self::ApiKeyMissing { .. } => codes::LARAVEL_CLOUD_API_KEY_MISSING,
62            Self::ApiFailed { .. } => codes::LARAVEL_CLOUD_API_FAILED,
63            Self::PaymentNotConfirmed { .. } => codes::LARAVEL_CLOUD_PAYMENT_NOT_CONFIRMED,
64            Self::ProvisionFailed { .. } => codes::LARAVEL_CLOUD_PROVISION_FAILED,
65            Self::DeployFailed { .. } => codes::LARAVEL_CLOUD_DEPLOY_FAILED,
66            Self::DeployTimeout { .. } => codes::LARAVEL_CLOUD_DEPLOY_TIMEOUT,
67            Self::HealthFailed { .. } => codes::LARAVEL_CLOUD_HEALTH_FAILED,
68            Self::PrepareFailed { .. } => codes::LARAVEL_CLOUD_PREPARE_FAILED,
69        }
70    }
71
72    fn remediation(&self) -> String {
73        match self {
74            Self::ConfigInvalid { location, .. } => {
75                format!(
76                    "fix the [{location}] block; see ARCHITECTURE.md ยง1 for the laravel-cloud schema"
77                )
78            }
79            Self::ApiKeyMissing { key_file } => format!(
80                "create a Laravel Cloud API token (cloud.laravel.com -> API tokens), then provide \
81                 it one of three ways: export LARAVEL_CLOUD_API_TOKEN, add \
82                 `LARAVEL_CLOUD_API_TOKEN=...` to .stackless.env, or store it scoped to this \
83                 tooling only:\n  ( umask 077 && pbpaste > {key_file} )"
84            ),
85            Self::ApiFailed { .. } => {
86                "check that cloud.laravel.com is reachable and the API token is valid, then \
87                 re-run `up`"
88                    .into()
89            }
90            Self::PaymentNotConfirmed { .. } => {
91                "re-run with --confirm-paid to consent to Laravel Cloud charges (bounded by the \
92                 project's hard spend cap)"
93                    .into()
94            }
95            Self::ProvisionFailed { .. } => {
96                "wait a moment for Laravel Cloud to finish provisioning and re-run `up` to resume"
97                    .into()
98            }
99            Self::DeployFailed { service, .. } => format!(
100                "`stackless logs <name> {service}` shows the build/deploy output; fix and re-run `up`"
101            ),
102            Self::DeployTimeout { service, .. } => format!(
103                "the {service:?} deploy is still running on Laravel Cloud; re-run `up` to resume \
104                 waiting, or check `stackless logs <name> {service}`"
105            ),
106            Self::HealthFailed { service, .. } => format!(
107                "`stackless logs <name> {service}` shows the service output; fix and re-run `up`"
108            ),
109            Self::PrepareFailed { service, .. } => format!(
110                "inspect context.log_tail; run the {service:?} prepare command by hand; re-run \
111                 `stackless up <name>`"
112            ),
113        }
114    }
115
116    fn context(&self) -> ErrorContext {
117        match self {
118            Self::PrepareFailed {
119                service,
120                command,
121                log_tail,
122                ..
123            } => ErrorContext {
124                service: Some(service.clone()),
125                command: command.clone(),
126                log_hint: Some(format!("stackless logs <name> {service}")),
127                log_tail: log_tail.clone(),
128                ..ErrorContext::default()
129            },
130            _ => ErrorContext::default(),
131        }
132    }
133}