Skip to content

Commit becfa51

Browse files
committed
Improve user retrieval error handling
1 parent 5426885 commit becfa51

3 files changed

Lines changed: 15 additions & 5 deletions

File tree

packages/server/api/src/app/authentication/context/create-project-auth-context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export async function getProjectAndToken(
2323
tablesRefreshToken: string;
2424
projectRole: ProjectMemberRole;
2525
}> {
26-
const updatedUser = await userService.getOneOrFail({ id: user.id });
26+
const updatedUser = await userService.getOneOrThrow({ id: user.id });
2727

2828
const project = await projectService.getOneForUser(updatedUser);
2929
if (isNil(project)) {

packages/server/api/src/app/authentication/new-user/organization-assignment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export async function assignDefaultOrganization(user: User): Promise<void> {
3131
organizationId: organization.id,
3232
});
3333

34-
const updatedUser = await userService.getOneOrFail({ id: user.id });
34+
const updatedUser = await userService.getOneOrThrow({ id: user.id });
3535
const project = await projectService.getOneForUser(updatedUser);
3636

3737
if (isNil(project)) {

packages/server/api/src/app/user/user-service.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,18 @@ export const userService = {
101101
async get({ id }: IdParams): Promise<User | null> {
102102
return userRepo().findOneBy({ id });
103103
},
104-
async getOneOrFail({ id }: IdParams): Promise<User> {
105-
return userRepo().findOneByOrFail({ id });
104+
async getOneOrThrow({ id }: IdParams): Promise<User> {
105+
const user = await this.get({ id });
106+
if (isNil(user)) {
107+
throw new ApplicationError({
108+
code: ErrorCode.ENTITY_NOT_FOUND,
109+
params: {
110+
entityId: id,
111+
entityType: 'user',
112+
},
113+
});
114+
}
115+
return user;
106116
},
107117

108118
async getDefaultAdmin(): Promise<User | null> {
@@ -201,7 +211,7 @@ export const userService = {
201211
password: hashedPassword,
202212
});
203213

204-
return userService.getOneOrFail({ id });
214+
return userService.getOneOrThrow({ id });
205215
},
206216

207217
async updateEmail({ id, newEmail }: UpdateEmailParams): Promise<void> {

0 commit comments

Comments
 (0)