Kotlin / dukat

Converter of <any kind of declarations> to Kotlin external declarations
552 stars 44 forks source link

Exception subclasses are generated as typealiases (for the wrong class) instead of proper subclasses #398

Open Vampire opened 3 years ago

Vampire commented 3 years ago

If you generate for @actions/cache, the generated cache.module_@actions_cache.kt contains

typealias ValidationError = Error
typealias ReserveCacheError = Error

This has three problems.

  1. JS Error is mapped to Throwable in Kotlin, so if at all, they should be aliases for Throwable
  2. The typealiases are no externals definition so Kotlin is not going to like them being in there, once the JsModule annotation was added
  3. The fact that they are actually typealiases instead of proper subclasses. This makes it impossible to have different catch clauses like this: https://github.com/Vampire/setup-wsl/blob/master/src/main/kotlin/net/kautler/github/action/setup_wsl/SetupWsl.kt#L140-L157, as all exceptions are mapped to the same class then and it will not work properly. If you instead replace the typealiases with this, you can properly use mutliple catch clauses:
    external class ValidationError : Throwable
    external class ReserveCacheError : Throwable
Vampire commented 3 years ago

These are the according TS declarations:

export declare class ValidationError extends Error {
    constructor(message: string);
}
export declare class ReserveCacheError extends Error {
    constructor(message: string);
}
Schahen commented 3 years ago

The fact that type aliases might end up in a @file:JsModule/ @file:JsQualifier-annotated file is a separate bug per se.

Vampire commented 3 years ago

Actually the file does not have a JsModule or JsQualifier annotation given by Dukat. I insert them after generation. Probably due to #240

Vampire commented 3 years ago

@actions/http-client 1.0.9 has the same problem now with typealias HttpClientError = Error.