Skip to content

Commit 57f1939

Browse files
fix: sometimes the IPFS server does not JSON encode an error
1 parent a97626f commit 57f1939

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

src/IpfsClient.cs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@ HttpClient Api()
252252
/// <returns>
253253
/// A string representation of the command's result.
254254
/// </returns>
255+
/// <exception cref="HttpRequestException">
256+
/// When the IPFS server indicates an error.
257+
/// </exception>
255258
public async Task<string> DoCommandAsync(string command, CancellationToken cancel, string arg = null, params string[] options)
256259
{
257260
var url = BuildCommand(command, arg, options);
@@ -294,6 +297,9 @@ public async Task<string> DoCommandAsync(string command, CancellationToken cance
294297
/// The command's response is converted to <typeparamref name="T"/> using
295298
/// <c>JsonConvert</c>.
296299
/// </remarks>
300+
/// <exception cref="HttpRequestException">
301+
/// When the IPFS server indicates an error.
302+
/// </exception>
297303
public async Task<T> DoCommandAsync<T>(string command, CancellationToken cancel, string arg = null, params string[] options)
298304
{
299305
var json = await DoCommandAsync(command, cancel, arg, options);
@@ -319,6 +325,9 @@ public async Task<T> DoCommandAsync<T>(string command, CancellationToken cancel,
319325
/// <returns>
320326
/// A string representation of the command's result.
321327
/// </returns>
328+
/// <exception cref="HttpRequestException">
329+
/// When the IPFS server indicates an error.
330+
/// </exception>
322331
public async Task<string> PostCommandAsync(string command, CancellationToken cancel, string arg = null, params string[] options)
323332
{
324333
var url = BuildCommand(command, arg, options);
@@ -363,6 +372,9 @@ public async Task<string> PostCommandAsync(string command, CancellationToken can
363372
/// The command's response is converted to <typeparamref name="T"/> using
364373
/// <c>JsonConvert</c>.
365374
/// </remarks>
375+
/// <exception cref="HttpRequestException">
376+
/// When the IPFS server indicates an error.
377+
/// </exception>
366378
public async Task<T> PostCommandAsync<T>(string command, CancellationToken cancel, string arg = null, params string[] options)
367379
{
368380
var json = await PostCommandAsync(command, cancel, arg, options);
@@ -388,6 +400,9 @@ public async Task<T> PostCommandAsync<T>(string command, CancellationToken cance
388400
/// <returns>
389401
/// A <see cref="Stream"/> containing the command's result.
390402
/// </returns>
403+
/// <exception cref="HttpRequestException">
404+
/// When the IPFS server indicates an error.
405+
/// </exception>
391406
public async Task<Stream> PostDownloadAsync(string command, CancellationToken cancel, string arg = null, params string[] options)
392407
{
393408
var url = BuildCommand(command, arg, options);
@@ -420,6 +435,9 @@ public async Task<Stream> PostDownloadAsync(string command, CancellationToken ca
420435
/// <returns>
421436
/// A <see cref="Stream"/> containing the command's result.
422437
/// </returns>
438+
/// <exception cref="HttpRequestException">
439+
/// When the IPFS server indicates an error.
440+
/// </exception>
423441
public async Task<Stream> DownloadAsync(string command, CancellationToken cancel, string arg = null, params string[] options)
424442
{
425443
var url = BuildCommand(command, arg, options);
@@ -450,6 +468,9 @@ public async Task<Stream> DownloadAsync(string command, CancellationToken cancel
450468
/// <returns>
451469
/// A byte array containing the command's result.
452470
/// </returns>
471+
/// <exception cref="HttpRequestException">
472+
/// When the IPFS server indicates an error.
473+
/// </exception>
453474
public async Task<byte[]> DownloadBytesAsync(string command, CancellationToken cancel, string arg = null, params string[] options)
454475
{
455476
var url = BuildCommand(command, arg, options);
@@ -470,6 +491,9 @@ public async Task<byte[]> DownloadBytesAsync(string command, CancellationToken c
470491
/// <param name="data"></param>
471492
/// <param name="options"></param>
472493
/// <returns></returns>
494+
/// <exception cref="HttpRequestException">
495+
/// When the IPFS server indicates an error.
496+
/// </exception>
473497
public async Task<String> UploadAsync(string command, CancellationToken cancel, Stream data, params string[] options)
474498
{
475499
var content = new MultipartFormDataContent();
@@ -513,13 +537,14 @@ public async Task<String> UploadAsync(string command, CancellationToken cancel,
513537
}
514538
}
515539

516-
517-
518540
/// <summary>
519-
///
541+
/// Throws an <see cref="HttpRequestException"/> if the response
542+
/// does not indicate success.
520543
/// </summary>
521544
/// <param name="response"></param>
522-
/// <returns></returns>
545+
/// <returns>
546+
/// <b>true</b>
547+
/// </returns>
523548
/// <remarks>
524549
/// The API server returns an JSON error in the form <c>{ "Message": "...", "Code": ... }</c>.
525550
/// </remarks>
@@ -538,7 +563,12 @@ async Task<bool> ThrowOnErrorAsync(HttpResponseMessage response)
538563
var body = await response.Content.ReadAsStringAsync();
539564
if (log.IsDebugEnabled)
540565
log.Debug("ERR " + body);
541-
var message = (string)JsonConvert.DeserializeObject<dynamic>(body).Message;
566+
string message = body;
567+
try
568+
{
569+
message = (string)JsonConvert.DeserializeObject<dynamic>(body).Message;
570+
}
571+
catch { }
542572
throw new HttpRequestException(message);
543573
}
544574

test/IpfsClientTest.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Ipfs.Api;
22
using Microsoft.VisualStudio.TestTools.UnitTesting;
33
using System;
4+
using System.Net.Http;
45
using System.Threading;
56

67
namespace Ipfs.Api
@@ -28,8 +29,15 @@ public void Do_Command_Throws_Exception_On_Invalid_Command()
2829
{
2930
IpfsClient target = TestFixture.Ipfs;
3031
object unknown;
31-
ExceptionAssert.Throws<Exception>(() => unknown = target.DoCommandAsync("foobar", default(CancellationToken)).Result);
32+
ExceptionAssert.Throws<HttpRequestException>(() => unknown = target.DoCommandAsync("foobar", default(CancellationToken)).Result);
3233
}
3334

35+
[TestMethod]
36+
public void Do_Command_Throws_Exception_On_Missing_Argument()
37+
{
38+
IpfsClient target = TestFixture.Ipfs;
39+
object unknown;
40+
ExceptionAssert.Throws<HttpRequestException>(() => unknown = target.DoCommandAsync("key/gen", default(CancellationToken)).Result);
41+
}
3442
}
3543
}

0 commit comments

Comments
 (0)