Skip to content

Commit

Permalink
feat(ErrorMessage): Standardized errorMessage parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
mediumTaj committed Mar 29, 2019
1 parent a338007 commit 89e4979
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 25 deletions.
47 changes: 22 additions & 25 deletions src/IBM.Cloud.SDK.Core/Http/Exceptions/ErrorConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using IBM.Cloud.SDK.Core.Util;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

Expand All @@ -36,31 +34,30 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
JObject jo = JObject.Load(reader);
Error err = new Error();

err.Message = Utility.GetErrorMessage(jo.ToString());

foreach (var property in jo.Properties())
{
switch (property.Name)
{
case "error":
case "error_message":
case "message":
err.Message = property.Value.ToString();
break;
case "error_code":
case "code":
err.Code = (int)property.Value;
break;
case "help":
err.Help = property.Value.ToString();
break;
case "description":
case "code_description":
err.CodeDescription = property.Value.ToString();
break;
case "session_closed":
err.SessionClosed = (bool)property.Value;
break;
default:
break;
{
case "error_code":
case "code":
int code;
int.TryParse(property.Value.ToString(), out code);
err.Code = code;
break;
case "help":
err.Help = property.Value.ToString();
break;
case "description":
case "code_description":
err.CodeDescription = property.Value.ToString();
break;
case "session_closed":
err.SessionClosed = (bool)property.Value;
break;
default:
break;
}
}

Expand Down
29 changes: 29 additions & 0 deletions src/IBM.Cloud.SDK.Core/Util/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*
*/

using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -172,5 +173,33 @@ public static List<string> GetCredentialsPaths()

return filePathsToLoad;
}

public static string GetErrorMessage(string input)
{
JObject o = JObject.Parse(input);

if(o["errors"] != null)
{
JToken errorsArray = o["errors"];
return errorsArray[0]["message"].ToString();
}

if(o["error"] != null)
{
return o["error"].ToString();
}

if (o["message"] != null)
{
return o["message"].ToString();
}

if (o["errorMessage"] != null)
{
return o["errorMessage"].ToString();
}

return "unknown error";
}
}
}
91 changes: 91 additions & 0 deletions test/IBM.Cloud.SDK.Core.Tests/ErrorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* Copyright 2019 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

using IBM.Cloud.SDK.Core.Http.Exceptions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;

namespace IBM.Cloud.SDK.Core.Tests
{
[TestClass]
public class ErrorTests
{
[TestMethod]
public void GetErrorMessageFromErrors()
{
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\"}";
Error error = JsonConvert.DeserializeObject<Error>(json);
Assert.IsTrue(error.Message == "The request path is not valid. Make sure that the endpoint is correct.");
}

[TestMethod]
public void GetErrorMessageFromError()
{
string json = "{\"code\":\"400\",\"error\":\"Error: Too many images in collection\"}";
Error error = JsonConvert.DeserializeObject<Error>(json);
Assert.IsTrue(error.Message == "Error: Too many images in collection");
}

[TestMethod]
public void GetErrorMessageFromMessage()
{
string json = "{\"code\":\"string\",\"message\":\"string\"}";
Error error = JsonConvert.DeserializeObject<Error>(json);
Assert.IsTrue(error.Message == "string");
}

[TestMethod]
public void GetErrorMessageFromErrorMessage()
{
string json = "{\"errorCode\": 404, \"errorMessage\": \"Provided API key could not be found\"}";
Error error = JsonConvert.DeserializeObject<Error>(json);
Assert.IsTrue(error.Message == "Provided API key could not be found");
}

[TestMethod]
public void GetErrorMessageFromErrorsWithErrorAndMessageAndErrorMessage()
{
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\"}}],\"error\": \"Error: Too many images in collection\", \"message\": \"string\", \"trace\": \"4e1b7b85-4dba-4219-b46b-6cdd2e2c06fd\", \"errorMessage\": \"Provided API key could not be found\"}";
Error error = JsonConvert.DeserializeObject<Error>(json);
Assert.IsTrue(error.Message == "The request path is not valid. Make sure that the endpoint is correct.");
}

[TestMethod]
public void GetErrorMessageWithErrorAndMessageAndErrorMessage()
{
string json = "{\"error\": \"Error: Too many images in collection\", \"message\": \"string\", \"trace\": \"4e1b7b85-4dba-4219-b46b-6cdd2e2c06fd\", \"errorMessage\": \"Provided API key could not be found\"}";
Error error = JsonConvert.DeserializeObject<Error>(json);
Assert.IsTrue(error.Message == "Error: Too many images in collection");
}

[TestMethod]
public void GetErrorMessageWithMessageAndErrorMessage()
{
string json = "{\"message\": \"string\", \"trace\": \"4e1b7b85-4dba-4219-b46b-6cdd2e2c06fd\", \"errorMessage\": \"Provided API key could not be found\"}";
Error error = JsonConvert.DeserializeObject<Error>(json);
Assert.IsTrue(error.Message == "string");
}

[TestMethod]
public void GetGenericErrorMessage()
{
string json = "{\"msg\": \":(\"}";
Error error = JsonConvert.DeserializeObject<Error>(json);
Assert.IsTrue(error.Message == "unknown error");
}
}
}
64 changes: 64 additions & 0 deletions test/IBM.Cloud.SDK.Core.Tests/UtilTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,69 @@ public void SimpleGetWithCredentialsSuccess()
string response = Utility.SimpleGet("https://jsonplaceholder.typicode.com/users", "bogus-username", "bogus-password").Result;
Assert.IsNotNull(response);
}

[TestMethod]
public void GetErrorMessageFromErrors()
{
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\"}";
string errorMessage = Utility.GetErrorMessage(json);
Assert.IsTrue(errorMessage == "The request path is not valid. Make sure that the endpoint is correct.");
}

[TestMethod]
public void GetErrorMessageFromError()
{
string json = "{\"code\":\"400\",\"error\":\"Error: Too many images in collection\"}";
string errorMessage = Utility.GetErrorMessage(json);
Assert.IsTrue(errorMessage == "Error: Too many images in collection");
}

[TestMethod]
public void GetErrorMessageFromMessage()
{
string json = "{\"code\":\"string\",\"message\":\"string\"}";
string errorMessage = Utility.GetErrorMessage(json);
Assert.IsTrue(errorMessage == "string");
}

[TestMethod]
public void GetErrorMessageFromErrorMessage()
{
string json = "{\"errorCode\": \"string\", \"errorMessage\": \"Provided API key could not be found\"}";
string errorMessage = Utility.GetErrorMessage(json);
Assert.IsTrue(errorMessage == "Provided API key could not be found");
}

[TestMethod]
public void GetErrorMessageFromErrorsWithErrorAndMessageAndErrorMessage()
{
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\"}}],\"error\": \"Error: Too many images in collection\", \"message\": \"string\", \"trace\": \"4e1b7b85-4dba-4219-b46b-6cdd2e2c06fd\", \"errorMessage\": \"Provided API key could not be found\"}";
string errorMessage = Utility.GetErrorMessage(json);
Assert.IsTrue(errorMessage == "The request path is not valid. Make sure that the endpoint is correct.");
}

[TestMethod]
public void GetErrorMessageWithErrorAndMessageAndErrorMessage()
{
string json = "{\"error\": \"Error: Too many images in collection\", \"message\": \"string\", \"trace\": \"4e1b7b85-4dba-4219-b46b-6cdd2e2c06fd\", \"errorMessage\": \"Provided API key could not be found\"}";
string errorMessage = Utility.GetErrorMessage(json);
Assert.IsTrue(errorMessage == "Error: Too many images in collection");
}

[TestMethod]
public void GetErrorMessageWithMessageAndErrorMessage()
{
string json = "{\"message\": \"string\", \"trace\": \"4e1b7b85-4dba-4219-b46b-6cdd2e2c06fd\", \"errorMessage\": \"Provided API key could not be found\"}";
string errorMessage = Utility.GetErrorMessage(json);
Assert.IsTrue(errorMessage == "string");
}

[TestMethod]
public void GetGenericErrorMessage()
{
string json = "{\"msg\": \":(\"}";
string errorMessage = Utility.GetErrorMessage(json);
Assert.IsTrue(errorMessage == "unknown error");
}
}
}

0 comments on commit 89e4979

Please sign in to comment.