Skip to content

Commit f7dbcfc

Browse files
better error handling in cli
1 parent 0a5e436 commit f7dbcfc

File tree

4 files changed

+65
-31
lines changed

4 files changed

+65
-31
lines changed

src/Microstack.CLI/Commands/SubCommands/Users.cs

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class Users : BaseCommand
5252
LongName = "download",
5353
ShortName = "d",
5454
ShowInHelpText = true,
55-
ValueName = "<CONFIG FILENAME>"
55+
ValueName = "CONFIG FILENAME"
5656
)]
5757
public string Download { get; set; }
5858

@@ -85,42 +85,25 @@ protected override async Task<int> OnExecute(CommandLineApplication app)
8585

8686
if (!string.IsNullOrWhiteSpace(UserId))
8787
{
88-
89-
using (var client = new HttpClient())
88+
var (response, error) = await _userSettingsProvider.GetUserSettings(UserId);
89+
if (error)
9090
{
91-
client.BaseAddress = new Uri(settings);
92-
try
93-
{
94-
var response = await (await client.GetAsync($"/api/users/{UserId}")).Content.ReadAsStringAsync();
95-
var formattedJson = JsonConvert.SerializeObject(JsonConvert.DeserializeObject(response), Formatting.Indented);
96-
OuputToConsole(formattedJson);
97-
return 0;
98-
}
99-
catch (Exception ex)
100-
{
101-
OutputError($"Error occurred while connecting to api {ex.Message}");
102-
return 1;
103-
}
91+
OutputError(response);
92+
return 1;
10493
}
94+
OuputToConsole(response);
95+
return 0;
10596
}
10697
if (All)
10798
{
108-
using (var client = new HttpClient())
99+
var (response, error) = await _userSettingsProvider.ListAllUsers();
100+
if (error)
109101
{
110-
client.BaseAddress = new Uri(settings);
111-
try
112-
{
113-
var response = await (await client.GetAsync($"/api/users")).Content.ReadAsStringAsync();
114-
var formattedJson = JsonConvert.SerializeObject(JsonConvert.DeserializeObject(response), Formatting.Indented);
115-
OuputToConsole(formattedJson);
116-
return 0;
117-
}
118-
catch (Exception ex)
119-
{
120-
OutputError($"Error occurred while connecting to api {ex.Message}");
121-
return 1;
122-
}
102+
OutputError(response);
103+
return 1;
123104
}
105+
OuputToConsole(response);
106+
return 0;
124107
}
125108
if (!string.IsNullOrWhiteSpace(Download))
126109
{

src/Microstack.CLI/Properties/launchSettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"commandName": "Project",
55
"executablePath": "C:\\github.com\\mstack\\microstack\\bin\\Debug\\netcoreapp3.1\\microstack.exe",
66
//"commandLineArgs": "run -v -c .mstkc2.json -p quotewf"
7-
"commandLineArgs": "users -u sharthak_ghosh -p"
7+
//"commandLineArgs": "users -u sharthak_ghosh -p"
8+
"commandLineArgs": " users -u coderookie1995"
89
}
910
}
1011
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Text;
4+
using System.Threading.Tasks;
45

56
namespace Microstack.Configuration.Abstractions
67
{
78
public interface IUserSettingsProvider
89
{
910
string GetSettings();
1011
void AddSetting(string url);
12+
Task<(string Response, bool Error)> GetUserSettings(string userId);
13+
Task<(string Response, bool Error)> ListAllUsers();
1114
}
1215
}

src/Microstack.Configuration/UserSettingsProvider.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
using Microstack.Configuration.Abstractions;
2+
using Newtonsoft.Json;
23
using System;
34
using System.Collections.Generic;
45
using System.IO;
6+
using System.Net.Http;
57
using System.Text;
8+
using System.Threading.Tasks;
69

710
namespace Microstack.Configuration
811
{
@@ -22,5 +25,49 @@ public string GetSettings()
2225

2326
return File.ReadAllText(dirPath);
2427
}
28+
29+
public async Task<(string Response, bool Error)> GetUserSettings(string userId)
30+
{
31+
using (var client = new HttpClient())
32+
{
33+
client.BaseAddress = new Uri(GetSettings());
34+
try
35+
{
36+
var response = await client.GetAsync($"/api/users/{userId}");
37+
if (response.IsSuccessStatusCode)
38+
{
39+
var formattedJson = JsonConvert.SerializeObject(JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()), Formatting.Indented);
40+
return (formattedJson, false);
41+
}
42+
return (await response.Content.ReadAsStringAsync(), true);
43+
}
44+
catch (Exception ex)
45+
{
46+
return ($"Error occurred while connecting to api {ex.Message}", true);
47+
}
48+
}
49+
}
50+
51+
public async Task<(string Response, bool Error)> ListAllUsers()
52+
{
53+
using (var client = new HttpClient())
54+
{
55+
client.BaseAddress = new Uri(GetSettings());
56+
try
57+
{
58+
var response = await client.GetAsync($"/api/users");
59+
if (response.IsSuccessStatusCode)
60+
{
61+
var formattedJson = JsonConvert.SerializeObject(JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()), Formatting.Indented);
62+
return (formattedJson, false);
63+
}
64+
return (await response.Content.ReadAsStringAsync(), true);
65+
}
66+
catch (Exception ex)
67+
{
68+
return ($"Error occurred while connecting to api {ex.Message}", true);
69+
}
70+
}
71+
}
2572
}
2673
}

0 commit comments

Comments
 (0)