|
| 1 | +// ---------------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// Copyright Microsoft Corporation |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | +// ---------------------------------------------------------------------------------- |
| 14 | + |
| 15 | +using Microsoft.Azure.Commands.ResourceManager.Common; |
| 16 | +using Microsoft.Azure.Graph.RBAC.Version1_6_20190326.Models; |
| 17 | +using System; |
| 18 | +using System.Management.Automation; |
| 19 | +using System.Net; |
| 20 | +using ProjectResources = Microsoft.Azure.Commands.Common.Graph.RBAC.Properties.Resources; |
| 21 | + |
| 22 | +namespace Microsoft.Azure.Graph.RBAC.Version1_6_20190326.ActiveDirectory |
| 23 | +{ |
| 24 | + public abstract class ActiveDirectoryBaseCmdlet : AzureRMCmdlet |
| 25 | + { |
| 26 | + private ActiveDirectoryClient activeDirectoryClient; |
| 27 | + |
| 28 | + public ActiveDirectoryClient ActiveDirectoryClient |
| 29 | + { |
| 30 | + get |
| 31 | + { |
| 32 | + if (activeDirectoryClient == null) |
| 33 | + { |
| 34 | + activeDirectoryClient = new ActiveDirectoryClient(DefaultProfile.DefaultContext); |
| 35 | + } |
| 36 | + |
| 37 | + return activeDirectoryClient; |
| 38 | + } |
| 39 | + |
| 40 | + set { activeDirectoryClient = value; } |
| 41 | + } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Handles graph exceptions thrown by client |
| 45 | + /// </summary> |
| 46 | + /// <param name="exception"></param> |
| 47 | + private void HandleException(Exception exception) |
| 48 | + { |
| 49 | + Exception targetEx = exception; |
| 50 | + string targetErrorId = String.Empty; |
| 51 | + ErrorCategory targetErrorCategory = ErrorCategory.NotSpecified; |
| 52 | + var graphEx = exception as GraphErrorException; |
| 53 | + |
| 54 | + if (graphEx != null) |
| 55 | + { |
| 56 | + if (graphEx.Body != null && graphEx.Body.Message != null && graphEx.Body.Code != null) { |
| 57 | + WriteDebug(String.Format(ProjectResources.GraphException, graphEx.Body.Code, graphEx.Body.Message)); |
| 58 | + targetEx = new Exception(graphEx.Body.Message); |
| 59 | + 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; |
| 68 | + } |
| 69 | + var errorRecord = new ErrorRecord(targetEx, targetErrorId, targetErrorCategory, null); |
| 70 | + WriteError(errorRecord); |
| 71 | + } |
| 72 | + else |
| 73 | + { |
| 74 | + throw exception; |
| 75 | + } |
| 76 | + } |
| 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 | + } |
| 90 | + |
| 91 | + protected void ExecutionBlock(Action execAction) |
| 92 | + { |
| 93 | + try |
| 94 | + { |
| 95 | + execAction(); |
| 96 | + } |
| 97 | + catch (Exception exception) |
| 98 | + { |
| 99 | + WriteDebug(String.Format(ProjectResources.ExceptionInExecution, exception.GetType())); |
| 100 | + HandleException(exception); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments