Skip to content

Commit

Permalink
feat: improve errors, fixes #250
Browse files Browse the repository at this point in the history
  • Loading branch information
shyim authored and pumpi committed Mar 29, 2024
1 parent 8423dbc commit c843896
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
25 changes: 18 additions & 7 deletions api/src/repository/organization.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Drizzle, getLastInsertId, schema } from "../db";
import { eq, and, inArray } from "drizzle-orm";
import Users from "./users";
import { TRPCError } from "@trpc/server";

interface OrganizationMember {
id: number;
Expand Down Expand Up @@ -60,16 +61,26 @@ async function addMember(
const exists = await Users.existsByEmail(con, email);

if (exists === null) {
throw new Error("User not found");
throw new TRPCError({
message: "User not found",
code: "NOT_FOUND",
})
}

await con
.insert(schema.userToOrganization)
.values({
organizationId,
userId: exists,
try {
await con
.insert(schema.userToOrganization)
.values({
organizationId,
userId: exists,
})
.execute();
} catch (e) {
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'User already in organization',
})
.execute();
}
}

async function removeMember(
Expand Down
2 changes: 1 addition & 1 deletion api/src/trpc/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const organizationAdminMiddleware = experimental_standaloneMiddleware<{
.get();

if (!result || result.ownerId !== ctx.user) {
throw new TRPCError({ code: 'NOT_FOUND' });
throw new TRPCError({ code: 'NOT_FOUND', message: 'Your are not the owner of the organization' });
}

return next();
Expand Down

0 comments on commit c843896

Please sign in to comment.