title: How to catch a Typeorm transaction error in NestJs
body: <p>I have a server side written in node.js and nestJs, querying with typeorm.</p>
  
  <p>I'm trying to wrap some query's to the database with transaction as suggested <a href="https://stackoverflow.com/questions/53680665/nestjs-typeorm-database-transaction">here</a> with some changes inspired by typeorm's <a href="https://github.com/typeorm/typeorm/blob/master/docs/transactions.md" rel="nofollow noreferrer">docs</a>, like this:</p>
  
  <pre><code>getManager().transaction(async transactionalEntityManager =&gt; {
      transactionalEntityManager.save&lt;Entity&gt;(newEntity)
      transactionalEntityManager.save&lt;Entity1&gt;(newEntity1)
  });
  </code></pre>
  
  <p>The transaction works well and rollback the database if there was an error.
  tested this way: </p>
  
  <pre><code>getManager().transaction(async transactionalEntityManager =&gt; {
      transactionalEntityManager.save&lt;Entity&gt;(newEntity)
      throw 'There is an error'
      transactionalEntityManager.save&lt;Entity1&gt;(newEntity1)
  });
  </code></pre>
  
  <p>The execution of the transaction is inside a graphQL Mutation, so I should return an error to the client if something went wrong, the problem is that I can't catch the errors from the transaction.</p>
  
  <p>Tried doing this: </p>
  
  <pre><code>  @Mutation(returns =&gt; Entity)
    async create(): Promise&lt;Entity&gt; {
      let entity = null;
      let error = null;
      getManager().transaction(async transactionalEntityManager =&gt; {
        try {
          entity = await transactionalEntityManager.save&lt;Entity&gt;(newEntity)
          await transactionalEntityManager.save&lt;Entity1&gt;(newEntity1);
        } catch (err) {
          error = err
        }
      })
      if (error) {
        return error
      }
      return entity
    }
  </code></pre>
  
  <p>when I throw error I catch it successfully, but when a real error occurs I can <code>console.log()</code> it in the server but it never reaches to return to the client.</p>