Skip to content

Understanding the Purpose of ChainedError in transaction Method in src/connections/AbstractConnection.ts #115

@kir4ik

Description

@kir4ik

Hello!

I'm having trouble understanding the purpose of ChainedError in the src/connections/AbstractConnection.ts file, specifically in the transaction method.

In my situation, my services can throw various errors within a transaction, including validation errors, and I would like to get the original errors immediately. However, I'm getting something like this:

ChainedError: [object Object]
at ...
Caused By: undefined
Transaction source: Error: Transaction executed at
at DBConnection.transaction
...
[cause]: ValidationErrors { fields: Map(1) { 'code' => [ { message: 'Invalid coupon code' } ] } }

I had to override the transaction method in DBConnection to avoid adding try-catch blocks everywhere:

class DBConnection extends MySqlConnection<'Connection'> {
  allowEmptyString = true;
  protected compatibilityMode = true;

  transaction<P extends Promise<any>[]>(fn: () => [...P]): Promise<UnwrapPromiseTuple<P>>;
  transaction<T>(fn: () => Promise<T>): Promise<T>;
  async transaction(fn: any): Promise<any> {
    try {
      return await super.transaction(fn);
    } catch (error) {
      if (error.cause) {
        throw error.cause;
      }

      throw error;
    }
  }
}

My question is, what is the purpose of ChainedError, and is it possible to work without it?

Is there any specific reason for using ChainedError in this context, or can it be removed (can avoid wrapping errors and simply return them as they are)?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions