© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•2y ago
kal

How to properly handle/distinguish between errors

I'm using Drizzle ORM with a postgres.

I'd like to distinguish between certain errors that occur when inserting / querying the database.

In my current case, I would like to know specifically when my insertion fails as a result of a user not existing in the database.

(See the comments in the catch block in the following example)

  /**
   *
   * @param targetID  the ID of the user to receive the friend request
   */
  async sendUserFriendRequest(targetID: string): Promise<boolean> {
    try {
      await db
        .insert(friends)
        .values({
          user1_ID: this.userID,
          user2_ID: targetID,
          status: "pending",
        })
        .onConflictDoNothing({ target: [friends.user1_ID, friends.user2_ID] });

      log("social").debug(
        `user ${this.userID} sent friend request to user ${targetID}`,
      );
    } catch (e) {
      // if e is caused by targetID user not existing
      // return false
      // else
      log("social").error(
        `user '${this.userID}' failed to send friend request to user '${targetID}' %o`,
        e,
      );
      return false;
    }

    return true;
  }
  /**
   *
   * @param targetID  the ID of the user to receive the friend request
   */
  async sendUserFriendRequest(targetID: string): Promise<boolean> {
    try {
      await db
        .insert(friends)
        .values({
          user1_ID: this.userID,
          user2_ID: targetID,
          status: "pending",
        })
        .onConflictDoNothing({ target: [friends.user1_ID, friends.user2_ID] });

      log("social").debug(
        `user ${this.userID} sent friend request to user ${targetID}`,
      );
    } catch (e) {
      // if e is caused by targetID user not existing
      // return false
      // else
      log("social").error(
        `user '${this.userID}' failed to send friend request to user '${targetID}' %o`,
        e,
      );
      return false;
    }

    return true;
  }


The error I get when a user does not exist is like so:
{
[message]: 'insert or update on table "friends" violates foreign key constraint "friends_user2_user_id_fk"',
  length: 256,
  name: 'error',
  severity: 'ERROR',
  code: '23503',
  detail: 'Key (user2)=(hi) is not present in table "user".',
  hint: undefined,
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: 'public',
  table: 'friends',
  column: undefined,
  dataType: undefined,
  constraint: 'friends_user2_user_id_fk',
  file: 'ri_triggers.c',
  line: '2619',
  routine: 'ri_ReportViolation'
}
{
[message]: 'insert or update on table "friends" violates foreign key constraint "friends_user2_user_id_fk"',
  length: 256,
  name: 'error',
  severity: 'ERROR',
  code: '23503',
  detail: 'Key (user2)=(hi) is not present in table "user".',
  hint: undefined,
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: 'public',
  table: 'friends',
  column: undefined,
  dataType: undefined,
  constraint: 'friends_user2_user_id_fk',
  file: 'ri_triggers.c',
  line: '2619',
  routine: 'ri_ReportViolation'
}


What is the proper / best way to differentiate between causes of errors while using drizzle?
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Drizzle / PlanetScale : (How to properly handle replicas)
Drizzle TeamDTDrizzle Team / help
2y ago
How to handle partitions?
Drizzle TeamDTDrizzle Team / help
3y ago
Drizzle studio doesn't handle timestamps properly
Drizzle TeamDTDrizzle Team / help
2y ago
What is the best way to handle Query Errors?
Drizzle TeamDTDrizzle Team / help
9mo ago