Skip to content

Commit 4519f25

Browse files
authored
Add header property to Graph error object (#1133)
* headers prop in GraphError * adding test * changing the headers type * remove commented code
1 parent 4631c7c commit 4519f25

File tree

5 files changed

+31
-6
lines changed

5 files changed

+31
-6
lines changed

shims.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ interface NodeStream {
1919
read(size?: number): any;
2020
on(event: string | symbol, listener: (...args: any[]) => void): this;
2121
}
22+
23+
interface Headers{}

src/GraphError.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ export class GraphError extends Error {
4242
*/
4343
public date: Date;
4444

45+
public headers?: Headers;
46+
4547
/**
4648
* @public
4749
* A member holding original error response by the graph service

src/GraphErrorHandler.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,14 @@ export class GraphErrorHandler {
4040
* @param {number} [statusCode] - The status code of the response
4141
* @returns The GraphError instance
4242
*/
43-
private static constructError(error: Error, statusCode?: number): GraphError {
43+
private static constructError(error: Error, statusCode?: number, rawResponse?: Response): GraphError {
4444
const gError = new GraphError(statusCode, "", error);
4545
if (error.name !== undefined) {
4646
gError.code = error.name;
4747
}
4848
gError.body = error.toString();
4949
gError.date = new Date();
50+
gError.headers = rawResponse?.headers;
5051
return gError;
5152
}
5253

@@ -71,7 +72,7 @@ export class GraphErrorHandler {
7172
* }
7273
* }
7374
*/
74-
private static constructErrorFromResponse(graphError: GraphAPIErrorResponse, statusCode: number): GraphError {
75+
private static constructErrorFromResponse(graphError: GraphAPIErrorResponse, statusCode: number, rawResponse?: Response): GraphError {
7576
const error = graphError.error;
7677
const gError = new GraphError(statusCode, error.message);
7778
gError.code = error.code;
@@ -81,6 +82,7 @@ export class GraphErrorHandler {
8182
}
8283

8384
gError.body = JSON.stringify(error);
85+
gError.headers = rawResponse?.headers;
8486

8587
return gError;
8688
}
@@ -96,12 +98,12 @@ export class GraphErrorHandler {
9698
* @param {GraphRequestCallback} [callback] - The graph request callback function
9799
* @returns A promise that resolves to GraphError instance
98100
*/
99-
public static async getError(error: any = null, statusCode = -1, callback?: GraphRequestCallback): Promise<GraphError> {
101+
public static async getError(error: any = null, statusCode = -1, callback?: GraphRequestCallback, rawResponse?: Response): Promise<GraphError> {
100102
let gError: GraphError;
101103
if (error && error.error) {
102-
gError = GraphErrorHandler.constructErrorFromResponse(error, statusCode);
104+
gError = GraphErrorHandler.constructErrorFromResponse(error, statusCode, rawResponse);
103105
} else if (error instanceof Error) {
104-
gError = GraphErrorHandler.constructError(error, statusCode);
106+
gError = GraphErrorHandler.constructError(error, statusCode, rawResponse);
105107
} else {
106108
gError = new GraphError(statusCode);
107109
gError.body = error; // if a custom error is passed which is not instance of Error object or a graph API response

src/GraphRequest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ export class GraphRequest {
388388
if (rawResponse) {
389389
statusCode = rawResponse.status;
390390
}
391-
const gError: GraphError = await GraphErrorHandler.getError(error, statusCode, callback);
391+
const gError: GraphError = await GraphErrorHandler.getError(error, statusCode, callback, rawResponse);
392392
throw gError;
393393
}
394394
}

test/common/core/GraphErrorHandler.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,24 @@ describe("GraphErrorHandler.ts", () => {
108108
assert.equal(gError.body, null);
109109
assert.equal(gError.requestId, null);
110110
});
111+
112+
it("Should get header from response", async () => {
113+
const headers = { keyTest: "valueTest" };
114+
const errorResponse = {
115+
error: {
116+
code: "500",
117+
message: "Internal Server Error",
118+
innerError: {
119+
"request-id": "some random id",
120+
},
121+
},
122+
};
123+
const rawResponse = new Response(undefined, {
124+
headers: new Headers(headers),
125+
});
126+
const gError = await GraphErrorHandler.getError(errorResponse, 500, undefined, rawResponse);
127+
assert.isDefined(gError.headers);
128+
assert.equal(gError.headers?.get("keyTest"), headers.keyTest);
129+
});
111130
});
112131
});

0 commit comments

Comments
 (0)