Skip to content

Commit 746e0b2

Browse files
committed
[server] don't error on project not_found
1 parent 5a7b346 commit 746e0b2

File tree

5 files changed

+24
-21
lines changed

5 files changed

+24
-21
lines changed

components/gitpod-protocol/src/messaging/error.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ export namespace ApplicationError {
2525
export function hasErrorCode(e: any): e is Error & { code: ErrorCode; data?: any } {
2626
return e && e.code !== undefined;
2727
}
28+
29+
export async function notFoundToUndefined<T>(p: Promise<T>): Promise<T | undefined> {
30+
try {
31+
return await p;
32+
} catch (e) {
33+
if (hasErrorCode(e) && e.code === ErrorCodes.NOT_FOUND) {
34+
return undefined;
35+
}
36+
throw e;
37+
}
38+
}
2839
}
2940

3041
export namespace ErrorCode {

components/server/src/workspace/env-var-service.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
} from "@gitpod/gitpod-protocol";
1616
import { inject, injectable } from "inversify";
1717
import { ProjectsService } from "../projects/projects-service";
18+
import { ApplicationError } from "@gitpod/gitpod-protocol/lib/messaging/error";
1819

1920
export interface ResolvedEnvVars {
2021
// all project env vars, censored included always
@@ -43,8 +44,11 @@ export class EnvVarService {
4344
};
4445

4546
const projectEnvVars = workspace.projectId
46-
? await this.projectsService.getProjectEnvironmentVariables(workspace.ownerId, workspace.projectId)
47+
? (await ApplicationError.notFoundToUndefined(
48+
this.projectsService.getProjectEnvironmentVariables(workspace.ownerId, workspace.projectId),
49+
)) || []
4750
: [];
51+
4852
if (workspace.type === "prebuild") {
4953
// prebuild does not have access to user env vars and cannot be started via prewfix URL
5054
const withValues = await this.projectDB.getProjectEnvironmentVariableValues(projectEnvVars);

components/server/src/workspace/gitpod-server-impl.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
848848
const user = await this.checkUser("getTeamMembersByProject");
849849
if (projectId) {
850850
const project = await this.projectsService.getProject(user.id, projectId);
851-
if (project && project.teamId) {
851+
if (project.teamId) {
852852
return await this.organizationService.listMembers(user.id, project.teamId);
853853
}
854854
}
@@ -978,7 +978,7 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
978978
}
979979
const envVarsPromise = this.envVarService.resolve(workspace);
980980
const projectPromise = workspace.projectId
981-
? this.projectsService.getProject(user.id, workspace.projectId)
981+
? ApplicationError.notFoundToUndefined(this.projectsService.getProject(user.id, workspace.projectId))
982982
: Promise.resolve(undefined);
983983

984984
await mayStartPromise;
@@ -1593,9 +1593,6 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
15931593
const user = await this.checkAndBlockUser("getPrebuildEvents");
15941594

15951595
const project = await this.projectsService.getProject(user.id, projectId);
1596-
if (!project) {
1597-
throw new ApplicationError(ErrorCodes.NOT_FOUND, "Project not found");
1598-
}
15991596
await this.guardProjectOperation(user, projectId, "get");
16001597

16011598
const events = await this.projectsService.getPrebuildEvents(user.id, project.cloneUrl);
@@ -1612,9 +1609,6 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
16121609
const user = await this.checkAndBlockUser("triggerPrebuild");
16131610

16141611
const project = await this.projectsService.getProject(user.id, projectId);
1615-
if (!project) {
1616-
throw new ApplicationError(ErrorCodes.NOT_FOUND, "Project not found");
1617-
}
16181612
await this.guardProjectOperation(user, projectId, "update");
16191613

16201614
const branchDetails = !!branchName
@@ -2810,9 +2804,6 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
28102804

28112805
private async guardProjectOperation(user: User, projectId: string, op: ResourceAccessOp): Promise<void> {
28122806
const project = await this.projectsService.getProject(user.id, projectId);
2813-
if (!project) {
2814-
throw new ApplicationError(ErrorCodes.NOT_FOUND, "Project not found");
2815-
}
28162807
// Anyone who can read a team's information (i.e. any team member) can manage team projects
28172808
await this.guardTeamOperation(project.teamId, "get", "not_implemented");
28182809
}
@@ -2954,10 +2945,7 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
29542945

29552946
const user = await this.checkAndBlockUser("cancelPrebuild");
29562947

2957-
const project = await this.projectsService.getProject(user.id, projectId);
2958-
if (!project) {
2959-
throw new ApplicationError(ErrorCodes.NOT_FOUND, "Project not found");
2960-
}
2948+
await this.projectsService.getProject(user.id, projectId);
29612949
await this.guardProjectOperation(user, projectId, "update");
29622950

29632951
const prebuild = await this.workspaceDb.trace(ctx).findPrebuildByID(prebuildId);

components/server/src/workspace/headless-log-controller.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { BearerAuth } from "../auth/bearer-authenticator";
3434
import { ProjectsService } from "../projects/projects-service";
3535
import { HostContextProvider } from "../auth/host-context-provider";
3636
import { TraceContext } from "@gitpod/gitpod-protocol/lib/util/tracing";
37+
import { ApplicationError } from "@gitpod/gitpod-protocol/lib/messaging/error";
3738

3839
export const HEADLESS_LOGS_PATH_PREFIX = "/headless-logs";
3940
export const HEADLESS_LOG_DOWNLOAD_PATH_PREFIX = "/headless-log-download";
@@ -214,7 +215,9 @@ export class HeadlessLogController {
214215

215216
let teamMembers: TeamMemberInfo[] = [];
216217
if (workspace?.projectId) {
217-
const p = await this.projectService.getProject(user.id, workspace.projectId);
218+
const p = await ApplicationError.notFoundToUndefined(
219+
this.projectService.getProject(user.id, workspace.projectId),
220+
);
218221
if (p?.teamId) {
219222
teamMembers = await this.teamDb.findMembersByTeam(p.teamId);
220223
}

components/server/src/workspace/workspace-starter.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -696,10 +696,7 @@ export class WorkspaceStarter {
696696
metadata.setMetaId(workspace.id);
697697
if (workspace.projectId) {
698698
metadata.setProject(workspace.projectId);
699-
const project = await this.projectDB.findProjectById(workspace.projectId);
700-
if (project && project.teamId) {
701-
metadata.setTeam(project.teamId);
702-
}
699+
metadata.setTeam(workspace.organizationId);
703700
}
704701

705702
return metadata;

0 commit comments

Comments
 (0)