Skip to content

Commit 9bce519

Browse files
authored
Use ExposableError for public errors in GitLab integration (#625)
* Use ExposableError for public errors in GitLab integration * Add changeset
1 parent d5041cc commit 9bce519

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed

.changeset/purple-foxes-arrive.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@gitbook/integration-gitlab': minor
3+
---
4+
5+
Use ExposableError for public errors in GitLab integration

integrations/gitlab/src/api.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import LinkHeader from 'http-link-header';
2-
import { StatusError } from 'itty-router';
32

4-
import { Logger } from '@gitbook/runtime';
3+
import { Logger, ExposableError } from '@gitbook/runtime';
54

65
import type { GitLabSpaceConfiguration } from './types';
76

@@ -275,7 +274,7 @@ async function requestGitLab(
275274

276275
logger.error(`[${options.method}] (${response.status}) GitLab API error: ${text}`);
277276

278-
throw new StatusError(response.status, `GitLab API error: ${response.statusText}`);
277+
throw new ExposableError(`GitLab API error: ${response.statusText}`, response.status);
279278
}
280279

281280
return response;
@@ -296,7 +295,10 @@ function getEndpoint(config: GitLabSpaceConfiguration): string {
296295
export function getAccessTokenOrThrow(config: GitLabSpaceConfiguration): string {
297296
const { accessToken } = config;
298297
if (!accessToken) {
299-
throw new StatusError(401, 'Unauthorized: kindly re-authenticate with a new access token.');
298+
throw new ExposableError(
299+
'Unauthorized: kindly re-authenticate with a new access token.',
300+
401,
301+
);
300302
}
301303

302304
return accessToken;

integrations/gitlab/src/index.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
import { StatusError, error } from 'itty-router';
2-
import { Router } from 'itty-router';
1+
import { Router, error } from 'itty-router';
32

43
import { ContentKitIcon, ContentKitSelectOption, GitSyncOperationState } from '@gitbook/api';
5-
import { createIntegration, FetchEventCallback, Logger, EventCallback } from '@gitbook/runtime';
4+
import {
5+
createIntegration,
6+
FetchEventCallback,
7+
Logger,
8+
EventCallback,
9+
ExposableError,
10+
} from '@gitbook/runtime';
611

712
import { fetchProject, fetchProjectBranches, fetchProjects, searchUserProjects } from './api';
813
import { configBlock } from './components';
@@ -70,7 +75,7 @@ const handleFetchEvent: FetchEventCallback<GitLabRuntimeContext> = async (reques
7075
if (!verified) {
7176
const message = `Invalid signature for integration task`;
7277
logger.error(message);
73-
throw new StatusError(400, message);
78+
throw new ExposableError(message);
7479
}
7580

7681
const { task } = JSON.parse(payloadString) as { task: IntegrationTask };
@@ -110,11 +115,11 @@ const handleFetchEvent: FetchEventCallback<GitLabRuntimeContext> = async (reques
110115
if (!valid) {
111116
const message = `Invalid signature for webhook event ${eventUuid}`;
112117
logger.error(message);
113-
throw new StatusError(400, message);
118+
throw new ExposableError(message);
114119
}
115120
} catch (error: any) {
116121
logger.error(`Error verifying signature ${error}`);
117-
throw new StatusError(400, error.message);
122+
throw new ExposableError(error.message);
118123
}
119124
}
120125

integrations/gitlab/src/installation.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { StatusError } from 'itty-router';
2-
31
import { IntegrationSpaceInstallation } from '@gitbook/api';
4-
import { Logger } from '@gitbook/runtime';
2+
import { Logger, ExposableError } from '@gitbook/runtime';
53

64
import { fetchProject } from './api';
75
import { createGitLabWebhookURL, installWebhook } from './provider';
@@ -30,7 +28,7 @@ export async function saveSpaceConfiguration(
3028
assertIsDefined(spaceInstallation, { label: 'spaceInstallation' });
3129

3230
if (!state.project || !state.branch) {
33-
throw new StatusError(400, 'Incomplete configuration: missing project or branch');
31+
throw new ExposableError('Incomplete configuration: missing project or branch');
3432
}
3533

3634
const projectId = parseInt(state.project, 10);

0 commit comments

Comments
 (0)