Effect CommunityEC
Effect Community17mo ago
9 replies
attila

Improving Error Handling for `effect-sql` with `mysql2` in Sentry

I'm seeing the following error in Sentry coming from effect-sql in one of my projects:
Error: SqlError: Failed to execute statement
    at Execute.onResult (file:///app/node_modules/@effect/sql-mysql2/dist/esm/MysqlClient.js:49:32)
    at Execute.execute (/app/node_modules/mysql2/lib/commands/command.js:36:14)

The underlying error comes from mysql2 which would normally contain more information like which sql query failed and how exactly it failed. I'd be interested to see a more useful error message because these errors are not very common and not easily reproducible. I can see that the effect mysql2 wrapper already provides the cause to the error here: https://github.com/Effect-TS/effect/blob/main/packages/sql-mysql2/src/MysqlClient.ts#L105
This is how I'm handling errors at my system boundary.

if (Exit.isFailure(exit)) {
  const cause = Cause.originalError(exit.cause);
  // const cause: Cause.Cause<E | SqlError | ShardAccess.client.ShardNotFoundError>

  if (Cause.isFailType(cause) && cause.error instanceof Error) {
    throw cause.error;
  }

  throw new Error(Cause.pretty(cause));
}


Is this wrong?
Was this page helpful?