@@ -41,7 +41,7 @@ export class GraphErrorHandler {
4141 * @static
4242 * @async
4343 * 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
44+ * @param {any } graphError - The error possibly returned by graph service or some native error
4545 * @param {number } statusCode - The status code of the response
4646 * @returns A promise that resolves to GraphError instance
4747 *
@@ -57,25 +57,17 @@ export class GraphErrorHandler {
5757 * }
5858 * }
5959 */
60- private static constructErrorFromResponse ( error : any , statusCode : number ) : GraphError {
61- if ( ! error . error ) {
62- // example - Consider error specified is a string when using type AuthProviderCallback = (error: any, accessToken: string | null) => void;
63- const graphError = new GraphError ( statusCode , "Microsoft Graph JavaScript Client SDK came across an error while processing the request" ) ;
64- graphError . body = error ;
65- return graphError ;
66- }
67- error = error . error ;
60+ private static constructErrorFromResponse ( graphError : any , statusCode : number ) : GraphError {
61+ const error = graphError . error ;
6862 const gError = new GraphError ( statusCode , error . message ) ;
6963 gError . code = error . code ;
7064 if ( error . innerError !== undefined ) {
7165 gError . requestId = error . innerError [ "request-id" ] ;
7266 gError . date = new Date ( error . innerError . date ) ;
7367 }
74- try {
75- gError . body = JSON . stringify ( error ) ;
76- } catch ( error ) {
77- // tslint:disable-line: no-empty
78- }
68+
69+ gError . body = JSON . stringify ( error ) ;
70+
7971 return gError ;
8072 }
8173
@@ -84,19 +76,21 @@ export class GraphErrorHandler {
8476 * @static
8577 * @async
8678 * To get the GraphError object
79+ * Reference - https://docs.microsoft.com/en-us/graph/errors
8780 * @param {any } [error = null] - The error returned by graph service or some native error
8881 * @param {number } [statusCode = -1] - The status code of the response
8982 * @param {GraphRequestCallback } [callback] - The graph request callback function
9083 * @returns A promise that resolves to GraphError instance
9184 */
9285 public static async getError ( error : any = null , statusCode : number = - 1 , callback ?: GraphRequestCallback ) : Promise < GraphError > {
9386 let gError : GraphError ;
94- if ( typeof Error !== "undefined" && error instanceof Error ) {
95- gError = GraphErrorHandler . constructError ( error , statusCode ) ;
96- } else if ( error ) {
87+ if ( error && error . error ) {
9788 gError = GraphErrorHandler . constructErrorFromResponse ( error , statusCode ) ;
89+ } else if ( error instanceof Error ) {
90+ gError = GraphErrorHandler . constructError ( error , statusCode ) ;
9891 } else {
9992 gError = new GraphError ( statusCode ) ;
93+ gError . body = error ; // if a custom error is passed which is not instance of Error object or a graph API response
10094 }
10195 if ( typeof callback === "function" ) {
10296 callback ( gError , null ) ;
0 commit comments