Viceroy icon indicating copy to clipboard operation
Viceroy copied to clipboard

Return HttpInvalid error instead of FastlyStatus::Error when failed to build Uri

Open arayaryoma opened this issue 1 year ago • 1 comments

Fastly Rust SDK maps FastlyStatus::Error to BackendCreationError::NameInUse. By this, when we passed invalid host name value to fastly::Backend::builder func, we always received NameInUse error. It's a misleading behavior for developers, who will assume they register the same backend multiple times.

In this PR, I defined a new internal Error InvalidBackendUrl and map it as FastlyStatus::HttpInvalid. It will help developers to awake they are using invalid URI to build the dynamic backend.

Example

before

let backend = match fastly::Backend::builder("example", "http://example.com").finish() {
    Ok(backend) => backend,
    Err(e) => {
        println!("Error: {:?}", e); // Error: NameInUse
        return Err(e);
    }
};

after

let backend = match fastly::Backend::builder("example", "http://example.com").finish() {
    Ok(backend) => backend,
    Err(e) => {
        println!("Error: {:?}", e); // Error: HostError(FastlyStatus::HTTP_INVALID_ERROR)
        return Err(e);
    }
};

arayaryoma avatar Jun 05 '24 07:06 arayaryoma

This is going in a good direction, but the name of the error itself seems incorrect: the parameter to the backend builder is not a URL, so the error cannot indicate that the user provided an 'invalid backend URL' (even though the provided value is used in constructing a URL). Should it be InvalidBackendSpecification or something similar?

Edit: I realize this is internal to Viceroy, but I'd still prefer the name to be accurate for the benefit of future readers of the code. It would also be nice if we had a better FastlyStatus value to map it into, but we don't.

kpfleming avatar Jul 26 '24 14:07 kpfleming