@@ -53,22 +53,19 @@ private void HandleException(Exception exception)
53
53
54
54
if ( graphEx != null )
55
55
{
56
- if ( graphEx . Body != null )
57
- {
56
+ if ( graphEx . Body != null && graphEx . Body . Message != null && graphEx . Body . Code != null ) {
58
57
WriteDebug ( String . Format ( ProjectResources . GraphException , graphEx . Body . Code , graphEx . Body . Message ) ) ;
59
58
targetEx = new Exception ( graphEx . Body . Message ) ;
60
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 ;
61
68
}
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
-
72
69
var errorRecord = new ErrorRecord ( targetEx , targetErrorId , targetErrorCategory , null ) ;
73
70
WriteError ( errorRecord ) ;
74
71
}
@@ -77,6 +74,19 @@ private void HandleException(Exception exception)
77
74
throw exception ;
78
75
}
79
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
+ }
80
90
81
91
protected void ExecutionBlock ( Action execAction )
82
92
{
0 commit comments