Hi everyone, I'm trying to catch an exception on K...
# javascript
f
Hi everyone, I'm trying to catch an exception on Kotlin/JS but it's not working for some reason. This is my code:
Copy code
try {
    val dateStr = date?.toIsoString(dateOnly = true)

    ...
} catch (e: Exception) {
    logError("Failed to parse date", cause = e)
}

public fun Date.toIsoString(dateOnly: Boolean = false): String {
    val timezoneOffset = getTimezoneOffset()
    val dateStr = Date(getTime() - (timezoneOffset * 60_000)).toISOString()

    return if (dateOnly) dateStr.split("T")[0] else dateStr
}
And this is the error I get:
Copy code
Uncaught RangeError: Invalid time value
    at Date.toISOString (<anonymous>)
Any idea if I'm doing something wrong or if this error just can't be catched for whatever reason?
This happens when I call
toISOString()
with an invalid date, which sometimes happens when people are entering the date manually on my web app
t
Possibly you need catch
Throwable
or even
Error
f
I had tried
Error
and that didn't work either @turansky, however I tried
Throwable
now and that did work, thanks 🙂
😀 2
h
Coming in a month later: I was looking for a function to get the locale date from a string − and found it lacks from the date library (in a jvm environment, I leave this to a DateFormatter object). Striving to understand how to use the LocaleOptions object, I did a search in this JS slack channel and came across your code snippet here. Thanks a lot, that saves my day!
f
I'm glad it helped @hallvard 🙂