From 289f5662400377318ef9ce33141451fb07dbb0a5 Mon Sep 17 00:00:00 2001 From: Nikitha Chettiar Date: Fri, 30 Oct 2020 01:30:19 -0700 Subject: [PATCH 1/9] custom error non error type set to Graph Error body --- src/CustomAuthenticationProvider.ts | 3 +++ src/GraphErrorHandler.ts | 11 ++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/CustomAuthenticationProvider.ts b/src/CustomAuthenticationProvider.ts index 58b7eb9f1..89ebd42b1 100644 --- a/src/CustomAuthenticationProvider.ts +++ b/src/CustomAuthenticationProvider.ts @@ -47,6 +47,9 @@ export class CustomAuthenticationProvider implements AuthenticationProvider { if (accessToken) { resolve(accessToken); } else { + if (!error) { + reject("Access token cannot be undefined or empty"); + } reject(error); } }); diff --git a/src/GraphErrorHandler.ts b/src/GraphErrorHandler.ts index 310596968..aa1b8a8eb 100644 --- a/src/GraphErrorHandler.ts +++ b/src/GraphErrorHandler.ts @@ -58,6 +58,11 @@ export class GraphErrorHandler { * } */ private static constructErrorFromResponse(error: any, statusCode: number): GraphError { + if (!error.error) { + const graphError = new GraphError(statusCode, "SDK failed to send the request"); + graphError.body = error; + return graphError; + } error = error.error; const gError = new GraphError(statusCode, error.message); gError.code = error.code; @@ -85,10 +90,10 @@ export class GraphErrorHandler { */ public static async getError(error: any = null, statusCode: number = -1, callback?: GraphRequestCallback): Promise { let gError: GraphError; - if (error && error.error) { - gError = GraphErrorHandler.constructErrorFromResponse(error, statusCode); - } else if (typeof Error !== "undefined" && error instanceof Error) { + if (typeof Error !== "undefined" && error instanceof Error) { gError = GraphErrorHandler.constructError(error, statusCode); + } else if (error) { + gError = GraphErrorHandler.constructErrorFromResponse(error, statusCode); } else { gError = new GraphError(statusCode); } From d06308ef3dd1736099e63f6e3b12ef2e4bd60c77 Mon Sep 17 00:00:00 2001 From: Nikitha Chettiar Date: Sun, 8 Nov 2020 20:15:07 -0800 Subject: [PATCH 2/9] Unit testing empty access token --- spec/core/Client.ts | 39 +++++++++++++++++++++++++++++ src/CustomAuthenticationProvider.ts | 3 ++- src/GraphErrorHandler.ts | 2 +- 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/spec/core/Client.ts b/spec/core/Client.ts index 55d98cc9e..dbc5f56e1 100644 --- a/spec/core/Client.ts +++ b/spec/core/Client.ts @@ -101,6 +101,45 @@ describe("Client.ts", () => { const response = await client.api("me").get(); assert.equal(response, responseBody); }); + + it("Should throw error in case the access token is undefined", async () => { + try { + const options = { + defaultVersion: "v1.0", + debugLogging: true, + authProvider: (done) => { + done(null, getTokenFunction()); + }, + }; + + const getTokenFunction = (): string => { + return undefined; + }; + const client = Client.init(options); + const res = await client.api("/test").get(); + } catch (error) { + assert.isDefined(error.body); + } + }); + + it("Should throw error in case the access token is empty", async () => { + try { + const options = { + defaultVersion: "v1.0", + debugLogging: true, + authProvider: (done) => { + done(null, getTokenFunction()); + }, + }; + const getTokenFunction = (): string => { + return ""; + }; + const client = Client.init(options); + const res = await client.api("/test").get(); + } catch (error) { + assert.isDefined(error.body); + } + }); }); describe("init", () => { diff --git a/src/CustomAuthenticationProvider.ts b/src/CustomAuthenticationProvider.ts index 89ebd42b1..63c732715 100644 --- a/src/CustomAuthenticationProvider.ts +++ b/src/CustomAuthenticationProvider.ts @@ -9,6 +9,7 @@ * @module CustomAuthenticationProvider */ +import { GraphError } from "./browser"; import { AuthenticationProvider } from "./IAuthenticationProvider"; import { AuthProvider } from "./IAuthProvider"; @@ -48,7 +49,7 @@ export class CustomAuthenticationProvider implements AuthenticationProvider { resolve(accessToken); } else { if (!error) { - reject("Access token cannot be undefined or empty"); + error = new GraphError(-1, "Access token cannot be undefined or empty."); } reject(error); } diff --git a/src/GraphErrorHandler.ts b/src/GraphErrorHandler.ts index aa1b8a8eb..8fc3c605f 100644 --- a/src/GraphErrorHandler.ts +++ b/src/GraphErrorHandler.ts @@ -59,7 +59,7 @@ export class GraphErrorHandler { */ private static constructErrorFromResponse(error: any, statusCode: number): GraphError { if (!error.error) { - const graphError = new GraphError(statusCode, "SDK failed to send the request"); + const graphError = new GraphError(statusCode, "Microsoft Graph JavaScript Client SDK came across an error while processing the request"); graphError.body = error; return graphError; } From cd9107cf22151468410d80262b040ddfda50c8e8 Mon Sep 17 00:00:00 2001 From: Nikitha Chettiar Date: Sun, 8 Nov 2020 20:21:08 -0800 Subject: [PATCH 3/9] Adding a comment --- src/GraphErrorHandler.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/GraphErrorHandler.ts b/src/GraphErrorHandler.ts index 8fc3c605f..07e286012 100644 --- a/src/GraphErrorHandler.ts +++ b/src/GraphErrorHandler.ts @@ -59,6 +59,7 @@ export class GraphErrorHandler { */ private static constructErrorFromResponse(error: any, statusCode: number): GraphError { if (!error.error) { + // example - Consider error specified is a string when using type AuthProviderCallback = (error: any, accessToken: string | null) => void; const graphError = new GraphError(statusCode, "Microsoft Graph JavaScript Client SDK came across an error while processing the request"); graphError.body = error; return graphError; From ee778f93cf286b1368dde8b9646785bd6c8646ab Mon Sep 17 00:00:00 2001 From: Nikitha Chettiar Date: Thu, 12 Nov 2020 17:19:25 -0800 Subject: [PATCH 4/9] Adding GraphClientError Class --- src/CustomAuthenticationProvider.ts | 9 +++--- src/GraphClientError.ts | 49 +++++++++++++++++++++++++++++ src/GraphRequest.ts | 5 +++ 3 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 src/GraphClientError.ts diff --git a/src/CustomAuthenticationProvider.ts b/src/CustomAuthenticationProvider.ts index 63c732715..88c9d9a61 100644 --- a/src/CustomAuthenticationProvider.ts +++ b/src/CustomAuthenticationProvider.ts @@ -9,7 +9,7 @@ * @module CustomAuthenticationProvider */ -import { GraphError } from "./browser"; +import { GraphClientError } from "./GraphClientError"; import { AuthenticationProvider } from "./IAuthenticationProvider"; import { AuthProvider } from "./IAuthProvider"; @@ -44,14 +44,15 @@ export class CustomAuthenticationProvider implements AuthenticationProvider { */ public async getAccessToken(): Promise { return new Promise((resolve: (accessToken: string) => void, reject: (error: any) => void) => { - this.provider((error: any, accessToken: string | null) => { + this.provider(async (error: any, accessToken: string | null) => { if (accessToken) { resolve(accessToken); } else { if (!error) { - error = new GraphError(-1, "Access token cannot be undefined or empty."); + error = new GraphClientError("Access token cannot be undefined or empty."); } - reject(error); + const err = await GraphClientError.getError(error); + reject(err); } }); }); diff --git a/src/GraphClientError.ts b/src/GraphClientError.ts new file mode 100644 index 000000000..e21627ed7 --- /dev/null +++ b/src/GraphClientError.ts @@ -0,0 +1,49 @@ +/** + * ------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. + * See License in the project root for license information. + * ------------------------------------------------------------------------------------------- + */ + +/** + * @module GraphClientError + */ + +/** + * @class + * This class is to handle errors that within the JavaScript Client SDK + * unlike GraphError Class which handles errors in response from the Graph API. + */ + +export class GraphClientError extends Error { + /** + * @public + * A member holding response by the graph service + */ + public body?: any; + + public constructor(message?: string, baseError?: Error) { + super(message || (baseError && baseError.message)); + this.stack = baseError ? baseError.stack : this.stack; + } + + /** + * @public + * @static + * @async + * To get the GraphError object + * @param {any} [error = null] - The error returned encountered by the Graph JavaScript Client + * SDK while processing request + * @returns A promise that resolves to GraphError instance + */ + public static async getError(error: any = null): Promise { + let graphClientError: GraphClientError; + if (typeof Error !== "undefined" && error instanceof Error) { + graphClientError = error; + } else if (error) { + graphClientError = new GraphClientError(); + graphClientError.body = error; + } + return graphClientError; + } +} diff --git a/src/GraphRequest.ts b/src/GraphRequest.ts index 7ec736be1..05980678e 100644 --- a/src/GraphRequest.ts +++ b/src/GraphRequest.ts @@ -10,6 +10,7 @@ */ import { GraphError } from "./GraphError"; +import { GraphClientError } from "./GraphClientError"; import { GraphErrorHandler } from "./GraphErrorHandler"; import { oDataQueryNames, serializeContent, urlJoin } from "./GraphRequestUtil"; import { GraphResponseHandler } from "./GraphResponseHandler"; @@ -379,7 +380,11 @@ export class GraphRequest { const response: any = await GraphResponseHandler.getResponse(rawResponse, this._responseType, callback); return response; } catch (error) { + if (error instanceof GraphClientError) { + throw error; + } let statusCode: number; + if (typeof rawResponse !== "undefined") { statusCode = rawResponse.status; } From 4c894a8335c33b8302e87402a2b1be290288c067 Mon Sep 17 00:00:00 2001 From: Nikitha Chettiar Date: Thu, 12 Nov 2020 17:21:20 -0800 Subject: [PATCH 5/9] Adding GraphClientError Class --- src/GraphClientError.ts | 10 +++++----- src/GraphRequest.ts | 3 +-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/GraphClientError.ts b/src/GraphClientError.ts index e21627ed7..fb1321021 100644 --- a/src/GraphClientError.ts +++ b/src/GraphClientError.ts @@ -22,11 +22,6 @@ export class GraphClientError extends Error { */ public body?: any; - public constructor(message?: string, baseError?: Error) { - super(message || (baseError && baseError.message)); - this.stack = baseError ? baseError.stack : this.stack; - } - /** * @public * @static @@ -46,4 +41,9 @@ export class GraphClientError extends Error { } return graphClientError; } + + public constructor(message?: string, baseError?: Error) { + super(message || (baseError && baseError.message)); + this.stack = baseError ? baseError.stack : this.stack; + } } diff --git a/src/GraphRequest.ts b/src/GraphRequest.ts index 05980678e..efa9abe56 100644 --- a/src/GraphRequest.ts +++ b/src/GraphRequest.ts @@ -8,9 +8,8 @@ /** * @module GraphRequest */ - -import { GraphError } from "./GraphError"; import { GraphClientError } from "./GraphClientError"; +import { GraphError } from "./GraphError"; import { GraphErrorHandler } from "./GraphErrorHandler"; import { oDataQueryNames, serializeContent, urlJoin } from "./GraphRequestUtil"; import { GraphResponseHandler } from "./GraphResponseHandler"; From faccdf62a675886282643edc99a30facfd05551f Mon Sep 17 00:00:00 2001 From: Nikitha Chettiar Date: Mon, 16 Nov 2020 16:48:40 -0800 Subject: [PATCH 6/9] Adding unit tests for graph client error class --- spec/core/Client.ts | 10 ++++--- spec/core/GraphClientError.ts | 34 +++++++++++++++++++++++ src/CustomAuthenticationProvider.ts | 7 +++-- src/GraphClientError.ts | 42 ++++++++++++++++++----------- src/GraphErrorHandler.ts | 28 ++++++++----------- 5 files changed, 84 insertions(+), 37 deletions(-) create mode 100644 spec/core/GraphClientError.ts diff --git a/spec/core/Client.ts b/spec/core/Client.ts index dbc5f56e1..4841c0402 100644 --- a/spec/core/Client.ts +++ b/spec/core/Client.ts @@ -10,6 +10,7 @@ import "isomorphic-fetch"; import { CustomAuthenticationProvider, TelemetryHandler } from "../../src"; import { Client } from "../../src/Client"; +import { GraphClientError } from "../../src/GraphClientError"; import { AuthProvider } from "../../src/IAuthProvider"; import { ClientOptions } from "../../src/IClientOptions"; import { Options } from "../../src/IOptions"; @@ -118,17 +119,19 @@ describe("Client.ts", () => { const client = Client.init(options); const res = await client.api("/test").get(); } catch (error) { - assert.isDefined(error.body); + assert.isTrue(error instanceof GraphClientError); + assert.isDefined(error.message); } }); it("Should throw error in case the access token is empty", async () => { + const customError = { message: "Token is empty" }; try { const options = { defaultVersion: "v1.0", debugLogging: true, authProvider: (done) => { - done(null, getTokenFunction()); + done(customError, getTokenFunction()); }, }; const getTokenFunction = (): string => { @@ -137,7 +140,8 @@ describe("Client.ts", () => { const client = Client.init(options); const res = await client.api("/test").get(); } catch (error) { - assert.isDefined(error.body); + assert.isTrue(error instanceof GraphClientError); + assert.equal(error.customError, customError); } }); }); diff --git a/spec/core/GraphClientError.ts b/spec/core/GraphClientError.ts new file mode 100644 index 000000000..1dd93bf65 --- /dev/null +++ b/spec/core/GraphClientError.ts @@ -0,0 +1,34 @@ +/** + * ------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. + * See License in the project root for license information. + * ------------------------------------------------------------------------------------------- + */ + +import { assert } from "chai"; + +import { GraphClientError } from "../../src/GraphClientError"; + +describe("GraphErrorHandler.ts", () => { + const message = "test"; + const name = "test_name"; + it("Should return GraphClientError error with message set", () => { + const gError = new GraphClientError(message); + assert.equal(gError.message, message); + }); + + it("Should return GraphClientError when Error object is passed", () => { + const errorParameter = new Error(message); + errorParameter.name = name; + const gError = GraphClientError.setGraphClientError(errorParameter); + assert.equal(gError.message, message); + assert.equal(gError.name, name); + }); + + it("Should return GraphClientError when custom error object is passed", () => { + const customErrorParameter = { errorName: name, errorMessage: message }; + const gError = GraphClientError.setGraphClientError(customErrorParameter); + assert.isDefined(gError.customError); + assert.equal(gError.customError, customErrorParameter); + }); +}); diff --git a/src/CustomAuthenticationProvider.ts b/src/CustomAuthenticationProvider.ts index 88c9d9a61..eac4ba6e3 100644 --- a/src/CustomAuthenticationProvider.ts +++ b/src/CustomAuthenticationProvider.ts @@ -49,9 +49,12 @@ export class CustomAuthenticationProvider implements AuthenticationProvider { resolve(accessToken); } else { if (!error) { - error = new GraphClientError("Access token cannot be undefined or empty."); + const invalidTokenMessage = "Access token is undefined or empty.\ + Please provide a valid token.\ + For more help - https://github.com/microsoftgraph/msgraph-sdk-javascript/blob/dev/docs/CustomAuthenticationProvider.md"; + error = new GraphClientError(invalidTokenMessage); } - const err = await GraphClientError.getError(error); + const err = await GraphClientError.setGraphClientError(error); reject(err); } }); diff --git a/src/GraphClientError.ts b/src/GraphClientError.ts index fb1321021..f6ef31a8d 100644 --- a/src/GraphClientError.ts +++ b/src/GraphClientError.ts @@ -11,39 +11,51 @@ /** * @class - * This class is to handle errors that within the JavaScript Client SDK - * unlike GraphError Class which handles errors in response from the Graph API. + * Create GraphClientError object to handle client-side errors + * encountered within the JavaScript Client SDK. + * Whereas GraphError Class should be used to handle errors in the response from the Graph API. */ export class GraphClientError extends Error { /** * @public - * A member holding response by the graph service + * A custom error. This property should set be when the error is not of instanceOf Error/GraphClientError. + * Example = + * const client = MicrosoftGraph.Client.init({ + * defaultVersion: "v1.0", + * authProvider: (done) => { done({TokenError:"AccessToken cannot be null"}, ""); + * }); */ - public body?: any; + public customError?: any; /** * @public * @static * @async - * To get the GraphError object - * @param {any} [error = null] - The error returned encountered by the Graph JavaScript Client - * SDK while processing request - * @returns A promise that resolves to GraphError instance + * To set the GraphClientError object + * @param {any} - The error returned encountered by the Graph JavaScript Client SDK while processing request + * @returns GraphClientError object set to the error passed */ - public static async getError(error: any = null): Promise { + public static setGraphClientError(error: any): GraphClientError { let graphClientError: GraphClientError; - if (typeof Error !== "undefined" && error instanceof Error) { + if (error instanceof Error) { graphClientError = error; - } else if (error) { + } else { graphClientError = new GraphClientError(); - graphClientError.body = error; + graphClientError.customError = error; } return graphClientError; } - public constructor(message?: string, baseError?: Error) { - super(message || (baseError && baseError.message)); - this.stack = baseError ? baseError.stack : this.stack; + /** + * @public + * @constructor + * Creates an instance of GraphClientError + * @param {string} message? - Error message + * @returns An instance of GraphClientError + */ + public constructor(message?: string) { + super(message); + Object.setPrototypeOf(this, GraphClientError.prototype); } } diff --git a/src/GraphErrorHandler.ts b/src/GraphErrorHandler.ts index 07e286012..5ac323ac4 100644 --- a/src/GraphErrorHandler.ts +++ b/src/GraphErrorHandler.ts @@ -41,7 +41,7 @@ export class GraphErrorHandler { * @static * @async * Populates the GraphError instance from the Error returned by graph service - * @param {any} error - The error returned by graph service or some native error + * @param {any} graphError - The error possibly returned by graph service or some native error * @param {number} statusCode - The status code of the response * @returns A promise that resolves to GraphError instance * @@ -57,25 +57,17 @@ export class GraphErrorHandler { * } * } */ - private static constructErrorFromResponse(error: any, statusCode: number): GraphError { - if (!error.error) { - // example - Consider error specified is a string when using type AuthProviderCallback = (error: any, accessToken: string | null) => void; - const graphError = new GraphError(statusCode, "Microsoft Graph JavaScript Client SDK came across an error while processing the request"); - graphError.body = error; - return graphError; - } - error = error.error; + private static constructErrorFromResponse(graphError: any, statusCode: number): GraphError { + const error = graphError.error; const gError = new GraphError(statusCode, error.message); gError.code = error.code; if (error.innerError !== undefined) { gError.requestId = error.innerError["request-id"]; gError.date = new Date(error.innerError.date); } - try { - gError.body = JSON.stringify(error); - } catch (error) { - // tslint:disable-line: no-empty - } + + gError.body = JSON.stringify(error); + return gError; } @@ -84,6 +76,7 @@ export class GraphErrorHandler { * @static * @async * To get the GraphError object + * Reference - https://docs.microsoft.com/en-us/graph/errors * @param {any} [error = null] - The error returned by graph service or some native error * @param {number} [statusCode = -1] - The status code of the response * @param {GraphRequestCallback} [callback] - The graph request callback function @@ -91,12 +84,13 @@ export class GraphErrorHandler { */ public static async getError(error: any = null, statusCode: number = -1, callback?: GraphRequestCallback): Promise { let gError: GraphError; - if (typeof Error !== "undefined" && error instanceof Error) { - gError = GraphErrorHandler.constructError(error, statusCode); - } else if (error) { + if (error && error.error) { gError = GraphErrorHandler.constructErrorFromResponse(error, statusCode); + } else if (error instanceof Error) { + gError = GraphErrorHandler.constructError(error, statusCode); } else { gError = new GraphError(statusCode); + gError.body = error; // if a custom error is passed which is not instance of Error object or a graph API response } if (typeof callback === "function") { callback(gError, null); From d4d1db0b62528a969a69329f56e2b2c64510a120 Mon Sep 17 00:00:00 2001 From: Nikitha Chettiar Date: Tue, 17 Nov 2020 11:32:29 -0800 Subject: [PATCH 7/9] Correcting file name and changing the rawresponse condition check --- spec/core/GraphClientError.ts | 2 +- src/GraphRequest.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/core/GraphClientError.ts b/spec/core/GraphClientError.ts index 1dd93bf65..b25569348 100644 --- a/spec/core/GraphClientError.ts +++ b/spec/core/GraphClientError.ts @@ -9,7 +9,7 @@ import { assert } from "chai"; import { GraphClientError } from "../../src/GraphClientError"; -describe("GraphErrorHandler.ts", () => { +describe("GraphClientError", () => { const message = "test"; const name = "test_name"; it("Should return GraphClientError error with message set", () => { diff --git a/src/GraphRequest.ts b/src/GraphRequest.ts index efa9abe56..050ed00c7 100644 --- a/src/GraphRequest.ts +++ b/src/GraphRequest.ts @@ -384,7 +384,7 @@ export class GraphRequest { } let statusCode: number; - if (typeof rawResponse !== "undefined") { + if (rawResponse) { statusCode = rawResponse.status; } const gError: GraphError = await GraphErrorHandler.getError(error, statusCode, callback); From 5a39925d624acd7c6777efe2ef0836ea5b5e8fe4 Mon Sep 17 00:00:00 2001 From: Nikitha Chettiar Date: Sun, 20 Dec 2020 23:36:40 -0800 Subject: [PATCH 8/9] adding the graph api response interface --- src/GraphErrorHandler.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/GraphErrorHandler.ts b/src/GraphErrorHandler.ts index 5ac323ac4..07fc15f9e 100644 --- a/src/GraphErrorHandler.ts +++ b/src/GraphErrorHandler.ts @@ -12,6 +12,20 @@ import { GraphError } from "./GraphError"; import { GraphRequestCallback } from "./IGraphRequestCallback"; +/** + * @interface + * Signature for the json represent of the error response from the Graph API + * https://docs.microsoft.com/en-us/graph/errors + * @property {[key: string] : string | number} - The Key value pair + */ +interface GraphAPIErrorResponse { + error: { + code: string; + message: string; + innerError: any; + }; +} + /** * @class * Class for GraphErrorHandler @@ -57,7 +71,7 @@ export class GraphErrorHandler { * } * } */ - private static constructErrorFromResponse(graphError: any, statusCode: number): GraphError { + private static constructErrorFromResponse(graphError: GraphAPIErrorResponse, statusCode: number): GraphError { const error = graphError.error; const gError = new GraphError(statusCode, error.message); gError.code = error.code; From ba5e318f35be3b38d34294b635f05356b43c3435 Mon Sep 17 00:00:00 2001 From: Nikitha Chettiar Date: Mon, 21 Dec 2020 10:56:22 -0800 Subject: [PATCH 9/9] Changing param type in doc --- src/GraphErrorHandler.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/GraphErrorHandler.ts b/src/GraphErrorHandler.ts index 07fc15f9e..3fe92be72 100644 --- a/src/GraphErrorHandler.ts +++ b/src/GraphErrorHandler.ts @@ -55,7 +55,7 @@ export class GraphErrorHandler { * @static * @async * Populates the GraphError instance from the Error returned by graph service - * @param {any} graphError - The error possibly returned by graph service or some native error + * @param {GraphAPIErrorResponse} graphError - The error possibly returned by graph service or some native error * @param {number} statusCode - The status code of the response * @returns A promise that resolves to GraphError instance *