Result icon indicating copy to clipboard operation
Result copied to clipboard

Add Correlation Id to Error Statuses

Open KyleMcMaster opened this issue 1 year ago • 2 comments

          Should we have more options for including correlation ids? And if so, rather than having separate methods for all of them, would it make sense to bake it into a single parameter object, like:
public record ErrorList(IEnumerable<string> ErrorMessages, string? CorrelationId);

?

Then the factory methods would just take in (ErrorList errorList) and we wouldn't need any overloads or alternate methods.

Originally posted by @ardalis in https://github.com/ardalis/Result/issues/169#issuecomment-1989829677

KyleMcMaster avatar Mar 31 '24 20:03 KyleMcMaster

I can see there is a CorrelationId in Result

public static implicit operator Result<T>(Result result) => new Result<T>(default(T))
{
    Status = result.Status,
    Errors = result.Errors,
    SuccessMessage = result.SuccessMessage,
    CorrelationId = result.CorrelationId,
    ValidationErrors = result.ValidationErrors,
};

However I can't figure out how to set CorrelationId using static methods such as Error, Invalid, NotFound etc... for example .. return Result<DetailedDeathRecordDto>.Error(message);

Is the purpose of this issue to set CorrelationId when returning a Result<T> using these this static methods?

If not how do you set the CorrelationId?

Currently in my Minimal Api I am logging the CorrelationId successfully using serilog like so

builder.Host.UseSerilog((context, services, configuration) => configuration
    .ReadFrom.Configuration(context.Configuration)
    .ReadFrom.Services(services)
    .Enrich.FromLogContext()
    .Enrich.WithCorrelationIdHeader("x-correlation-id")
);

My expectation or hope was that "x-correlation-id" value would have been added by default to Result<T> CorrelationId property.

At a minimum I would like to be able to set the CorrelationId.

What am I doing wrong?

AminurRouf avatar Apr 03 '24 20:04 AminurRouf

@AminurRouf The current plan is to create a factory method for Error that takes in a record ErrorList which has properties for IEnumerable ErrorMessages and a string CorrelationId. I should have PR up shortly for this functionality.

KyleMcMaster avatar Apr 03 '24 21:04 KyleMcMaster