Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions shims.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ interface NodeStream {
read(size?: number): any;
on(event: string | symbol, listener: (...args: any[]) => void): this;
}

interface Headers{}
2 changes: 2 additions & 0 deletions src/GraphError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions src/GraphErrorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
Expand All @@ -81,6 +82,7 @@ export class GraphErrorHandler {
}

gError.body = JSON.stringify(error);
gError.headers = rawResponse?.headers;

return gError;
}
Expand All @@ -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<GraphError> {
public static async getError(error: any = null, statusCode = -1, callback?: GraphRequestCallback, rawResponse?: Response): Promise<GraphError> {
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
Expand Down
2 changes: 1 addition & 1 deletion src/GraphRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
19 changes: 19 additions & 0 deletions test/common/core/GraphErrorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});