Commit e5f5de6b authored by Thomas's avatar Thomas
Browse files

Improve exception handling

parent 2f8e1862
...@@ -18,8 +18,8 @@ namespace pEp.DPE ...@@ -18,8 +18,8 @@ namespace pEp.DPE
/// </summary> /// </summary>
/// <param name="patch">The patch to reject.</param> /// <param name="patch">The patch to reject.</param>
/// <param name="me">The own identity that rejects the patch.</param> /// <param name="me">The own identity that rejects the patch.</param>
/// <exception cref="ArgumentNullException" /> /// <exception cref="ArgumentNullException">The post URI is null.</exception>
/// <exception cref="HttpRequestException" /> /// <exception cref="HttpRequestException">The HTTP request failed.</exception>
/// <returns>The Http response.</returns> /// <returns>The Http response.</returns>
public static async Task<HttpResponseMessage> RejectPatch(Patch patch, PEPIdentity me) public static async Task<HttpResponseMessage> RejectPatch(Patch patch, PEPIdentity me)
{ {
......
...@@ -145,7 +145,7 @@ namespace pEp.DPE ...@@ -145,7 +145,7 @@ namespace pEp.DPE
{ {
Outlook.Folder inbox = null; Outlook.Folder inbox = null;
Outlook.Items items = null; Outlook.Items items = null;
try try
{ {
inbox = Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) as Outlook.Folder; inbox = Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) as Outlook.Folder;
...@@ -168,7 +168,7 @@ namespace pEp.DPE ...@@ -168,7 +168,7 @@ namespace pEp.DPE
// Now find the one with the respective patch id // Now find the one with the respective patch id
string filter = string.Format("[{0}] = '{1}'", MailItemExtensions.USER_PROPERTY_KEY_DPE_PATCH_ID, patch.Id); string filter = string.Format("[{0}] = '{1}'", MailItemExtensions.USER_PROPERTY_KEY_DPE_PATCH_ID, patch.Id);
items = items.Restrict(filter); items = items.Restrict(filter);
if (items.Count > 1) if (items.Count > 1)
{ {
Log.Error("GetPatchMailItem: More than one item found with patch id " + patch.Id); Log.Error("GetPatchMailItem: More than one item found with patch id " + patch.Id);
...@@ -206,42 +206,32 @@ namespace pEp.DPE ...@@ -206,42 +206,32 @@ namespace pEp.DPE
/// <param name="patch">The patch to post.</param> /// <param name="patch">The patch to post.</param>
/// <param name="me">The own identity.</param> /// <param name="me">The own identity.</param>
/// <param name="postAction">The POST action to perform.</param> /// <param name="postAction">The POST action to perform.</param>
/// <returns></returns> /// <exception cref="HttpRequestException">The HTTP request failed.</exception>
private async Task<Exception> PostAsync(Patch patch, PEPIdentity me, PostAction postAction) private async Task PostAsync(Patch patch, PEPIdentity me, PostAction postAction)
{ {
// Determine the correct method to execute // Determine the correct method to execute
Task<HttpResponseMessage> postTask = null; HttpResponseMessage httpResponseMessage = null;
switch (postAction) switch (postAction)
{ {
case PostAction.Reject: case PostAction.Reject:
postTask = DPEWebClient.RejectPatch(patch, me); httpResponseMessage = await DPEWebClient.RejectPatch(patch, me);
break; break;
case PostAction.Suggest: case PostAction.Suggest:
postTask = DPEWebClient.SuggestPatch(patch, me); httpResponseMessage = await DPEWebClient.SuggestPatch(patch, me);
break; break;
case PostAction.Support: case PostAction.Support:
postTask = DPEWebClient.SupportPatch(patch, me); httpResponseMessage = await DPEWebClient.SupportPatch(patch, me);
break; break;
default: default:
Log.ErrorAndFailInDebugMode("PostAsync: Unknown post action."); Log.ErrorAndFailInDebugMode("PostAsync: Unknown post action.");
break; break;
} }
// Execute the task and return status if (httpResponseMessage.StatusCode != HttpStatusCode.OK)
return await postTask.ContinueWith((task) =>
{ {
if (task.Exception != null) Log.Error("PostAsync: Status code is " + Enum.GetName(typeof(HttpStatusCode), httpResponseMessage.StatusCode));
{ throw new HttpRequestException(Enum.GetName(typeof(HttpStatusCode), httpResponseMessage.StatusCode));
Log.Error("ExecuteAsync: Error executing POST of type " + Enum.GetName(typeof(PostAction), postAction)); }
return task.Exception;
}
else
{
return (task.Result.StatusCode != HttpStatusCode.OK) ?
new Exception(Enum.GetName(typeof(HttpStatusCode), task.Result.StatusCode)) :
null;
}
});
} }
/// <summary> /// <summary>
...@@ -249,9 +239,15 @@ namespace pEp.DPE ...@@ -249,9 +239,15 @@ namespace pEp.DPE
/// </summary> /// </summary>
/// <param name="patch">The patch to reject.</param> /// <param name="patch">The patch to reject.</param>
/// <param name="me">The own identity that rejects the patch.</param> /// <param name="me">The own identity that rejects the patch.</param>
/// <exception cref="HttpRequestException">The HTTP request failed.</exception>
/// <exception cref="UpdatePatchMailItemException">The update of the mail item failed.</exception>
public async Task Reject(Patch patch, PEPIdentity me) public async Task Reject(Patch patch, PEPIdentity me)
{ {
if (await this.PostAsync(patch, me, PostAction.Reject) is Exception ex) try
{
await this.PostAsync(patch, me, PostAction.Reject);
}
catch (HttpRequestException ex)
{ {
throw ex; throw ex;
} }
...@@ -262,7 +258,7 @@ namespace pEp.DPE ...@@ -262,7 +258,7 @@ namespace pEp.DPE
} }
else else
{ {
throw new Exception("Error updating patch mail item."); throw new UpdatePatchMailItemException("Error updating patch mail item.");
} }
} }
...@@ -318,9 +314,14 @@ namespace pEp.DPE ...@@ -318,9 +314,14 @@ namespace pEp.DPE
/// </summary> /// </summary>
/// <param name="patch">The patch to suggest.</param> /// <param name="patch">The patch to suggest.</param>
/// <param name="me">The own identity that suggests the patch.</param> /// <param name="me">The own identity that suggests the patch.</param>
/// <exception cref="HttpRequestException">The HTTP request failed.</exception>
public async Task Suggest(Patch patch, PEPIdentity me) public async Task Suggest(Patch patch, PEPIdentity me)
{ {
if (await this.PostAsync(patch, me, PostAction.Suggest) is Exception ex) try
{
await this.PostAsync(patch, me, PostAction.Suggest);
}
catch (HttpRequestException ex)
{ {
throw ex; throw ex;
} }
...@@ -333,20 +334,26 @@ namespace pEp.DPE ...@@ -333,20 +334,26 @@ namespace pEp.DPE
/// </summary> /// </summary>
/// <param name="patch">The patch to support.</param> /// <param name="patch">The patch to support.</param>
/// <param name="me">The own identity that supports the patch.</param> /// <param name="me">The own identity that supports the patch.</param>
/// <exception cref="HttpRequestException">The HTTP request failed.</exception>
/// <exception cref="UpdatePatchMailItemException">The update of the mail item failed.</exception>
public async Task Support(Patch patch, PEPIdentity me) public async Task Support(Patch patch, PEPIdentity me)
{ {
if (await this.PostAsync(patch, me, PostAction.Support) is Exception ex) try
{
await this.PostAsync(patch, me, PostAction.Support);
}
catch (HttpRequestException ex)
{ {
throw ex; throw ex;
} }
if (this.UpdatePatchMailItem(patch, PatchStatus.Supported)) if (this.UpdatePatchMailItem(patch, PatchStatus.Supported))
{ {
AdapterExtensions.ShowNotification("Patch supported", patch.CommitMessage); AdapterExtensions.ShowNotification("Patch supported", patch.CommitMessage);
} }
else else
{ {
throw new Exception("Error updating patch mail item."); throw new UpdatePatchMailItemException("Error updating patch mail item.");
} }
} }
...@@ -362,5 +369,15 @@ namespace pEp.DPE ...@@ -362,5 +369,15 @@ namespace pEp.DPE
} }
#endregion #endregion
/// <summary>
/// Custom exception for when the update of the patch mail item fails.
/// </summary>
public class UpdatePatchMailItemException : Exception
{
public UpdatePatchMailItemException(string message) : base(message)
{
}
}
} }
} }
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment