@@ -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,30 @@ private void HandleException(Exception exception)
77
74
throw exception ;
78
75
}
79
76
}
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
+ }
80
101
81
102
protected void ExecutionBlock ( Action execAction )
82
103
{
0 commit comments