[ANN] ErrorTypes.jl - Rust-like safe errors in Julia

There is no easy way to change an exception into a function that uses error values (without using try-catch which is slow and unreliable). On the other hand, it’s very easy to convert an error value into an exception.

If the exception you are catching come from the HTTP.request function itself, you need to use try/catch:

res::Result{SomeValue, SomeError} = try
    Ok(HTTP.request( ... ))
catch e
    Err(e)
end
return res

However, if the HTTP request itself returns a result/error value, I don’t think you will benefit very much form converting it to an ErrorTypes.jl Result. You could do something like this, however:

function do_request()::Result{SomeValue, SomeError}
    response = HTTP.request( ... )
    return if (response isa GoodType)
        Ok(do_stuff(response))
    else
        Err(some_error_value)
    end
end

But I don’t know about HTTP or what it can actually return, so I can’t give examples to what your Result type should look like.

2 Likes