Browse Source

Display errors more humanely

master
Dylan Baker 5 years ago
parent
commit
029d0b9c1b
2 changed files with 27 additions and 15 deletions
  1. 10
    5
      src/cli.rs
  2. 17
    10
      src/client.rs

+ 10
- 5
src/cli.rs View File

@@ -2,7 +2,9 @@ use clap::ArgMatches;
2 2
 use std::fs;
3 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 9
 fn build_snippet_from_matches(matches: &ArgMatches) -> io::Result<OutgoingSnippet> {
8 10
     let title = matches.value_of("name").unwrap();
@@ -42,10 +44,13 @@ pub fn new_snippet(matches: &ArgMatches) -> io::Result<()> {
42 44
     let url = build_url_from_config(&config);
43 45
 
44 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 54
         Err(err) => println!("{}", err),
50 55
     }
51 56
 

+ 17
- 10
src/client.rs View File

@@ -9,14 +9,6 @@ pub struct OutgoingSnippet {
9 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 12
 #[derive(Debug)]
21 13
 pub struct ServerConfig {
22 14
     pub hostname: String,
@@ -24,6 +16,20 @@ pub struct ServerConfig {
24 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 33
 pub fn build_url_from_config(config: &ServerConfig) -> String {
28 34
     match config.port {
29 35
         80 => format!("{}://{}", config.protocol, config.hostname),
@@ -34,13 +40,14 @@ pub fn build_url_from_config(config: &ServerConfig) -> String {
34 40
 pub fn create_snippet(
35 41
     snippet: OutgoingSnippet,
36 42
     config: &ServerConfig,
37
-) -> Result<IncomingSnippet, Box<std::error::Error>> {
43
+) -> Result<ApiResult, Box<std::error::Error>> {
38 44
     let client = reqwest::Client::new();
39 45
     let url = build_url_from_config(config);
40
-    let resp: IncomingSnippet = client
46
+    let resp: ApiResult = client
41 47
         .post(&format!("{}/api/snippets", url))
42 48
         .json(&snippet)
43 49
         .send()?
44 50
         .json()?;
51
+
45 52
     Ok(resp)
46 53
 }

Loading…
Cancel
Save