Skip to content

Commit fb94617

Browse files
committed
Resolve review comments
1 parent 7d5d603 commit fb94617

File tree

2 files changed

+34
-30
lines changed

2 files changed

+34
-30
lines changed

src/ResourceManager/Resources/Commands.Resources/ActiveDirectory/Cmdlets/ActiveDirectoryBaseCmdlet.cs

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -51,47 +51,51 @@ private void HandleException(Exception exception)
5151
ErrorCategory targetErrorCategory = ErrorCategory.NotSpecified;
5252
var graphEx = exception as GraphErrorException;
5353

54-
if (graphEx != null)
54+
if (graphEx == null)
5555
{
56-
if (graphEx.Body != null && graphEx.Body.Message != null && graphEx.Body.Code != null)
56+
throw exception;
57+
}
58+
59+
if (graphEx.Body != null && graphEx.Body.Message != null && graphEx.Body.Code != null)
60+
{
61+
WriteDebug(String.Format(ProjectResources.GraphException, graphEx.Body.Code, graphEx.Body.Message));
62+
targetEx = new Exception(graphEx.Body.Message);
63+
targetErrorId = graphEx.Body.Code;
64+
}
65+
else
66+
{
67+
if (graphEx.Response != null && graphEx.Response.StatusCode == HttpStatusCode.NotFound)
5768
{
58-
WriteDebug(String.Format(ProjectResources.GraphException, graphEx.Body.Code, graphEx.Body.Message));
59-
targetEx = new Exception(graphEx.Body.Message);
60-
targetErrorId = graphEx.Body.Code;
69+
targetErrorCategory = ErrorCategory.InvalidArgument;
6170
}
6271
else
6372
{
64-
if (graphEx.Response != null && graphEx.Response.StatusCode == HttpStatusCode.NotFound)
65-
{
66-
targetErrorCategory = ErrorCategory.InvalidArgument;
67-
}
68-
else
69-
{
70-
targetErrorCategory = ErrorCategory.InvalidOperation;
71-
}
72-
Exception parsedException = ParseResponse(graphEx);
73-
targetEx = parsedException != null ? parsedException : targetEx;
73+
targetErrorCategory = ErrorCategory.InvalidOperation;
7474
}
75-
var errorRecord = new ErrorRecord(targetEx, targetErrorId, targetErrorCategory, null);
76-
WriteError(errorRecord);
77-
}
78-
else
79-
{
80-
throw exception;
75+
76+
Exception parsedException = ParseResponse(graphEx);
77+
targetEx = parsedException ?? targetEx;
8178
}
79+
80+
var errorRecord = new ErrorRecord(targetEx, targetErrorId, targetErrorCategory, null);
81+
WriteError(errorRecord);
8282
}
8383

8484

8585
private Exception ParseResponse(GraphErrorException graphEx)
8686
{
87-
int exceptionMessageIndex = graphEx.Response.Content.IndexOf("\"value\":", StringComparison.CurrentCultureIgnoreCase);
88-
if (exceptionMessageIndex > 0)
87+
if (graphEx?.Response?.Content != null)
8988
{
90-
string substring = graphEx.Response.Content.Substring(exceptionMessageIndex + 9);
91-
// the start index is added 9, so as to remove the delimiter \"value\":\
92-
string exceptionDetails = substring.Substring(0, substring.IndexOf("\"}"));
93-
return new Exception(exceptionDetails);
89+
int exceptionMessageIndex = graphEx.Response.Content.IndexOf("\"value\":", StringComparison.CurrentCultureIgnoreCase);
90+
if (exceptionMessageIndex > 0)
91+
{
92+
string substring = graphEx.Response.Content.Substring(exceptionMessageIndex + 9);
93+
// the start index is added 9, so as to remove the delimiter \"value\":\
94+
string exceptionDetails = substring.Substring(0, substring.IndexOf("\"}"));
95+
return new Exception(exceptionDetails);
96+
}
9497
}
98+
9599
return null;
96100
}
97101

src/ResourceManager/Resources/Commands.Resources/ActiveDirectory/Models/ActiveDirectoryClient.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public PSADServicePrincipal GetServicePrincipalBySPN(string spn)
9595
try
9696
{
9797
var odataQuery = new Rest.Azure.OData.ODataQuery<ServicePrincipal>(s => s.ServicePrincipalNames.Contains(spn));
98-
servicePrincipal = GraphClient.ServicePrincipals.List(odataQuery.ToString()).FirstOrDefault()?.ToPSADServicePrincipal();
98+
servicePrincipal = GraphClient.ServicePrincipals.List(odataQuery.ToString()).First().ToPSADServicePrincipal();
9999
}
100100
catch { /* The service principal does not exist, ignore the exception. */ }
101101

@@ -149,7 +149,7 @@ public IEnumerable<PSADServicePrincipal> FilterServicePrincipals(ADObjectFilterO
149149
if (!string.IsNullOrEmpty(options.SearchString) && options.SearchString.EndsWith("*"))
150150
{
151151
options.SearchString = options.SearchString.TrimEnd('*');
152-
odataQuery = new Rest.Azure.OData.ODataQuery<ServicePrincipal>(s => s.DisplayName.StartsWith(options.SearchString));
152+
odataQuery = new Rest.Azure.OData.ODataQuery<ServicePrincipal>(s => s.DisplayName != null && s.DisplayName.StartsWith(options.SearchString));
153153
}
154154
else
155155
{
@@ -216,7 +216,7 @@ public IEnumerable<PSADUser> FilterUsers(ADObjectFilterOptions options, ulong fi
216216
if (!string.IsNullOrEmpty(options.SearchString) && options.SearchString.EndsWith("*"))
217217
{
218218
options.SearchString = options.SearchString.TrimEnd('*');
219-
odataQuery = new Rest.Azure.OData.ODataQuery<User>(u => u.DisplayName.StartsWith(options.SearchString));
219+
odataQuery = new Rest.Azure.OData.ODataQuery<User>(u => u.DisplayName != null && u.DisplayName.StartsWith(options.SearchString));
220220
}
221221
else
222222
{

0 commit comments

Comments
 (0)