Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Windows
pEp for Outlook
Commits
e5f5de6b
Commit
e5f5de6b
authored
Sep 28, 2021
by
Thomas
Browse files
Improve exception handling
parent
2f8e1862
Changes
2
Hide whitespace changes
Inline
Side-by-side
DPE/DPEWebClient.cs
View file @
e5f5de6b
...
@@ -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
)
{
{
...
...
DPE/DistributedPolicyEngine.cs
View file @
e5f5de6b
...
@@ -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
UpdatePatchMailItem
Exception
(
"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
UpdatePatchMailItem
Exception
(
"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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment