Skip to content

Commit ea00ad5

Browse files
authored
Merge pull request #334 from darshanhs90/darshanhs90/adapplnissue
Fix ADappln exception issue
2 parents 05ee9d0 + 1167845 commit ea00ad5

File tree

2 files changed

+182
-169
lines changed

2 files changed

+182
-169
lines changed

src/Common/Commands.Common.Graph.RBAC/ActiveDirectory/ActiveDirectoryBaseCmdlet.cs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,19 @@ private void HandleException(Exception exception)
5353

5454
if (graphEx != null)
5555
{
56-
if (graphEx.Body != null)
57-
{
56+
if (graphEx.Body != null && graphEx.Body.Message != null && graphEx.Body.Code != null) {
5857
WriteDebug(String.Format(ProjectResources.GraphException, graphEx.Body.Code, graphEx.Body.Message));
5958
targetEx = new Exception(graphEx.Body.Message);
6059
targetErrorId = graphEx.Body.Code;
60+
} else {
61+
if (graphEx.Response != null && graphEx.Response.StatusCode == HttpStatusCode.NotFound) {
62+
targetErrorCategory = ErrorCategory.InvalidArgument;
63+
} else {
64+
targetErrorCategory = ErrorCategory.InvalidOperation;
65+
}
66+
Exception parsedException = ParseResponse(graphEx);
67+
targetEx = parsedException != null? parsedException : targetEx;
6168
}
62-
63-
if (graphEx.Response != null && graphEx.Response.StatusCode == HttpStatusCode.NotFound)
64-
{
65-
targetErrorCategory = ErrorCategory.InvalidArgument;
66-
}
67-
else
68-
{
69-
targetErrorCategory = ErrorCategory.InvalidOperation;
70-
}
71-
7269
var errorRecord = new ErrorRecord(targetEx, targetErrorId, targetErrorCategory, null);
7370
WriteError(errorRecord);
7471
}
@@ -77,6 +74,19 @@ private void HandleException(Exception exception)
7774
throw exception;
7875
}
7976
}
77+
78+
79+
private Exception ParseResponse(GraphErrorException graphEx) {
80+
int exceptionMessageIndex = graphEx.Response.Content.IndexOf("\"value\":", StringComparison.CurrentCultureIgnoreCase);
81+
if (exceptionMessageIndex > 0)
82+
{
83+
string substring = graphEx.Response.Content.Substring(exceptionMessageIndex+9);
84+
// the start index is added 9, so as to remove the delimiter \"value\":\
85+
string exceptionDetails = substring.Substring(0,substring.IndexOf("\"}"));
86+
return new Exception(exceptionDetails);
87+
}
88+
return null;
89+
}
8090

8191
protected void ExecutionBlock(Action execAction)
8292
{

0 commit comments

Comments
 (0)