Skip to content

Commit 85ab306

Browse files
committed
feat: Correctly parse error message
1 parent d13fc23 commit 85ab306

File tree

6 files changed

+122
-5
lines changed

6 files changed

+122
-5
lines changed

Connection/RESTConnector.cs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626
using System.Text;
2727
using UnityEngine;
2828
using UnityEngine.Networking;
29-
29+
using Newtonsoft.Json.Linq;
30+
using Newtonsoft.Json;
31+
using MiniJSON;
32+
3033
#if !NETFX_CORE
3134
using System.Net;
3235
using System.Net.Security;
@@ -89,8 +92,8 @@ public class Response
8992
/// </summary>
9093
public Dictionary<string, string> Headers { get; set; }
9194
#endregion
92-
};
93-
95+
};
96+
9497
/// <summary>
9598
/// Multi-part form data class.
9699
/// </summary>
@@ -485,7 +488,7 @@ private IEnumerator ProcessRequestQueue()
485488
method = req.HttpMethod
486489
};
487490

488-
if(req.HttpMethod == UnityWebRequest.kHttpVerbPOST)
491+
if (req.HttpMethod == UnityWebRequest.kHttpVerbPOST)
489492
{
490493
unityWebRequest.SetRequestHeader("Content-Type", "application/json");
491494
}
@@ -568,11 +571,13 @@ private IEnumerator ProcessRequestQueue()
568571
break;
569572
}
570573

574+
string errorMessage = GetErrorMessage(unityWebRequest.downloadHandler.text);
575+
571576
error = new IBMError()
572577
{
573578
Url = url,
574579
StatusCode = unityWebRequest.responseCode,
575-
ErrorMessage = unityWebRequest.error,
580+
ErrorMessage = errorMessage,
576581
Response = unityWebRequest.downloadHandler.text,
577582
ResponseHeaders = unityWebRequest.GetResponseHeaders()
578583
};
@@ -628,6 +633,29 @@ private IEnumerator ProcessRequestQueue()
628633
// reduce the connection count before we exit.
629634
_activeConnections -= 1;
630635
yield break;
636+
}
637+
#endregion
638+
639+
#region Get Error Message
640+
public string GetErrorMessage(string error)
641+
{
642+
dynamic deserializedObject = Json.Deserialize(error);
643+
if ((deserializedObject as Dictionary<string, object>).ContainsKey("errors"))
644+
{
645+
return deserializedObject["errors"][0]["message"];
646+
}
647+
648+
if ((deserializedObject as Dictionary<string, object>).ContainsKey("error"))
649+
{
650+
return deserializedObject["error"];
651+
}
652+
653+
if ((deserializedObject as Dictionary<string, object>).ContainsKey("message"))
654+
{
655+
return deserializedObject["message"];
656+
}
657+
658+
return "Unknown error";
631659
}
632660
#endregion
633661
}

Tests.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tests/CoreTests.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Copyright 2019 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
using System.Collections;
19+
using System.Collections.Generic;
20+
using NUnit.Framework;
21+
using IBM.Cloud.SDK.Connection;
22+
23+
namespace IBM.Cloud.SDK.Tests
24+
{
25+
public class CoreTests
26+
{
27+
[Test]
28+
public void GetErrorFromArray()
29+
{
30+
string json = "{\"errors\":[{\"code\":\"missing_field\",\"message\":\"The request path is not valid. Make sure that the endpoint is correct.\",\"more_info\":\"https://cloud.ibm.com/apidocs/visual-recognition-v4\",\"target\":{\"type\":\"field\",\"name\":\"URL path\"}}],\"trace\":\"4e1b7b85-4dba-4219-b46b-6cdd2e2c06fd\"}";
31+
RESTConnector restConnector = new RESTConnector();
32+
string errorMessage = restConnector.GetErrorMessage(json);
33+
Assert.IsTrue(errorMessage == "The request path is not valid. Make sure that the endpoint is correct.");
34+
}
35+
36+
[Test]
37+
public void GetErrorFromError()
38+
{
39+
string json = "{\"code\":\"400\",\"error\":\"Error: Too many images in collection\"}";
40+
RESTConnector restConnector = new RESTConnector();
41+
string errorMessage = restConnector.GetErrorMessage(json);
42+
Assert.IsTrue(errorMessage == "Error: Too many images in collection");
43+
}
44+
45+
[Test]
46+
public void GetErrorFromMessage()
47+
{
48+
string json = "{\"code\":\"string\",\"message\":\"string\"}";
49+
RESTConnector restConnector = new RESTConnector();
50+
string errorMessage = restConnector.GetErrorMessage(json);
51+
Assert.IsTrue(errorMessage == "string");
52+
}
53+
}
54+
}

Tests/CoreTests.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tests/IBM.Cloud.SDK.Tests.asmdef

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "IBM.Cloud.SDK.Tests",
3+
"references": [
4+
"IBM.Cloud.SDK"
5+
],
6+
"optionalUnityReferences": [
7+
"TestAssemblies"
8+
]
9+
}

Tests/IBM.Cloud.SDK.Tests.asmdef.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)