From bf20250e51e4c045227e8e2371abcf53e3f2a288 Mon Sep 17 00:00:00 2001 From: Nikitha Chettiar Date: Wed, 28 Dec 2022 17:19:31 -0800 Subject: [PATCH 1/4] headers prop in GraphError --- src/GraphError.ts | 2 ++ src/GraphErrorHandler.ts | 12 +++++++----- src/GraphRequest.ts | 5 ++++- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/GraphError.ts b/src/GraphError.ts index b846fb3f8..1e3b2dc54 100644 --- a/src/GraphError.ts +++ b/src/GraphError.ts @@ -42,6 +42,8 @@ export class GraphError extends Error { */ public date: Date; + public headers?: Headers; + /** * @public * A member holding original error response by the graph service diff --git a/src/GraphErrorHandler.ts b/src/GraphErrorHandler.ts index 4703a0002..8fa3b666a 100644 --- a/src/GraphErrorHandler.ts +++ b/src/GraphErrorHandler.ts @@ -40,13 +40,14 @@ export class GraphErrorHandler { * @param {number} [statusCode] - The status code of the response * @returns The GraphError instance */ - private static constructError(error: Error, statusCode?: number): GraphError { + private static constructError(error: Error, statusCode?: number, rawResponse?: Response): GraphError { const gError = new GraphError(statusCode, "", error); if (error.name !== undefined) { gError.code = error.name; } gError.body = error.toString(); gError.date = new Date(); + gError.headers = rawResponse?.headers; return gError; } @@ -71,7 +72,7 @@ export class GraphErrorHandler { * } * } */ - private static constructErrorFromResponse(graphError: GraphAPIErrorResponse, statusCode: number): GraphError { + private static constructErrorFromResponse(graphError: GraphAPIErrorResponse, statusCode: number, rawResponse?: Response): GraphError { const error = graphError.error; const gError = new GraphError(statusCode, error.message); gError.code = error.code; @@ -81,6 +82,7 @@ export class GraphErrorHandler { } gError.body = JSON.stringify(error); + gError.headers = rawResponse?.headers; return gError; } @@ -96,12 +98,12 @@ export class GraphErrorHandler { * @param {GraphRequestCallback} [callback] - The graph request callback function * @returns A promise that resolves to GraphError instance */ - public static async getError(error: any = null, statusCode = -1, callback?: GraphRequestCallback): Promise { + public static async getError(error: any = null, statusCode = -1, callback?: GraphRequestCallback, rawResponse?: Response): Promise { let gError: GraphError; if (error && error.error) { - gError = GraphErrorHandler.constructErrorFromResponse(error, statusCode); + gError = GraphErrorHandler.constructErrorFromResponse(error, statusCode, rawResponse); } else if (error instanceof Error) { - gError = GraphErrorHandler.constructError(error, statusCode); + gError = GraphErrorHandler.constructError(error, statusCode, rawResponse); } 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 diff --git a/src/GraphRequest.ts b/src/GraphRequest.ts index e7f5999df..eb21c00b9 100644 --- a/src/GraphRequest.ts +++ b/src/GraphRequest.ts @@ -377,6 +377,9 @@ export class GraphRequest { }); rawResponse = context.response; + // if (rawResponse.status >= 400 && rawResponse.status < 500) { + // throw new GraphError(rawResponse.status,) + // } const response: any = await GraphResponseHandler.getResponse(rawResponse, this._responseType, callback); return response; } catch (error) { @@ -388,7 +391,7 @@ export class GraphRequest { if (rawResponse) { statusCode = rawResponse.status; } - const gError: GraphError = await GraphErrorHandler.getError(error, statusCode, callback); + const gError: GraphError = await GraphErrorHandler.getError(error, statusCode, callback, rawResponse); throw gError; } } From cd45b63ad8d72571923ca3d0851fc93bf67031a5 Mon Sep 17 00:00:00 2001 From: Nikitha Chettiar Date: Thu, 12 Jan 2023 00:33:08 -0800 Subject: [PATCH 2/4] adding test --- src/GraphError.ts | 2 +- src/GraphErrorHandler.ts | 21 +++++++++++++++++++-- test/common/core/GraphErrorHandler.ts | 19 +++++++++++++++++++ 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/GraphError.ts b/src/GraphError.ts index 1e3b2dc54..655f07e2b 100644 --- a/src/GraphError.ts +++ b/src/GraphError.ts @@ -42,7 +42,7 @@ export class GraphError extends Error { */ public date: Date; - public headers?: Headers; + public headers?: Record; /** * @public diff --git a/src/GraphErrorHandler.ts b/src/GraphErrorHandler.ts index 8fa3b666a..248e78610 100644 --- a/src/GraphErrorHandler.ts +++ b/src/GraphErrorHandler.ts @@ -47,7 +47,7 @@ export class GraphErrorHandler { } gError.body = error.toString(); gError.date = new Date(); - gError.headers = rawResponse?.headers; + this.setErrorHeaders(gError, rawResponse?.headers); return gError; } @@ -82,11 +82,28 @@ export class GraphErrorHandler { } gError.body = JSON.stringify(error); - gError.headers = rawResponse?.headers; + //if(rawResponse?.headers){} + this.setErrorHeaders(gError, rawResponse?.headers); return gError; } + private static setErrorHeaders(gError: GraphError, headers?: Headers) { + if (headers) { + gError.headers = {}; + } else return; + if (typeof Headers !== "undefined" && headers instanceof Headers) { + headers.forEach((val, key) => { + if (gError.headers) gError.headers[val] = key; + }); + } else if (headers instanceof Array) { + for (let i = 0, l = headers.length; i < l; i++) { + gError.headers[headers[i][0]] = headers[i][1]; + } + } else { + gError.headers = headers as unknown as Record; + } + } /** * @public * @static diff --git a/test/common/core/GraphErrorHandler.ts b/test/common/core/GraphErrorHandler.ts index f133953b2..7295e184e 100644 --- a/test/common/core/GraphErrorHandler.ts +++ b/test/common/core/GraphErrorHandler.ts @@ -108,5 +108,24 @@ describe("GraphErrorHandler.ts", () => { assert.equal(gError.body, null); assert.equal(gError.requestId, null); }); + + it("Should get header from response", async () => { + const headers = { keyTest: "valueTest" }; + const errorResponse = { + error: { + code: "500", + message: "Internal Server Error", + innerError: { + "request-id": "some random id", + }, + }, + }; + const rawResponse = new Response(undefined, { + headers, + }); + const gError = await GraphErrorHandler.getError(errorResponse, 500, undefined, rawResponse); + assert.isDefined(gError.headers); + assert.equal(gError.headers, headers); + }); }); }); From c57728baebc31837448a464fd0680e188e8ad0ea Mon Sep 17 00:00:00 2001 From: Nikitha Chettiar Date: Thu, 12 Jan 2023 22:18:29 -0800 Subject: [PATCH 3/4] changing the headers type --- shims.d.ts | 2 ++ src/GraphError.ts | 2 +- src/GraphErrorHandler.ts | 21 ++------------------- test/common/core/GraphErrorHandler.ts | 4 ++-- 4 files changed, 7 insertions(+), 22 deletions(-) diff --git a/shims.d.ts b/shims.d.ts index d62ba4667..fac059d8c 100644 --- a/shims.d.ts +++ b/shims.d.ts @@ -19,3 +19,5 @@ interface NodeStream { read(size?: number): any; on(event: string | symbol, listener: (...args: any[]) => void): this; } + +interface Headers{} \ No newline at end of file diff --git a/src/GraphError.ts b/src/GraphError.ts index 655f07e2b..1e3b2dc54 100644 --- a/src/GraphError.ts +++ b/src/GraphError.ts @@ -42,7 +42,7 @@ export class GraphError extends Error { */ public date: Date; - public headers?: Record; + public headers?: Headers; /** * @public diff --git a/src/GraphErrorHandler.ts b/src/GraphErrorHandler.ts index 248e78610..b0fdee2c4 100644 --- a/src/GraphErrorHandler.ts +++ b/src/GraphErrorHandler.ts @@ -47,7 +47,7 @@ export class GraphErrorHandler { } gError.body = error.toString(); gError.date = new Date(); - this.setErrorHeaders(gError, rawResponse?.headers); + gError.headers = rawResponse?.headers; return gError; } @@ -82,28 +82,11 @@ export class GraphErrorHandler { } gError.body = JSON.stringify(error); - //if(rawResponse?.headers){} - this.setErrorHeaders(gError, rawResponse?.headers); + gError.headers = rawResponse?.headers; return gError; } - private static setErrorHeaders(gError: GraphError, headers?: Headers) { - if (headers) { - gError.headers = {}; - } else return; - if (typeof Headers !== "undefined" && headers instanceof Headers) { - headers.forEach((val, key) => { - if (gError.headers) gError.headers[val] = key; - }); - } else if (headers instanceof Array) { - for (let i = 0, l = headers.length; i < l; i++) { - gError.headers[headers[i][0]] = headers[i][1]; - } - } else { - gError.headers = headers as unknown as Record; - } - } /** * @public * @static diff --git a/test/common/core/GraphErrorHandler.ts b/test/common/core/GraphErrorHandler.ts index 7295e184e..20cda13aa 100644 --- a/test/common/core/GraphErrorHandler.ts +++ b/test/common/core/GraphErrorHandler.ts @@ -121,11 +121,11 @@ describe("GraphErrorHandler.ts", () => { }, }; const rawResponse = new Response(undefined, { - headers, + headers: new Headers(headers), }); const gError = await GraphErrorHandler.getError(errorResponse, 500, undefined, rawResponse); assert.isDefined(gError.headers); - assert.equal(gError.headers, headers); + assert.equal(gError.headers?.get("keyTest"), headers.keyTest); }); }); }); From 25ffdcf2ad42dc117f3900ca4684829de344e259 Mon Sep 17 00:00:00 2001 From: Nikitha Chettiar Date: Thu, 12 Jan 2023 22:22:04 -0800 Subject: [PATCH 4/4] remove commented code --- src/GraphRequest.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/GraphRequest.ts b/src/GraphRequest.ts index eb21c00b9..80a9e2c8c 100644 --- a/src/GraphRequest.ts +++ b/src/GraphRequest.ts @@ -377,9 +377,6 @@ export class GraphRequest { }); rawResponse = context.response; - // if (rawResponse.status >= 400 && rawResponse.status < 500) { - // throw new GraphError(rawResponse.status,) - // } const response: any = await GraphResponseHandler.getResponse(rawResponse, this._responseType, callback); return response; } catch (error) {