Skip to content

Commit

Permalink
Updated with errorhandling
Browse files Browse the repository at this point in the history
  • Loading branch information
bskjon committed Jan 5, 2025
1 parent 56cdca4 commit 131b40d
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 73 deletions.
171 changes: 133 additions & 38 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/main/kotlin/no/iktdev/streamit/library/db/Helpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import java.time.LocalDateTime
import java.time.ZoneId
import java.time.ZoneOffset




fun timestampToLocalDateTime(timestamp: Int): LocalDateTime {
return Instant.ofEpochSecond(timestamp.toLong()).atZone(ZoneId.systemDefault()).toLocalDateTime()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ open class TableDefaultOperations<T : Table> {

}

fun <T> withDirtyRead(db: Database? = null, block: () -> T): T? {
data class TransactionResult<T>(
val result: T,
val exception: Exception? = null
)

fun <T> withDirtyRead(db: Database? = null, block: () -> T, onError: ((Exception) -> Unit)? = null): T? {
return try {
transaction(db = db, transactionIsolation = Connection.TRANSACTION_READ_UNCOMMITTED) {
try {
Expand All @@ -24,14 +29,17 @@ fun <T> withDirtyRead(db: Database? = null, block: () -> T): T? {
}
}
} catch (e: Exception) {
e.printStackTrace()
// log the error here or handle the exception as needed
if (onError == null) {
e.printStackTrace()
} else {
onError.invoke(e)
}
null
}
}


fun <T> withTransaction(db: Database? = null, block: () -> T): T? {
fun <T> withTransaction(db: Database? = null, block: () -> T, onError: ((Exception) -> Unit)? = null): T? {
return try {
transaction(db) {
try {
Expand All @@ -43,14 +51,17 @@ fun <T> withTransaction(db: Database? = null, block: () -> T): T? {
}
}
} catch (e: Exception) {
e.printStackTrace()
// log the error here or handle the exception as needed
if (onError == null) {
e.printStackTrace()
} else {
onError.invoke(e)
}
null
}
}


fun <T> insertWithSuccess(db: Database? = null, block: () -> T): Boolean {
fun <T> insertWithSuccess(db: Database? = null, block: () -> T, onError: ((Exception) -> Unit)? = null): Boolean {
return try {
transaction(db) {
try {
Expand All @@ -64,7 +75,11 @@ fun <T> insertWithSuccess(db: Database? = null, block: () -> T): Boolean {
}
true
} catch (e: Exception) {
e.printStackTrace()
if (onError == null) {
e.printStackTrace()
} else {
onError.invoke(e)
}
false
}
}
Expand All @@ -85,31 +100,34 @@ fun <T> executeOrException(db: Database? = null, rollbackOnFailure: Boolean = fa
}
}
} catch (e: Exception) {
e.printStackTrace()
return e
}
}

fun <T> executeWithResult(db: Database? = null, block: () -> T): Pair<T?, Exception?> {
fun <T> executeWithResult(db: Database? = null, block: () -> T, onError: ((Exception) -> Unit)? = null): T? {
return try {
transaction(db) {
try {
val res = block()
commit()
res to null
res
} catch (e: Exception) {
// log the error here or handle the exception as needed
rollback()
null to e
throw e
}
}
} catch (e: Exception) {
e.printStackTrace()
return null to e
if (onError == null) {
e.printStackTrace()
} else {
onError.invoke(e)
}
return null
}
}

fun <T> executeWithStatus(db: Database? = null, block: () -> T): Boolean {
fun <T> executeWithStatus(db: Database? = null, block: () -> T, onError: ((Exception) -> Unit)? = null): Boolean {
return try {
transaction(db) {
try {
Expand All @@ -123,7 +141,11 @@ fun <T> executeWithStatus(db: Database? = null, block: () -> T): Boolean {
}
true
} catch (e: Exception) {
e.printStackTrace()
if (onError == null) {
e.printStackTrace()
} else {
onError.invoke(e)
}
false
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class CatalogQuery(
}

override fun insertAndGetStatus(): Boolean {
return insertWithSuccess {
return insertWithSuccess(block = {
if (this@CatalogQuery.type == "serie") {
val existing = catalog.select {
(catalog.collection eq this@CatalogQuery.collection) and
Expand All @@ -39,7 +39,7 @@ class CatalogQuery(
it[iid] = this@CatalogQuery.iid
it[genres] = this@CatalogQuery.genres
}
}
})
}


Expand All @@ -65,11 +65,11 @@ class CatalogQuery(
}

fun getId(): Int? {
return withTransaction {
return withTransaction(block = {
catalog.select { catalog.title eq title }.andWhere {
catalog.type eq type
}.map { it[catalog.id].value }.firstOrNull()
}
})
}


Expand Down
Loading

0 comments on commit 131b40d

Please sign in to comment.