-
Notifications
You must be signed in to change notification settings - Fork 236
Expand file tree
/
Copy pathGraphErrorHandler.ts
More file actions
131 lines (119 loc) · 4.37 KB
/
GraphErrorHandler.ts
File metadata and controls
131 lines (119 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* -------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/
import { assert } from "chai";
import { GraphError } from "../../../src";
import { GraphErrorHandler } from "../../../src/GraphErrorHandler";
describe("GraphErrorHandler.ts", () => {
describe("constructError", () => {
it("Should return custom error without code", () => {
const message = "test";
const error = new Error(message);
const gError = GraphErrorHandler["constructError"](error);
assert.equal(gError.message, message);
});
it("Should return custom error with code and message", () => {
const message = "test";
const name = "test_name";
const error = new Error(message);
error.name = name;
const gError = GraphErrorHandler["constructError"](error);
assert.equal(gError.message, message);
assert.equal(gError.code, name);
});
});
describe("constructErrorFromResponse", () => {
const statusCode = 400;
const error: any = {
error: {
code: "DataNotAvailable",
message: "The data that is being requested is not present",
},
};
it("Should construct error for error response without innerError property", () => {
const gError = GraphErrorHandler["constructErrorFromResponse"](error, statusCode);
assert.isTrue(gError instanceof GraphError);
assert.equal(gError.statusCode, statusCode);
assert.equal(gError.requestId, null);
});
it("Should construct error for the response missing one or more properties in innerError property", () => {
error.error.innerError = {
"request-id": "some random id",
};
const gError = GraphErrorHandler["constructErrorFromResponse"](error, statusCode);
assert.isTrue(gError instanceof GraphError);
assert.equal(gError.statusCode, statusCode);
assert.equal(gError.requestId, "some random id");
});
it("Should construct error for the complete error response", () => {
const date = new Date();
const requestId = "some random id";
error.error.innerError = {
"request-id": requestId,
date,
};
const gError = GraphErrorHandler["constructErrorFromResponse"](error, statusCode);
assert.isTrue(gError instanceof GraphError);
assert.equal(gError.statusCode, statusCode);
assert.equal(gError.requestId, "some random id");
assert.equal(gError.date.toUTCString(), date.toUTCString());
});
});
describe("getError", async () => {
it("Should construct error from response", async () => {
const errorResponse = {
error: {
code: "500",
message: "Internal Server Error",
innerError: {
"request-id": "some random id",
},
},
};
const gError = await GraphErrorHandler.getError(errorResponse);
assert.isTrue(gError instanceof GraphError);
assert.equal(gError.requestId, "some random id");
assert.equal(gError.code, "500");
assert.equal(gError.message, "Internal Server Error");
});
it("Should construct error from error object", async () => {
const error = new Error("Some Error");
error.name = "InvalidError";
const gError = await GraphErrorHandler.getError(error);
assert.isTrue(gError instanceof GraphError);
assert.equal(gError.requestId, null);
assert.equal(gError.message, "Some Error");
assert.equal(gError.code, "InvalidError");
});
it("Should construct some default error", async () => {
const gError = await GraphErrorHandler.getError();
assert.isTrue(gError instanceof GraphError);
assert.equal(gError.message, "");
assert.equal(gError.statusCode, -1);
assert.equal(gError.code, null);
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);
});
});
});