Skip to content

[Question] Adding shared context for Result which presents in both Success and Error cases #536

@hankovich

Description

@hankovich

I'm writing an important integration with a buggy third-party API that constantly changes its returning models. For debugging purposes and to be able to reprocess or replay its responses, I want to store plain responses along with some metadata like request duration. So, I'm looking for something like:

async Task<Result<Response, Error>> InvokeAsync()
{
    var firstResponse = await GetResponseAsStringAsync(...); // network call that returns raw API response, may fail
    var parsedFirstResponse = serializer.Deserialize<FirstResponse>(firstResponse); // may fail because API has weird contracts
    
    var secondResponse = await httpClient.GetStringAsync/* some parts of parsedFirstResponse */); // may fail
    var parsedSecondResponse = serializer.Deserialize<SecondResponse>(secondResponse); // may fail
}

And I want the Result I return to have some shared context in both success and error cases. In the case of success, it will have information about two successful responses, including their raw values and durations. In the case of failure, it will contain all available information (i.e., if processing fails during parsing, I want the Result to contain the raw response that caused parsing issues).

Has anyone ever faced such requirements before? I'm considering adding Result<T, TError, TContext> to my project.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions