Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dashboard] Add global timeout for Public APIs and better error message #18902

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 50 additions & 6 deletions components/dashboard/src/service/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,61 @@ import { TeamMemberInfo, TeamMemberRole } from "@gitpod/gitpod-protocol";
import { TeamMember, TeamRole } from "@gitpod/public-api/lib/gitpod/experimental/v1/teams_pb";
import { Project } from "@gitpod/public-api/lib/gitpod/experimental/v1/projects_pb";

class WrapError extends Error {
constructor(msg: string, readonly cause: any, readonly code?: string) {
super();

let originalMessage = cause?.message;
if (!originalMessage) {
if (cause instanceof Error) {
originalMessage = cause?.toString();
} else {
try {
originalMessage = JSON.stringify(cause);
} catch {}
}
}
this.message = `${msg}: ${originalMessage}`;

if (cause instanceof Error) {
this.name = cause.name;
this.stack = this.stack + "\n\n" + cause.stack;
}

this.code ??= cause?.code;
}
}

const transport = createConnectTransport({
baseUrl: `${window.location.protocol}//${window.location.host}/public-api`,
interceptors: [getMetricsInterceptor()],
defaultTimeoutMs: 4000,
});

export const helloService = createPromiseClient(HelloService, transport);
export const teamsService = createPromiseClient(TeamsService, transport);
export const personalAccessTokensService = createPromiseClient(TokensService, transport);
export const projectsService = createPromiseClient(ProjectsService, transport);
export const workspacesService = createPromiseClient(WorkspacesService, transport);
export const oidcService = createPromiseClient(OIDCService, transport);
export const helloService = wrapServiceError(createPromiseClient(HelloService, transport));
export const teamsService = wrapServiceError(createPromiseClient(TeamsService, transport));
export const personalAccessTokensService = wrapServiceError(createPromiseClient(TokensService, transport));
export const projectsService = wrapServiceError(createPromiseClient(ProjectsService, transport));
export const workspacesService = wrapServiceError(createPromiseClient(WorkspacesService, transport));
export const oidcService = wrapServiceError(createPromiseClient(OIDCService, transport));

// We use Proxy here instead of add interceptor to transport
// that's because AbortError is out of interceptor, connect-es will force convert error to deadline_exceeded
// @see https://github.com/connectrpc/connect-es/blob/e0bffbab4e75e19fd7eeb9eadabe050941d39e5f/packages/connect/src/protocol/run-call.ts#L174-L181
function wrapServiceError<T extends object>(service: T): T {
return new Proxy(service, {
get: (target, prop) => {
return async (...args: any[]) => {
try {
// @ts-ignore
return await target[prop](...args);
} catch (e) {
throw new WrapError(`failed to call API [${String(prop)}]`, e);
}
};
},
});
}

export function publicApiTeamToProtocol(team: Team): ProtocolTeam {
return {
Expand Down
4 changes: 4 additions & 0 deletions components/server/src/workspace/gitpod-server-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2197,6 +2197,10 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
const user = await this.checkUser("getOrganizations");
const orgs = await this.organizationService.listOrganizationsByMember(user.id, user.id);

const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

await sleep(5000);

const filterOrg = async (org: Organization): Promise<Organization | undefined> => {
const members = await this.organizationService.listMembers(user.id, org.id);
if (!(await this.resourceAccessGuard.canAccess({ kind: "team", subject: org, members }, "get"))) {
Expand Down
Loading