Jorge Bo
04/06/2023, 2:06 PMpost {
var externalKeyValue = null
effect{
     val keyValue = requestHeaderCaching<String>(IDEMPOTENCY_KEY_HEADER).bind()
     externalKeyValue = keyValue
    newSuspendeTransaction {
       idempotencyKeyService.insertRecord(record).bind()
    }
}.fold(
    {
        println("Handle exception thrown by the code")
        if (externalKeyValue != null) {
            newSuspendedTransaction {
                idempotencyKeyService.unlockKey(idempotencyKey = externalKeyValue!!).bind() //CANNOT BIND HERE!!
            }
        }
        throw it
    },
    {
        println("BUSINESS ERROR")
        handleLeft(it)
    },
    {
        call.respond(HttpStatusCode.Created)
    }
)
}simon.vergauwen
04/06/2023, 2:34 PMexception handler into the effect as well.
post {
var externalKeyValue = null
effect{
    val keyValue = requestHeaderCaching<String>(IDEMPOTENCY_KEY_HEADER).bind()
    externalKeyValue = keyValue
    catch({
      newSuspendeTransaction {
         idempotencyKeyService.insertRecord(record).bind()
      }
    )} { 
        println("Handle exception thrown by the code")
        if (externalKeyValue != null) {
            newSuspendedTransaction {
                idempotencyKeyService.unlockKey(idempotencyKey = externalKeyValue!!).bind()
            }
        }
        throw it
    }
}.fold(
    {
        println("BUSINESS ERROR")
        handleLeft(it)
    },
    {
        call.respond(HttpStatusCode.Created)
    }
)
}simon.vergauwen
04/06/2023, 2:36 PMpost {
var externalKeyValue = null
recover({
    val keyValue = requestHeaderCaching<String>(IDEMPOTENCY_KEY_HEADER).bind()
    externalKeyValue = keyValue
    catch({
      newSuspendeTransaction {
         idempotencyKeyService.insertRecord(record).bind()
      }
    )} { 
        println("Handle exception thrown by the code")
        if (externalKeyValue != null) {
            newSuspendedTransaction {
                idempotencyKeyService.unlockKey(idempotencyKey = externalKeyValue!!).bind()
            }
        }
        throw it
    }
    call.respond(HttpStatusCode.Created)
}) {
  println("BUSINESS ERROR")
  handleLeft(it)
}
}simon.vergauwen
04/06/2023, 2:37 PMJorge Bo
04/06/2023, 2:39 PMJorge Bo
04/11/2023, 1:19 PM.mapLeft i lost control there, i don`t know what is happening but neither the transaction nor the exception are executed.
effect {
            when (currentKey.recoveryPoint) {
                STARTED -> {
                    catch {
                        newSuspendedTransaction {
                            addLogger(StdOutSqlLogger)
                            throw IllegalStateException()
                        }
                    }.mapLeft {
                        newSuspendedTransaction {
                            // we don't know what happened with the request. Let's unlock the row for retry.
                            idempotencyKeyService.updateKey(currentKey.copy(lockedAt = null)).bind()
                        }
                        throw it
                    }
                }
                else -> {}
            }
        }
    }
        .fold(
            {
                println("CAN RETRY")
                throw it
            },
            {
                println("FINISHED WITH ERROR")
                handleLeft(it)
            },
            {
                println("FINISHED WITH OK")
                call.respond(HttpStatusCode.Created)
            }
        )
}Jorge Bo
04/11/2023, 1:27 PMnewSuspendedTransaction(context = IO)simon.vergauwen
04/11/2023, 1:29 PMnewSuspendedTransaction?Jorge Bo
04/11/2023, 1:34 PMJorge Bo
04/11/2023, 1:34 PMroute("/markup-payments-billi") {
    notarizedPost(markupPaymentBillingOASSpec) {
        effect {
          .....
       }
    }
}simon.vergauwen
04/11/2023, 1:35 PMJorge Bo
04/11/2023, 1:40 PM1.1.5Jorge Bo
04/11/2023, 1:41 PMsimon.vergauwen
04/11/2023, 2:39 PM