Skip to content

Commit 36074b1

Browse files
committed
udpate code
1 parent c109109 commit 36074b1

File tree

2 files changed

+193
-169
lines changed

2 files changed

+193
-169
lines changed

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

Lines changed: 33 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,30 @@ private void HandleException(Exception exception)
7774
throw exception;
7875
}
7976
}
77+
78+
private Exception ParseResponse(GraphErrorException graphEx) {
79+
int exceptionMessageIndex = graphEx.Response.Content.IndexOf("\"value\":", StringComparison.CurrentCultureIgnoreCase);
80+
if (exceptionMessageIndex > 0)
81+
{
82+
string substring = graphEx.Response.Content.Substring(exceptionMessageIndex+9);
83+
// the start index is added 9, so as to remove the delimiter \"value\":\
84+
string exceptionDetails = substring.Substring(0,substring.IndexOf("\"}"));
85+
return new Exception(exceptionDetails);
86+
}
87+
return null;
88+
}
89+
90+
private Exception ParseResponse(GraphErrorException graphEx) {
91+
int exceptionMessageIndex = graphEx.Response.Content.IndexOf("\"value\":", StringComparison.CurrentCultureIgnoreCase);
92+
if (exceptionMessageIndex > 0)
93+
{
94+
string substring = graphEx.Response.Content.Substring(exceptionMessageIndex+9);
95+
// the start index is added 9, so as to remove the delimiter \"value\":\
96+
string exceptionDetails = substring.Substring(0,substring.IndexOf("\"}"));
97+
return new Exception(exceptionDetails);
98+
}
99+
return null;
100+
}
80101

81102
protected void ExecutionBlock(Action execAction)
82103
{

0 commit comments

Comments
 (0)