How do I formulate this in Kotlin/JS? ```try { ...
# javascript
v
How do I formulate this in Kotlin/JS?
Copy code
try {
    /* ... */
} catch (error) {
    if (error.name === cache.ValidationError.name) {
        throw error
    } else if (error.name === cache.ReserveCacheError.name) {
        <http://core.info|core.info>(error.message)
    } else {
        core.warning(error.message)
    }
}
t
Looks like valid Kotlin :) Only type for error required Custom external interface can be used
v
I thought there might be some more idiomatic way like
Copy code
typealias ValidationError = Error
typealias ReserveCacheError = Error

try {
    /* ... */
} catch (e: ValidationError) {
    throw e
} catch (e: ReserveCacheError) {
    info(e.message ?: "$e")
} catch (e: Error) {
    val message = e.message
    if (message == null) {
        warning(e)
    } else {
        warning(message)
    }
}
or similar
t
Multicatch not supported :( Ticket exists :)
v
Oh, I see. 😞 Do you have URL so I can watch and vote? And there is no readymade exception type I can use for this currently but I have to define an external interface for it, right?
t
how about this?
Copy code
try {
    /* ... */
} catch (error) {
    when(error) {
        is ValidationError -> throw error
        is ReserveCacheError -> <http://core.info|core.info>(error.message)
        else -> core.warning(error.message)
    }
}
or if you want to keep comparing names (not sure if the type checks work in Kotlin/JS):
Copy code
...
when(error.name) {
    cache.ValidationError.name -> ...
...
v
The problem is, that not even the types are available
Ah, no, that the types are not available is my fault, Dukat made them as `typealias`and then the compiler complained that there are non-external definitions
u
you mean aliases were not moved out to a separate file? that's sounds like a bug to me. Can you please file an issue?
v
There are several issues I work-arounded. That is one of them. Another is the missing
@JsModule
annotation. Maybe that's why it was also not moved out to a separate file. I added the
@JsModule
and removed the `typealias`es to made it compile properly
All the
@actions/...
have no
@JsModule
but someone said this is already reported somewhere. In this case it is
@actions/cache
t
Looks like errors will have same type and
when
will be most compact and readable
v
Besides that
Error
has no
name
property
t
dynamic
has :)
v
dynamic
has them all 😄
The
Error
interface in
lib.es5.d.ts
actually has a
name
property
@turansky actually multi-catch is supported, it works perfectly fine, or did I miss something? This is working perfectly fine if the exception types are properly declared as external classes:
Copy code
try {
    /* ... */
} catch (e: ValidationError) {
    throw e
} catch (e: ReserveCacheError) {
    info(e.message ?: "$e")
} catch (e: Throwable) {
    val message = e.message
    if (message == null) {
        if (e is Error) {
            warning(e)
        } else {
            warning("$e")
        }
    } else {
        warning(message)
    }
}
@[JB] Shagen the main problem is not that the `typealias`es were in the same file. The main problem is, that they are actually `typealias`es. I now replace
typealias ValidationError = Error
by
external class ValidationError : Throwable
and
typealias ReserveCacheError = Error
by
external class ReserveCacheError : Throwable
and now I can properly use multiple catch clauses for the different exceptions. And even if the `typealias`es would be ok, what they alias is not. afaiu JavaScript
Error
is translated to Kotlin
Throwable
, so the type aliases should alias
Throwable
, not
Error
. Anyway, before with the `typealias`es all
if
-branches checked for the exception being an instance of
Error
(Kotlin) which of course failed. Now the generated
if
-cascade is working properly.
t
actually multi-catch is supported
Yes, my bad 😞 Multicatch in multicatch - KT-7128
v
Ah, that did you mean with multi-catch, I see. :-)
@turansky tysm for all your help. If you are interested in the outcome, I'm finished now: https://github.com/Vampire/setup-wsl :-)
🔥 1
t
get/set
declarations are redundant here and can be removed
Unusual formatting 🙃 FYI: Coding convention can be activated by Gradle property
v
Are they? (redundant) I adapted to what Dukat generates. I thought those makes the properties optional I think? But I guess I can also make then have null default value instead. Where didn't I follow Kotlin code conventions? I thought I did and the project is set up properly with the default conventions.
t
I adapted to what Dukat generates.
Dukat generate redundant code 😞
Where didn’t I follow Kotlin code conventions?
Parameter formatting for example. Indent - 8 spaces (convention - 4) I saw the same bug IDEA 2020.1
@Vampire PR is ready
v
Yep, seen it, thanks. But right now I'm on vacation. I'll have a look when I'm back.
👍 1
Why did you make the types non-nullable? Isn't it cleaner to have them nullable, as they are actually optional?
t
When you use external interface as “options” (our case) all fields are optional by default.
v
Thanks, merged 🙂
👍 1