1212import { GraphError } from "./GraphError" ;
1313import { GraphRequestCallback } from "./IGraphRequestCallback" ;
1414
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+
1529/**
1630 * @class
1731 * Class for GraphErrorHandler
@@ -41,7 +55,7 @@ export class GraphErrorHandler {
4155 * @static
4256 * @async
4357 * 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
4559 * @param {number } statusCode - The status code of the response
4660 * @returns A promise that resolves to GraphError instance
4761 *
@@ -57,19 +71,17 @@ export class GraphErrorHandler {
5771 * }
5872 * }
5973 */
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 ;
6276 const gError = new GraphError ( statusCode , error . message ) ;
6377 gError . code = error . code ;
6478 if ( error . innerError !== undefined ) {
6579 gError . requestId = error . innerError [ "request-id" ] ;
6680 gError . date = new Date ( error . innerError . date ) ;
6781 }
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+
7385 return gError ;
7486 }
7587
@@ -78,6 +90,7 @@ export class GraphErrorHandler {
7890 * @static
7991 * @async
8092 * To get the GraphError object
93+ * Reference - https://docs.microsoft.com/en-us/graph/errors
8194 * @param {any } [error = null] - The error returned by graph service or some native error
8295 * @param {number } [statusCode = -1] - The status code of the response
8396 * @param {GraphRequestCallback } [callback] - The graph request callback function
@@ -87,10 +100,11 @@ export class GraphErrorHandler {
87100 let gError : GraphError ;
88101 if ( error && error . error ) {
89102 gError = GraphErrorHandler . constructErrorFromResponse ( error , statusCode ) ;
90- } else if ( typeof Error !== "undefined" && error instanceof Error ) {
103+ } else if ( error instanceof Error ) {
91104 gError = GraphErrorHandler . constructError ( error , statusCode ) ;
92105 } else {
93106 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
94108 }
95109 if ( typeof callback === "function" ) {
96110 callback ( gError , null ) ;
0 commit comments