Skip to content

Commit

Permalink
Use ExposableError for public errors in GitLab integration (#625)
Browse files Browse the repository at this point in the history
* Use ExposableError for public errors in GitLab integration

* Add changeset
  • Loading branch information
jpreynat authored Nov 20, 2024
1 parent d5041cc commit 9bce519
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/purple-foxes-arrive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@gitbook/integration-gitlab': minor
---

Use ExposableError for public errors in GitLab integration
10 changes: 6 additions & 4 deletions integrations/gitlab/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import LinkHeader from 'http-link-header';
import { StatusError } from 'itty-router';

import { Logger } from '@gitbook/runtime';
import { Logger, ExposableError } from '@gitbook/runtime';

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

Expand Down Expand Up @@ -275,7 +274,7 @@ async function requestGitLab(

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

throw new StatusError(response.status, `GitLab API error: ${response.statusText}`);
throw new ExposableError(`GitLab API error: ${response.statusText}`, response.status);
}

return response;
Expand All @@ -296,7 +295,10 @@ function getEndpoint(config: GitLabSpaceConfiguration): string {
export function getAccessTokenOrThrow(config: GitLabSpaceConfiguration): string {
const { accessToken } = config;
if (!accessToken) {
throw new StatusError(401, 'Unauthorized: kindly re-authenticate with a new access token.');
throw new ExposableError(
'Unauthorized: kindly re-authenticate with a new access token.',
401,
);
}

return accessToken;
Expand Down
17 changes: 11 additions & 6 deletions integrations/gitlab/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { StatusError, error } from 'itty-router';
import { Router } from 'itty-router';
import { Router, error } from 'itty-router';

import { ContentKitIcon, ContentKitSelectOption, GitSyncOperationState } from '@gitbook/api';
import { createIntegration, FetchEventCallback, Logger, EventCallback } from '@gitbook/runtime';
import {
createIntegration,
FetchEventCallback,
Logger,
EventCallback,
ExposableError,
} from '@gitbook/runtime';

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

const { task } = JSON.parse(payloadString) as { task: IntegrationTask };
Expand Down Expand Up @@ -110,11 +115,11 @@ const handleFetchEvent: FetchEventCallback<GitLabRuntimeContext> = async (reques
if (!valid) {
const message = `Invalid signature for webhook event ${eventUuid}`;
logger.error(message);
throw new StatusError(400, message);
throw new ExposableError(message);
}
} catch (error: any) {
logger.error(`Error verifying signature ${error}`);
throw new StatusError(400, error.message);
throw new ExposableError(error.message);
}
}

Expand Down
6 changes: 2 additions & 4 deletions integrations/gitlab/src/installation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { StatusError } from 'itty-router';

import { IntegrationSpaceInstallation } from '@gitbook/api';
import { Logger } from '@gitbook/runtime';
import { Logger, ExposableError } from '@gitbook/runtime';

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

if (!state.project || !state.branch) {
throw new StatusError(400, 'Incomplete configuration: missing project or branch');
throw new ExposableError('Incomplete configuration: missing project or branch');
}

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

0 comments on commit 9bce519

Please sign in to comment.