Skip to content

Commit

Permalink
Make GraphError real Error (#335)
Browse files Browse the repository at this point in the history
Co-authored-by: nikithauc <[email protected]>
  • Loading branch information
OlivierCuyp and nikithauc authored Oct 5, 2020
1 parent f99d0ff commit ad4399b
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 14 deletions.
2 changes: 1 addition & 1 deletion spec/core/GraphErrorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ describe("GraphErrorHandler.ts", () => {

it("Should construct some default error", async () => {
const gError = await GraphErrorHandler.getError();
assert.equal(gError.message, "");
assert.equal(gError.statusCode, -1);
assert.equal(gError.code, null);
assert.equal(gError.message, null);
assert.equal(gError.body, null);
assert.equal(gError.requestId, null);
});
Expand Down
13 changes: 4 additions & 9 deletions src/GraphError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* Some fields are renamed ie, "request-id" => requestId so you can use dot notation
*/

export class GraphError {
export class GraphError extends Error {
/**
* @public
* A member holding status code of the error
Expand All @@ -30,12 +30,6 @@ export class GraphError {
*/
public code: string | null;

/**
* @public
* A member holding error message
*/
public message: string | null;

/**
* @public
* A member holding request-id i.e identifier of the request
Expand All @@ -61,12 +55,13 @@ export class GraphError {
* @param {number} [statusCode = -1] - The status code of the error
* @returns An instance of GraphError
*/
public constructor(statusCode: number = -1) {
public constructor(statusCode: number = -1, message?: string, baseError?: Error) {
super(message || (baseError && baseError.message));
this.statusCode = statusCode;
this.code = null;
this.message = null;
this.requestId = null;
this.date = new Date();
this.body = null;
this.stack = baseError ? baseError.stack : this.stack;
}
}
6 changes: 2 additions & 4 deletions src/GraphErrorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ export class GraphErrorHandler {
* @returns The GraphError instance
*/
private static constructError(error: Error, statusCode?: number): GraphError {
const gError = new GraphError(statusCode);
const gError = new GraphError(statusCode, "", error);
if (error.name !== undefined) {
gError.code = error.name;
}
gError.body = error.toString();
gError.message = error.message;
gError.date = new Date();
return gError;
}
Expand Down Expand Up @@ -60,9 +59,8 @@ export class GraphErrorHandler {
*/
private static constructErrorFromResponse(error: any, statusCode: number): GraphError {
error = error.error;
const gError = new GraphError(statusCode);
const gError = new GraphError(statusCode, error.message);
gError.code = error.code;
gError.message = error.message;
if (error.innerError !== undefined) {
gError.requestId = error.innerError["request-id"];
gError.date = new Date(error.innerError.date);
Expand Down

0 comments on commit ad4399b

Please sign in to comment.