-
Notifications
You must be signed in to change notification settings - Fork 228
Expand file tree
/
Copy pathcustom-errors.test.ts
More file actions
183 lines (169 loc) · 6.4 KB
/
custom-errors.test.ts
File metadata and controls
183 lines (169 loc) · 6.4 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import { RequestError } from "@octokit/request-error";
import { createMockLogger } from "../../__mocks__/loggerMock";
import { handleRequestError } from "../../../src/variant-analysis/custom-errors";
import { faker } from "@faker-js/faker";
describe("handleRequestError", () => {
const githubUrl = new URL("https://github.com");
const logger = createMockLogger();
it("returns false when handling a non-422 error", () => {
const e = mockRequestError(404, {
message: "Not Found",
});
expect(handleRequestError(e, githubUrl, logger)).toBe(false);
expect(logger.showErrorMessage).not.toHaveBeenCalled();
});
it("returns false when handling a different error without errors", () => {
const e = mockRequestError(422, {
message:
"Unable to trigger a variant analysis. None of the requested repositories could be found.",
});
expect(handleRequestError(e, githubUrl, logger)).toBe(false);
expect(logger.showErrorMessage).not.toHaveBeenCalled();
});
it("returns false when handling an error without response body", () => {
const e = mockRequestError(422, undefined);
expect(handleRequestError(e, githubUrl, logger)).toBe(false);
expect(logger.showErrorMessage).not.toHaveBeenCalled();
});
it("returns false when handling an error without response", () => {
const e = new RequestError("Timeout", 500, {
request: {
method: "POST",
url: faker.internet.url(),
headers: {
"Content-Type": "application/json",
},
},
});
expect(handleRequestError(e, githubUrl, logger)).toBe(false);
expect(logger.showErrorMessage).not.toHaveBeenCalled();
});
it("returns false when handling a different error with errors", () => {
const e = mockRequestError(422, {
message:
"Unable to trigger a variant analysis. None of the requested repositories could be found.",
errors: [
{
resource: "VariantAnalysis",
field: "repositories",
code: "not_found",
},
],
});
expect(handleRequestError(e, githubUrl, logger)).toBe(false);
expect(logger.showErrorMessage).not.toHaveBeenCalled();
});
it("returns false when handling without repository field", () => {
const e = mockRequestError(422, {
message:
"Variant analysis failed because controller repository github/pickles does not have a branch 'main'. Please create a 'main' branch in the repository and re-run the variant analysis.",
errors: [
{
resource: "Repository",
field: "default_branch",
code: "missing",
default_branch: "main",
},
],
});
expect(handleRequestError(e, githubUrl, logger)).toBe(false);
expect(logger.showErrorMessage).not.toHaveBeenCalled();
});
it("returns false when handling without default_branch field", () => {
const e = mockRequestError(422, {
message:
"Variant analysis failed because controller repository github/pickles does not have a branch 'main'. Please create a 'main' branch in the repository and re-run the variant analysis.",
errors: [
{
resource: "Repository",
field: "default_branch",
code: "missing",
repository: "github/pickles",
},
],
});
expect(handleRequestError(e, githubUrl, logger)).toBe(false);
expect(logger.showErrorMessage).not.toHaveBeenCalled();
});
it("shows notification when handling a missing default branch error with github.com URL", () => {
const e = mockRequestError(422, {
message:
"Variant analysis failed because controller repository github/pickles does not have a branch 'main'. Please create a 'main' branch in the repository and re-run the variant analysis.",
errors: [
{
resource: "Repository",
field: "default_branch",
code: "missing",
repository: "github/pickles",
default_branch: "main",
},
],
});
expect(handleRequestError(e, githubUrl, logger)).toBe(true);
expect(logger.showErrorMessage).toHaveBeenCalledWith(
"Variant analysis failed because the controller repository github/pickles does not have a branch 'main'. Please create a 'main' branch by clicking [here](https://github.com/github/pickles/new/main) and re-run the variant analysis query.",
);
});
it("shows notification when handling a missing default branch error with GHEC-DR URL", () => {
const e = mockRequestError(422, {
message:
"Variant analysis failed because controller repository github/pickles does not have a branch 'main'. Please create a 'main' branch in the repository and re-run the variant analysis.",
errors: [
{
resource: "Repository",
field: "default_branch",
code: "missing",
repository: "github/pickles",
default_branch: "main",
},
],
});
expect(
handleRequestError(e, new URL("https://tenant.ghe.com"), logger),
).toBe(true);
expect(logger.showErrorMessage).toHaveBeenCalledWith(
"Variant analysis failed because the controller repository github/pickles does not have a branch 'main'. Please create a 'main' branch by clicking [here](https://tenant.ghe.com/github/pickles/new/main) and re-run the variant analysis query.",
);
});
});
function mockRequestError(status: number, body: any): RequestError {
return new RequestError(
body ? toErrorMessage(body) : "Unknown error",
status,
{
request: {
method: "POST",
url: faker.internet.url(),
headers: {
"Content-Type": "application/json",
},
},
response: {
url: faker.internet.url(),
status,
headers: {
"Content-Type": "application/json",
},
data: body,
retryCount: 0,
},
},
);
}
// Copied from https://github.com/octokit/request.js/blob/c67f902350384846f88d91196e7066daadc08357/src/fetch-wrapper.ts#L166 to have a
// somewhat realistic error message
function toErrorMessage(data: any) {
if (typeof data === "string") {
return data;
}
if ("message" in data) {
if (Array.isArray(data.errors)) {
return `${data.message}: ${data.errors.map(JSON.stringify).join(", ")}`;
}
if (typeof data.message === "string") {
return data.message as string;
}
}
// istanbul ignore next - just in case
return `Unknown error: ${JSON.stringify(data)}`;
}