From 11f19b1f76e580056b8629cc2dea29e8b2bbbd77 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Wed, 7 Sep 2022 13:05:23 +0300 Subject: [PATCH] Add asserts --- .../kotlin/org/koitharu/kotatsu/parsers/util/Assert.kt | 8 ++++++++ .../kotlin/org/koitharu/kotatsu/parsers/util/Parse.kt | 3 +++ 2 files changed, 11 insertions(+) create mode 100644 src/main/kotlin/org/koitharu/kotatsu/parsers/util/Assert.kt diff --git a/src/main/kotlin/org/koitharu/kotatsu/parsers/util/Assert.kt b/src/main/kotlin/org/koitharu/kotatsu/parsers/util/Assert.kt new file mode 100644 index 00000000..fbf6b9af --- /dev/null +++ b/src/main/kotlin/org/koitharu/kotatsu/parsers/util/Assert.kt @@ -0,0 +1,8 @@ +package org.koitharu.kotatsu.parsers.util + +fun T?.assertNotNull(name: String): T? { + assert(this != null) { + "Value $name is null" + } + return this +} \ No newline at end of file diff --git a/src/main/kotlin/org/koitharu/kotatsu/parsers/util/Parse.kt b/src/main/kotlin/org/koitharu/kotatsu/parsers/util/Parse.kt index 2bac7814..7c965778 100644 --- a/src/main/kotlin/org/koitharu/kotatsu/parsers/util/Parse.kt +++ b/src/main/kotlin/org/koitharu/kotatsu/parsers/util/Parse.kt @@ -68,10 +68,13 @@ fun String.toAbsoluteUrl(domain: String): String = when { } fun DateFormat.tryParse(str: String?): Long = if (str.isNullOrEmpty()) { + assert(false) { "Date string is null or empty" } 0L } else { runCatching { parse(str)?.time ?: 0L + }.onFailure { + assert(false) { "Cannot parse date $str: ${it.message}" } }.getOrDefault(0L) }