Procházet zdrojové kódy

Display errors more humanely

master
Dylan Baker před 5 roky
rodič
revize
029d0b9c1b
2 změnil soubory, kde provedl 27 přidání a 15 odebrání
  1. 10
    5
      src/cli.rs
  2. 17
    10
      src/client.rs

+ 10
- 5
src/cli.rs Zobrazit soubor

2
 use std::fs;
2
 use std::fs;
3
 use std::io;
3
 use std::io;
4
 
4
 
5
-use crate::client::{build_url_from_config, create_snippet, OutgoingSnippet, ServerConfig};
5
+use crate::client::{
6
+    build_url_from_config, create_snippet, ApiResult, OutgoingSnippet, ServerConfig,
7
+};
6
 
8
 
7
 fn build_snippet_from_matches(matches: &ArgMatches) -> io::Result<OutgoingSnippet> {
9
 fn build_snippet_from_matches(matches: &ArgMatches) -> io::Result<OutgoingSnippet> {
8
     let title = matches.value_of("name").unwrap();
10
     let title = matches.value_of("name").unwrap();
42
     let url = build_url_from_config(&config);
44
     let url = build_url_from_config(&config);
43
 
45
 
44
     match create_snippet(snippet, &config) {
46
     match create_snippet(snippet, &config) {
45
-        Ok(resp) => println!(
46
-            "Success! Your new snippet is available at {}/snippets/{}",
47
-            url, resp.id
48
-        ),
47
+        Ok(resp) => match resp {
48
+            ApiResult::IncomingSnippet { id, .. } => println!(
49
+                "Success! Your new snippet is available at {}/snippets/{}",
50
+                url, id
51
+            ),
52
+            ApiResult::IncomingError { message } => println!("error: {}", message),
53
+        },
49
         Err(err) => println!("{}", err),
54
         Err(err) => println!("{}", err),
50
     }
55
     }
51
 
56
 

+ 17
- 10
src/client.rs Zobrazit soubor

9
     pub filetype: String,
9
     pub filetype: String,
10
 }
10
 }
11
 
11
 
12
-#[derive(Serialize, Deserialize, Debug)]
13
-pub struct IncomingSnippet {
14
-    pub id: i32,
15
-    pub title: String,
16
-    pub body: String,
17
-    pub created_at: NaiveDateTime,
18
-}
19
-
20
 #[derive(Debug)]
12
 #[derive(Debug)]
21
 pub struct ServerConfig {
13
 pub struct ServerConfig {
22
     pub hostname: String,
14
     pub hostname: String,
24
     pub port: i32,
16
     pub port: i32,
25
 }
17
 }
26
 
18
 
19
+#[derive(Serialize, Deserialize, Debug)]
20
+#[serde(untagged)]
21
+pub enum ApiResult {
22
+    IncomingSnippet {
23
+        id: i32,
24
+        title: String,
25
+        body: String,
26
+        created_at: NaiveDateTime,
27
+    },
28
+    IncomingError {
29
+        message: String,
30
+    },
31
+}
32
+
27
 pub fn build_url_from_config(config: &ServerConfig) -> String {
33
 pub fn build_url_from_config(config: &ServerConfig) -> String {
28
     match config.port {
34
     match config.port {
29
         80 => format!("{}://{}", config.protocol, config.hostname),
35
         80 => format!("{}://{}", config.protocol, config.hostname),
34
 pub fn create_snippet(
40
 pub fn create_snippet(
35
     snippet: OutgoingSnippet,
41
     snippet: OutgoingSnippet,
36
     config: &ServerConfig,
42
     config: &ServerConfig,
37
-) -> Result<IncomingSnippet, Box<std::error::Error>> {
43
+) -> Result<ApiResult, Box<std::error::Error>> {
38
     let client = reqwest::Client::new();
44
     let client = reqwest::Client::new();
39
     let url = build_url_from_config(config);
45
     let url = build_url_from_config(config);
40
-    let resp: IncomingSnippet = client
46
+    let resp: ApiResult = client
41
         .post(&format!("{}/api/snippets", url))
47
         .post(&format!("{}/api/snippets", url))
42
         .json(&snippet)
48
         .json(&snippet)
43
         .send()?
49
         .send()?
44
         .json()?;
50
         .json()?;
51
+
45
     Ok(resp)
52
     Ok(resp)
46
 }
53
 }

Načítá se…
Zrušit
Uložit