12
12
import { GraphError } from "./GraphError" ;
13
13
import { GraphRequestCallback } from "./IGraphRequestCallback" ;
14
14
15
+ /**
16
+ * @interface
17
+ * Signature for the json represent of the error response from the Graph API
18
+ * https://docs.microsoft.com/en-us/graph/errors
19
+ * @property {[key: string] : string | number } - The Key value pair
20
+ */
21
+ interface GraphAPIErrorResponse {
22
+ error : {
23
+ code : string ;
24
+ message : string ;
25
+ innerError : any ;
26
+ } ;
27
+ }
28
+
15
29
/**
16
30
* @class
17
31
* Class for GraphErrorHandler
@@ -41,7 +55,7 @@ export class GraphErrorHandler {
41
55
* @static
42
56
* @async
43
57
* Populates the GraphError instance from the Error returned by graph service
44
- * @param {any } error - The error returned by graph service or some native error
58
+ * @param {GraphAPIErrorResponse } graphError - The error possibly returned by graph service or some native error
45
59
* @param {number } statusCode - The status code of the response
46
60
* @returns A promise that resolves to GraphError instance
47
61
*
@@ -57,19 +71,17 @@ export class GraphErrorHandler {
57
71
* }
58
72
* }
59
73
*/
60
- private static constructErrorFromResponse ( error : any , statusCode : number ) : GraphError {
61
- error = error . error ;
74
+ private static constructErrorFromResponse ( graphError : GraphAPIErrorResponse , statusCode : number ) : GraphError {
75
+ const error = graphError . error ;
62
76
const gError = new GraphError ( statusCode , error . message ) ;
63
77
gError . code = error . code ;
64
78
if ( error . innerError !== undefined ) {
65
79
gError . requestId = error . innerError [ "request-id" ] ;
66
80
gError . date = new Date ( error . innerError . date ) ;
67
81
}
68
- try {
69
- gError . body = JSON . stringify ( error ) ;
70
- } catch ( error ) {
71
- // tslint:disable-line: no-empty
72
- }
82
+
83
+ gError . body = JSON . stringify ( error ) ;
84
+
73
85
return gError ;
74
86
}
75
87
@@ -78,6 +90,7 @@ export class GraphErrorHandler {
78
90
* @static
79
91
* @async
80
92
* To get the GraphError object
93
+ * Reference - https://docs.microsoft.com/en-us/graph/errors
81
94
* @param {any } [error = null] - The error returned by graph service or some native error
82
95
* @param {number } [statusCode = -1] - The status code of the response
83
96
* @param {GraphRequestCallback } [callback] - The graph request callback function
@@ -87,10 +100,11 @@ export class GraphErrorHandler {
87
100
let gError : GraphError ;
88
101
if ( error && error . error ) {
89
102
gError = GraphErrorHandler . constructErrorFromResponse ( error , statusCode ) ;
90
- } else if ( typeof Error !== "undefined" && error instanceof Error ) {
103
+ } else if ( error instanceof Error ) {
91
104
gError = GraphErrorHandler . constructError ( error , statusCode ) ;
92
105
} else {
93
106
gError = new GraphError ( statusCode ) ;
107
+ gError . body = error ; // if a custom error is passed which is not instance of Error object or a graph API response
94
108
}
95
109
if ( typeof callback === "function" ) {
96
110
callback ( gError , null ) ;
0 commit comments