Skip to content

Commit 3ee3ac5

Browse files
refactor(IpfsException): No real need it, just bubble the exceptions
1 parent 872fe87 commit 3ee3ac5

File tree

7 files changed

+58
-230
lines changed

7 files changed

+58
-230
lines changed

src/CoreApi/ConfigApi.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public async Task<JObject> GetAsync()
5050
/// <returns>
5151
/// The value of the <paramref name="key"/> as <see cref="JToken"/>.
5252
/// </returns>
53-
/// <exception cref="IpfsException">
53+
/// <exception cref="Exception">
5454
/// When the <paramref name="key"/> does not exist.
5555
/// </exception>
5656
/// <remarks>

src/IpfsApi.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@
9393
<Compile Include="CoreApi\ConfigApi.cs" />
9494
<Compile Include="CoreApi\BlockApi.cs" />
9595
<Compile Include="ConnectedPeer.cs" />
96-
<Compile Include="IpfsException.cs" />
9796
<Compile Include="MerkleNode.cs" />
9897
<Compile Include="PinMode.cs" />
9998
<Compile Include="PinnedObject.cs" />

src/IpfsClient.cs

Lines changed: 53 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -222,27 +222,16 @@ HttpClient Api()
222222
/// </returns>
223223
public async Task<string> DoCommandAsync(string command, string arg = null, params string[] options)
224224
{
225-
try
225+
var url = BuildCommand(command, arg, options);
226+
if (log.IsDebugEnabled)
227+
log.Debug("GET " + url.ToString());
228+
using (var response = await Api().GetAsync(url))
226229
{
227-
var url = BuildCommand(command, arg, options);
230+
await ThrowOnErrorAsync(response);
231+
var body = await response.Content.ReadAsStringAsync();
228232
if (log.IsDebugEnabled)
229-
log.Debug("GET " + url.ToString());
230-
using (var response = await Api().GetAsync(url))
231-
{
232-
await ThrowOnErrorAsync(response);
233-
var body = await response.Content.ReadAsStringAsync();
234-
if (log.IsDebugEnabled)
235-
log.Debug("RSP " + body);
236-
return body;
237-
}
238-
}
239-
catch (IpfsException)
240-
{
241-
throw;
242-
}
243-
catch (Exception e)
244-
{
245-
throw new IpfsException(e);
233+
log.Debug("RSP " + body);
234+
return body;
246235
}
247236
}
248237

@@ -294,27 +283,16 @@ public async Task<T> DoCommandAsync<T>(string command, string arg = null, params
294283
/// </returns>
295284
public async Task<string> PostCommandAsync(string command, string arg = null, params string[] options)
296285
{
297-
try
286+
var url = BuildCommand(command, arg, options);
287+
if (log.IsDebugEnabled)
288+
log.Debug("POST " + url.ToString());
289+
using (var response = await Api().PostAsync(url, null))
298290
{
299-
var url = BuildCommand(command, arg, options);
291+
await ThrowOnErrorAsync(response);
292+
var body = await response.Content.ReadAsStringAsync();
300293
if (log.IsDebugEnabled)
301-
log.Debug("POST " + url.ToString());
302-
using (var response = await Api().PostAsync(url, null))
303-
{
304-
await ThrowOnErrorAsync(response);
305-
var body = await response.Content.ReadAsStringAsync();
306-
if (log.IsDebugEnabled)
307-
log.Debug("RSP " + body);
308-
return body;
309-
}
310-
}
311-
catch (IpfsException)
312-
{
313-
throw;
314-
}
315-
catch (Exception e)
316-
{
317-
throw new IpfsException(e);
294+
log.Debug("RSP " + body);
295+
return body;
318296
}
319297
}
320298

@@ -366,23 +344,12 @@ public async Task<T> PostCommandAsync<T>(string command, string arg = null, para
366344
/// </returns>
367345
public async Task<Stream> PostDownloadAsync(string command, string arg = null, params string[] options)
368346
{
369-
try
370-
{
371-
var url = BuildCommand(command, arg, options);
372-
if (log.IsDebugEnabled)
373-
log.Debug("POST " + url.ToString());
374-
var response = await Api().PostAsync(url, null);
375-
await ThrowOnErrorAsync(response);
376-
return await response.Content.ReadAsStreamAsync();
377-
}
378-
catch (IpfsException)
379-
{
380-
throw;
381-
}
382-
catch (Exception e)
383-
{
384-
throw new IpfsException(e);
385-
}
347+
var url = BuildCommand(command, arg, options);
348+
if (log.IsDebugEnabled)
349+
log.Debug("POST " + url.ToString());
350+
var response = await Api().PostAsync(url, null);
351+
await ThrowOnErrorAsync(response);
352+
return await response.Content.ReadAsStreamAsync();
386353
}
387354

388355
/// <summary>
@@ -404,23 +371,12 @@ public async Task<Stream> PostDownloadAsync(string command, string arg = null, p
404371
/// </returns>
405372
public async Task<Stream> DownloadAsync(string command, string arg = null, params string[] options)
406373
{
407-
try
408-
{
409-
var url = BuildCommand(command, arg, options);
410-
if (log.IsDebugEnabled)
411-
log.Debug("GET " + url.ToString());
412-
var response = await Api().GetAsync(url);
413-
await ThrowOnErrorAsync(response);
414-
return await response.Content.ReadAsStreamAsync();
415-
}
416-
catch (IpfsException)
417-
{
418-
throw;
419-
}
420-
catch (Exception e)
421-
{
422-
throw new IpfsException(e);
423-
}
374+
var url = BuildCommand(command, arg, options);
375+
if (log.IsDebugEnabled)
376+
log.Debug("GET " + url.ToString());
377+
var response = await Api().GetAsync(url);
378+
await ThrowOnErrorAsync(response);
379+
return await response.Content.ReadAsStreamAsync();
424380
}
425381

426382
/// <summary>
@@ -438,27 +394,16 @@ public async Task<Stream> DownloadAsync(string command, string arg = null, param
438394
/// The optional flags to the command.
439395
/// </param>
440396
/// <returns>
441-
/// A byte arra> containing the command's result.
397+
/// A byte array containing the command's result.
442398
/// </returns>
443399
public async Task<byte[]> DownloadBytesAsync(string command, string arg = null, params string[] options)
444400
{
445-
try
446-
{
447-
var url = BuildCommand(command, arg, options);
448-
if (log.IsDebugEnabled)
449-
log.Debug("GET " + url.ToString());
450-
var response = await Api().GetAsync(url);
451-
await ThrowOnErrorAsync(response);
452-
return await response.Content.ReadAsByteArrayAsync();
453-
}
454-
catch (IpfsException)
455-
{
456-
throw;
457-
}
458-
catch (Exception e)
459-
{
460-
throw new IpfsException(e);
461-
}
401+
var url = BuildCommand(command, arg, options);
402+
if (log.IsDebugEnabled)
403+
log.Debug("GET " + url.ToString());
404+
var response = await Api().GetAsync(url);
405+
await ThrowOnErrorAsync(response);
406+
return await response.Content.ReadAsByteArrayAsync();
462407
}
463408

464409
/// <summary>
@@ -475,27 +420,16 @@ public async Task<String> UploadAsync(string command, Stream data, params string
475420
streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
476421
content.Add(streamContent, "file");
477422

478-
try
423+
var url = BuildCommand(command, null, options);
424+
if (log.IsDebugEnabled)
425+
log.Debug("POST " + url.ToString());
426+
using (var response = await Api().PostAsync(url, content))
479427
{
480-
var url = BuildCommand(command, null, options);
428+
await ThrowOnErrorAsync(response);
429+
var json = await response.Content.ReadAsStringAsync();
481430
if (log.IsDebugEnabled)
482-
log.Debug("POST " + url.ToString());
483-
using (var response = await Api().PostAsync(url, content))
484-
{
485-
await ThrowOnErrorAsync(response);
486-
var json = await response.Content.ReadAsStringAsync();
487-
if (log.IsDebugEnabled)
488-
log.Debug("RSP " + json);
489-
return json;
490-
}
491-
}
492-
catch (IpfsException)
493-
{
494-
throw;
495-
}
496-
catch (Exception e)
497-
{
498-
throw new IpfsException(e);
431+
log.Debug("RSP " + json);
432+
return json;
499433
}
500434
}
501435

@@ -509,27 +443,16 @@ public async Task<String> UploadAsync(string command, byte[] data, params string
509443
streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
510444
content.Add(streamContent, "file");
511445

512-
try
446+
var url = BuildCommand(command, null, options);
447+
if (log.IsDebugEnabled)
448+
log.Debug("POST " + url.ToString());
449+
using (var response = await Api().PostAsync(url, content))
513450
{
514-
var url = BuildCommand(command, null, options);
451+
await ThrowOnErrorAsync(response);
452+
var json = await response.Content.ReadAsStringAsync();
515453
if (log.IsDebugEnabled)
516-
log.Debug("POST " + url.ToString());
517-
using (var response = await Api().PostAsync(url, content))
518-
{
519-
await ThrowOnErrorAsync(response);
520-
var json = await response.Content.ReadAsStringAsync();
521-
if (log.IsDebugEnabled)
522-
log.Debug("RSP " + json);
523-
return json;
524-
}
525-
}
526-
catch (IpfsException)
527-
{
528-
throw;
529-
}
530-
catch (Exception e)
531-
{
532-
throw new IpfsException(e);
454+
log.Debug("RSP " + json);
455+
return json;
533456
}
534457
}
535458

@@ -618,13 +541,13 @@ async Task<bool> ThrowOnErrorAsync(HttpResponseMessage response)
618541
if (response.IsSuccessStatusCode)
619542
return true;
620543
if (response.StatusCode == HttpStatusCode.NotFound)
621-
throw new IpfsException("Invalid command");
544+
throw new HttpRequestException("Invalid IPFS command");
622545

623546
var body = await response.Content.ReadAsStringAsync();
624547
if (log.IsDebugEnabled)
625548
log.Debug("ERR " + body);
626549
var message = (string)JsonConvert.DeserializeObject<dynamic>(body).Message;
627-
throw new IpfsException(message);
550+
throw new HttpRequestException(message);
628551
}
629552

630553
}

src/IpfsException.cs

Lines changed: 0 additions & 93 deletions
This file was deleted.

test/Commands/ConfigTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void Keys_are_Case_Sensitive()
4545
var api = ipfs.Config.GetAsync("Addresses.API").Result;
4646
Assert.AreEqual(apiAddress, api);
4747

48-
ExceptionAssert.Throws<IpfsException>(() => { var x = ipfs.Config.GetAsync("Addresses.api").Result; });
48+
ExceptionAssert.Throws<Exception>(() => { var x = ipfs.Config.GetAsync("Addresses.api").Result; });
4949
}
5050

5151
[TestMethod]

test/IpfsClientTest.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,11 @@ public void Can_Create()
3535
}
3636

3737
[TestMethod]
38-
public void Do_Command_Throws_IpfsException_On_Invalid_Command()
38+
public void Do_Command_Throws_Exception_On_Invalid_Command()
3939
{
4040
IpfsClient target = new IpfsClient();
4141
object unknown;
42-
ExceptionAssert.Throws<IpfsException>(() => unknown = target.DoCommandAsync("foobar").Result, "Invalid command");
43-
42+
ExceptionAssert.Throws<Exception>(() => unknown = target.DoCommandAsync("foobar").Result, "Invalid IPFS command");
4443
}
4544

4645
}

test/TrustedPeersTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void Trusted_Peers_Add_Missing_Peer_ID()
3636
{
3737
var missingPeerId = new MultiAddress("/ip4/25.196.147.100/tcp/4001");
3838
var ipfs = new IpfsClient();
39-
ExceptionAssert.Throws<IpfsException>(() => ipfs.TrustedPeers.Add(missingPeerId), "invalid ipfs address");
39+
ExceptionAssert.Throws<Exception>(() => ipfs.TrustedPeers.Add(missingPeerId), "invalid ipfs address");
4040
}
4141

4242
[TestMethod]

0 commit comments

Comments
 (0)