ardalis / Result

A result abstraction that can be mapped to HTTP response codes if needed.
MIT License
847 stars 100 forks source link

Add Correlation Id to Error Statuses #172

Closed KyleMcMaster closed 4 months ago

KyleMcMaster commented 5 months ago
          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

AminurRouf commented 5 months ago

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 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?

KyleMcMaster commented 5 months ago

@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.