1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
pub mod form;
mod krate;
pub mod package;

use indicatif::{ProgressBar, ProgressStyle};
pub use package::Package;

use anyhow::Result;
use reqwest::blocking::Client;

use crate::settings::toml::Target;
use crate::sites::AssetManifest;

pub fn script(
    client: &Client,
    target: &Target,
    asset_manifest: Option<AssetManifest>,
) -> Result<()> {
    let worker_addr = format!(
        "https://api.cloudflare.com/client/v4/accounts/{}/workers/scripts/{}",
        target.account_id.load()?,
        target.name,
    );

    let script_upload_form = form::build(target, asset_manifest, None)?;

    let style = ProgressStyle::default_spinner().template("{spinner}   {msg}");
    let spinner = ProgressBar::new_spinner().with_style(style);
    spinner.set_message("Uploading script...");
    spinner.enable_steady_tick(20);

    let res = client
        .put(&worker_addr)
        .multipart(script_upload_form)
        .send()?;

    spinner.finish_and_clear();

    if !res.status().is_success() {
        anyhow::bail!(error_msg(res.text()?))
    }

    Ok(())
}

fn error_msg(text: String) -> String {
    if text.contains("\"code\": 10034,") {
        "You need to verify your account's email address before you can publish. You can do this by checking your email or logging in to https://dash.cloudflare.com.".into()
    } else if text.contains("\"code\": 10000,") {
        "Your user configuration is invalid, please run wrangler login or wrangler config and enter a new set of credentials.".into()
    } else if text.contains("\"code\": 10075,") {
        "Setting a Usage Model requires a Paid plan with Unbound enabled. You can do this in the dash by logging in to https://dash.cloudflare.com/?account=workers/plans".into()
    } else {
        crate::format_api_errors(text)
    }
}

#[test]
fn fails_with_good_error_msg_on_verify_email_err() {
    let text = r#"{
  "result": null,
  "success": false,
  "errors": [
    {
      "code": 10034,
      "message": "workers.api.error.email_verification_required"
    }
  ],
  "messages": []
}"#
    .to_string();
    let result = error_msg(text);
    assert!(result.contains("https://dash.cloudflare.com"));
}