Skip to content

Commit

Permalink
Refactoring of SDK/API Exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
antonchi committed Jul 22, 2018
1 parent 65f5c1a commit a9e19e1
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 19 deletions.
6 changes: 3 additions & 3 deletions IntentoSDK/IntentoAiTextTranslate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ async public Task<dynamic> FulfillAsync(object text, string to, string from = nu
else if (failover_list is JArray)
failoverJson = (JArray)failover_list;
else
throw new Exception("Invalid failover_list parameter: need to json-list-string or Newtonsoft JArray");
throw new IntentoInvalidParameterException("failover_list", "need to json-list-string or Newtonsoft JArray");
service.failover_list = failover_list;
}
if (!string.IsNullOrEmpty(bidding))
service.bidding = bidding;
service.bidding = bidding;
}

json.service = service;
Expand Down Expand Up @@ -163,7 +163,7 @@ private dynamic GetJson(object data, string name)
return (JObject.FromObject((Dictionary<string, string>)data));
else if (data is IEnumerable<string>)
return (JArray.FromObject((IEnumerable<string>)data));
throw new Exception(string.Format("Invalid {0} parameter: need to be null or string or json-string or Newtonsoft JObject/JArray", name));
throw new IntentoInvalidParameterException(name, "need to be null or string or json-string or Newtonsoft JObject/JArray");
}

/// <summary>
Expand Down
57 changes: 47 additions & 10 deletions IntentoSDK/IntentoException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ namespace IntentoSDK
{
public class IntentoException : Exception
{
HttpResponseMessage response;
dynamic jsonResult;
string messaage;

protected internal IntentoException(HttpResponseMessage response, object jsonResult)
protected internal IntentoException(string message)
{
this.response = response;
this.jsonResult = jsonResult;
}

// Make is used because in the future we may have more specific Exceptions with additional fields like invalid lang pair
/// <summary>
/// Makes appropriate Exception depending on response
/// </summary>
/// <param name="response"></param>
/// <param name="jsonResult"></param>
/// <returns></returns>
public static IntentoException Make(HttpResponseMessage response, dynamic jsonResult)
{
switch(response.StatusCode)
Expand All @@ -28,7 +30,37 @@ public static IntentoException Make(HttpResponseMessage response, dynamic jsonRe
return new IntentoInvalidApiKeyException(response, jsonResult);
}

return new IntentoException(response, jsonResult);
return new IntentoApiException(response, jsonResult);
}

}

public class IntentoSdkException : IntentoException
{
protected internal IntentoSdkException(string message)
: base(message)
{
}
}

public class IntentoInvalidApiKeyException : IntentoApiException
{
protected internal IntentoInvalidApiKeyException(HttpResponseMessage response, object jsonResult)
: base(response, jsonResult)
{
}
}

public class IntentoApiException : IntentoException
{
HttpResponseMessage response;
dynamic jsonResult;

protected internal IntentoApiException(HttpResponseMessage response, object jsonResult)
: base("Error in Intento API call")
{
this.response = response;
this.jsonResult = jsonResult;
}

public HttpStatusCode StatusCode
Expand All @@ -43,12 +75,17 @@ public string ReasonPhrase
public dynamic Content
{ get { return jsonResult; } }

public dynamic JsonResult
{ get { return jsonResult; } }

}

public class IntentoInvalidApiKeyException: IntentoException
public class IntentoInvalidParameterException: IntentoException
{
protected internal IntentoInvalidApiKeyException(HttpResponseMessage response, object jsonResult)
: base(response, jsonResult)
public IntentoInvalidParameterException(string parameterName, string hint = null)
: base(hint != null ?
string.Format("Invalid {0} parameter: {1}", parameterName, hint) :
string.Format("Invalid {0} parameter", parameterName))
{ }
}
}
Binary file modified IntentoSDK/bin/Debug/IntentoSDK.dll
Binary file not shown.
10 changes: 6 additions & 4 deletions TestForm/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,10 @@ private void buttonSend_Click(object sender, EventArgs e)
Exception ex = ex2.InnerExceptions[0];
if (ex is IntentoInvalidApiKeyException)
textBoxResult.Text = string.Format("Invalid api key");
else if (ex is IntentoException)
textBoxResult.Text = string.Format("Exception {2}: {0}: {1}", ex.Message, ((IntentoException)ex).Content, ex.GetType().Name);
else if (ex is IntentoApiException)
textBoxResult.Text = string.Format("Api Exception {2}: {0}: {1}", ex.Message, ((IntentoApiException)ex).Content, ex.GetType().Name);
else if (ex is IntentoSdkException)
textBoxResult.Text = string.Format("Sdk Exception {1}: {0}", ex.Message, ex.GetType().Name);
else
textBoxResult.Text = string.Format("Unexpected exception {0}: {1}", ex.GetType().Name, ex.Message);

Expand Down Expand Up @@ -171,8 +173,8 @@ private string CheckAsync()
Exception ex = ex2.InnerExceptions[0];
if (ex is IntentoInvalidApiKeyException)
return string.Format("Error: Invalid api key");
else if (ex is IntentoException)
return string.Format("Error: Exception {2}: {0}: {1}", ex.Message, ((IntentoException)ex).Content, ex.GetType().Name);
else if (ex is IntentoApiException)
return string.Format("Error: Exception {2}: {0}: {1}", ex.Message, ((IntentoApiException)ex).Content, ex.GetType().Name);
else
return string.Format("Error: Unexpected exception {0}: {1}", ex.GetType().Name, ex.Message);
return null;
Expand Down
6 changes: 4 additions & 2 deletions TestForm/Form2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ public void Prepare(string apiKey)
Exception ex = ex2.InnerExceptions[0];
if (ex is IntentoInvalidApiKeyException)
labelError.Text = string.Format("Invalid api key");
else if (ex is IntentoException)
labelError.Text = string.Format("Exception {2}: {0}: {1}", ex.Message, ((IntentoException)ex).Content, ex.GetType().Name);
else if (ex is IntentoApiException)
labelError.Text = string.Format("Api Exception {2}: {0}: {1}", ex.Message, ((IntentoApiException)ex).Content, ex.GetType().Name);
else if (ex is IntentoSdkException)
labelError.Text = string.Format("Sdk Exception {1}: {0}", ex.Message, ex.GetType().Name);
else
labelError.Text = string.Format("Unexpected exception {0}: {1}", ex.GetType().Name, ex.Message);
return;
Expand Down
Binary file modified TestForm/bin/Debug/IntentoSDK.dll
Binary file not shown.
Binary file modified TestForm/bin/Debug/TestForm.exe
Binary file not shown.

0 comments on commit a9e19e1

Please sign in to comment.