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 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..b0fdee2c4 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..80a9e2c8c 100644 --- a/src/GraphRequest.ts +++ b/src/GraphRequest.ts @@ -388,7 +388,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; } } diff --git a/test/common/core/GraphErrorHandler.ts b/test/common/core/GraphErrorHandler.ts index f133953b2..20cda13aa 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: new Headers(headers), + }); + const gError = await GraphErrorHandler.getError(errorResponse, 500, undefined, rawResponse); + assert.isDefined(gError.headers); + assert.equal(gError.headers?.get("keyTest"), headers.keyTest); + }); }); });