Skip to content

Either introduce getOrElseThrow method #3598

Open
@mipo256

Description

@mipo256

I have not found any appropriate issue, so I'm leaving it here.

So, assume we have the following function that returns the karrow Either:

fun <T> send(body: T) : Either<ExternalFailure, ExternalSuccess> {
    // implementation
}

What it does is it sends over a specific protocol some information, it does not matter for this issue. What I need is to call the function send(body: t), and I need to throw a RuntimeException to the callee, in case we hit an error. This is the contract that I have to adhere to.

The way I should recognize that the error had happened is by checking if the Either is Left. So I need something like this:

return send(myBody).throwOnLeft({ left ->
    ProtocolIOException("Something bad had happened :${left}")
})

I do not need to mapLeft, or getOrElse or anything - I just need to return the ExternalSuccess (the Right in my case), or I need to throw an exception.

Is there any built-in way to do so?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions