Result icon indicating copy to clipboard operation
Result copied to clipboard

Can I pass a value into Conflict() ?

Open alvipeo opened this issue 6 months ago • 9 comments

In my scenario I need to return "Retry-After" value when the result is Conflict (basically a DateTimeOffset). Is this possible? I looked into the code and the only way to pass anything is errorMessages array.

I think this is a valid scenario.

alvipeo avatar Jul 16 '25 14:07 alvipeo

You want to return the value that's already present or the value you tried to provide or both?

ardalis avatar Jul 16 '25 15:07 ardalis

I return a Conflict but I have a timestamp which must be returned along with the status.

alvipeo avatar Jul 16 '25 16:07 alvipeo

I see so as an example:

There's a Contact with a LastName of Smith and a timestamp of 1234.

Someone fetches it. Someone else updates the LastName to Jones and the timestamp updates to 1235 (or whatever different)

First someone sends an update. Timestamps don't match. Return a Conflict.

What might be useful to return?

A) The Contact or values you tried to save B) The Contact or values present in the data store C) The Timestamp in the data store (which doesn't match yours)

You just want C?

ardalis avatar Jul 16 '25 16:07 ardalis

In any case if we permit setting the value you can obviously return any combination of A/B/C/etc as the type returned, but you'd need a custom type. If you're expecting to return a Result or Result<Contact> then there's not any easy way for the Value to be used for just a timestamp or some custom response type in a strongly typed fashion. That's why maybe it would make sense to support a new ResultConflictDetails type or something which could include some or all of these things.

ardalis avatar Jul 16 '25 17:07 ardalis

Suppose you have an entity which state changes with the SetStatus() method. Changing the status is a time consuming operation, thus needs to be throttled. The 1st caller issues SetStatus request and gets back Accepted. The throttling period is 15 sec. If the 2nd request comes sooner than that, I need to return Conflict with the time when the next request is possible to run.

alvipeo avatar Jul 16 '25 17:07 alvipeo

Interesting. I'd think of that more as a 429 Limit Exceeded than a 409 Conflict, in REST/HTTP terms. Which I don't think we have a Result for presently. In the HTTP world you'd return details like when the limit resets in a header.

ardalis avatar Jul 16 '25 17:07 ardalis

You might be right with 429, but the problem stays...

alvipeo avatar Jul 16 '25 17:07 alvipeo

Interesting. I'd think of that more as a 429 Limit Exceeded than a 409 Conflict, in REST/HTTP terms. Which I don't think we have a Result for presently. In the HTTP world you'd return details like when the limit resets in a header.

I agree. I also feel like the limit reset header should be added in some cross-cutting middleware as opposed to adding that limit reset value to Result and having Result-extensions translate it to a response header. I'm not saying I'm completely against the feature request, but it feels like a misuse of the Result object currently. I'd be open to review any potential PRs if someone wants to try an implementation.

KyleMcMaster avatar Jul 18 '25 15:07 KyleMcMaster

Basically, you're saying the Result must contain a union type with "dual" DTO's. Is that correct? Example, please.

alvipeo avatar Jul 18 '25 21:07 alvipeo