Skip to content

Commit 260cb2a

Browse files
committed
Break up wrapApiConfigurationError tests
1 parent 31252c2 commit 260cb2a

1 file changed

Lines changed: 43 additions & 45 deletions

File tree

‎src/api-client.test.ts‎

Lines changed: 43 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -111,71 +111,69 @@ test.serial("getGitHubVersion for GHEC-DR", async (t) => {
111111
t.deepEqual({ type: util.GitHubVariant.GHEC_DR }, gheDotcom);
112112
});
113113

114-
test("wrapApiConfigurationError correctly wraps specific configuration errors", (t) => {
115-
// We don't reclassify arbitrary errors
116-
const arbitraryError = new Error("arbitrary error");
117-
let res = api.wrapApiConfigurationError(arbitraryError);
118-
t.is(res, arbitraryError);
119-
120-
// Same goes for arbitrary errors
121-
const configError = new util.ConfigurationError("arbitrary error");
122-
res = api.wrapApiConfigurationError(configError);
123-
t.is(res, configError);
114+
test("wrapApiConfigurationError doesn't wrap errors it isn't supposed to", (t) => {
115+
const unwrappedErrors = [
116+
// We don't reclassify arbitrary errors
117+
new Error("arbitrary error"),
118+
// Same goes for arbitrary strings
119+
"arbitrary error",
120+
// If an HTTP error doesn't contain a specific error message, we don't wrap it.
121+
new util.HTTPError("arbitrary HTTP error", 456),
122+
];
124123

125-
// If an HTTP error doesn't contain a specific error message, we don't
126-
// wrap is an an API error.
127-
const httpError = new util.HTTPError("arbitrary HTTP error", 456);
128-
res = api.wrapApiConfigurationError(httpError);
129-
t.is(res, httpError);
124+
for (const unwrappedError of unwrappedErrors) {
125+
const res = api.wrapApiConfigurationError(unwrappedError);
126+
t.is(
127+
res,
128+
unwrappedError,
129+
`${util.getErrorMessage(unwrappedError)} should not be wrapped by wrapApiConfigurationError`,
130+
);
131+
}
132+
});
130133

134+
test("wrapApiConfigurationError correctly wraps specific configuration errors", (t) => {
131135
// For other HTTP errors, we wrap them as Configuration errors if they contain
132136
// specific error messages.
133137
const httpNotFoundError = new util.HTTPError("commit not found", 404);
134-
res = api.wrapApiConfigurationError(httpNotFoundError);
135-
t.deepEqual(res, new util.ConfigurationError("commit not found"));
136-
137138
const refNotFoundError = new util.HTTPError(
138139
"ref 'refs/heads/jitsi' not found in this repository - https://github.com/proxy/docs.github.com/rest",
139140
404,
140141
);
141-
res = api.wrapApiConfigurationError(refNotFoundError);
142-
t.deepEqual(
143-
res,
144-
new util.ConfigurationError(
145-
"ref 'refs/heads/jitsi' not found in this repository - https://github.com/proxy/docs.github.com/rest",
146-
),
147-
);
148-
149142
const apiRateLimitError = new util.HTTPError(
150143
"API rate limit exceeded for installation",
151144
403,
152145
);
153-
res = api.wrapApiConfigurationError(apiRateLimitError);
154-
t.deepEqual(
155-
res,
156-
new util.ConfigurationError("API rate limit exceeded for installation"),
146+
const resourceNotAccessibleError = new util.HTTPError(
147+
"Resource not accessible by integration",
148+
403,
157149
);
150+
const errorsToWrap = [
151+
httpNotFoundError,
152+
refNotFoundError,
153+
apiRateLimitError,
154+
resourceNotAccessibleError,
155+
];
156+
157+
for (const errorToWrap of errorsToWrap) {
158+
const res = api.wrapApiConfigurationError(errorToWrap);
159+
t.deepEqual(res, new util.ConfigurationError(errorToWrap.message));
160+
}
161+
});
158162

163+
test("wrapApiConfigurationError wraps token errors", async (t) => {
159164
const tokenSuggestionMessage =
160165
"Please check that your token is valid and has the required permissions: contents: read, security-events: write";
161166
const badCredentialsError = new util.HTTPError("Bad credentials", 401);
162-
res = api.wrapApiConfigurationError(badCredentialsError);
163-
t.deepEqual(res, new util.ConfigurationError(tokenSuggestionMessage));
164-
165167
const notFoundError = new util.HTTPError("Not Found", 404);
166-
res = api.wrapApiConfigurationError(notFoundError);
167-
t.deepEqual(res, new util.ConfigurationError(tokenSuggestionMessage));
168+
const errorsToWrap = [badCredentialsError, notFoundError];
168169

169-
const resourceNotAccessibleError = new util.HTTPError(
170-
"Resource not accessible by integration",
171-
403,
172-
);
173-
res = api.wrapApiConfigurationError(resourceNotAccessibleError);
174-
t.deepEqual(
175-
res,
176-
new util.ConfigurationError("Resource not accessible by integration"),
177-
);
170+
for (const errorToWrap of errorsToWrap) {
171+
const res = api.wrapApiConfigurationError(errorToWrap);
172+
t.deepEqual(res, new util.ConfigurationError(tokenSuggestionMessage));
173+
}
174+
});
178175

176+
test("wrapApiConfigurationError wraps enablement errors", async (t) => {
179177
// Enablement errors.
180178
const enablementErrorMessages = [
181179
"Code Security must be enabled for this repository to use code scanning",
@@ -195,7 +193,7 @@ test("wrapApiConfigurationError correctly wraps specific configuration errors",
195193
transform(enablementErrorMessage),
196194
403,
197195
);
198-
res = api.wrapApiConfigurationError(enablementError);
196+
const res = api.wrapApiConfigurationError(enablementError);
199197
t.deepEqual(
200198
res,
201199
new util.ConfigurationError(

0 commit comments

Comments
 (0)