vkhorikov / CSharpFunctionalExtensions

Functional extensions for C#
MIT License
2.39k stars 300 forks source link

You attempted to access the Error property for a successful result. A successful result has no Error. #500

Open namigmustafa opened 1 year ago

namigmustafa commented 1 year ago

I have receiving You attempted to access the Error property for a successful result. A successful result has no Error. error when I send value from service to controller. I receive it on service level.

public async Task<CSharpFunctionalExtensions.Result<QuestionDto>> GetQuestionWithElementsById2(int id)
    {
        var questionWithElements = await _tenantUnitOfWork.QuestionRepository.GetQuestionWithElementsById(id);

        var questionsWithElementsDto = _mapper.Map<QuestionDto>(questionWithElements);

        return Result.Success<QuestionDto>(questionsWithElementsDto);
    }

image

titouancreach commented 1 year ago

Is this a real error or just your debugger ? because I have the same in vs code when I inspect the value with the debugger, because it tries to show me the value of the Error even if there is no error.

namigmustafa commented 1 year ago

It is real

hankovich commented 1 year ago

Show the stacktrace, please

namigmustafa commented 1 year ago
 at System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1.GetMemberAndWriteJson(Object obj, WriteStack& state, Utf8JsonWriter writer)
   at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1.GetMemberAndWriteJson(Object obj, WriteStack& state, Utf8JsonWriter writer)
   at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.JsonConverter`1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.JsonConverter`1.WriteCoreAsObject(Utf8JsonWriter writer, Object value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.JsonSerializer.WriteCore[TValue](Utf8JsonWriter writer, TValue& value, JsonTypeInfo jsonTypeInfo, WriteStack& state)
   at System.Text.Json.JsonSerializer.<WriteStreamAsync>d__121`1.MoveNext()
   at System.Text.Json.JsonSerializer.<WriteStreamAsync>d__121`1.MoveNext()
   at System.Text.Json.JsonSerializer.<WriteStreamAsync>d__121`1.MoveNext()
   at Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter.<WriteResponseBodyAsync>d__5.MoveNext()
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<<InvokeNextResultFilterAsync>g__Awaited|30_0>d`2.MoveNext()
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<<InvokeFilterPipelineAsync>g__Awaited|20_0>d.MoveNext()
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<<InvokeAsync>g__Awaited|17_0>d.MoveNext()
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<<InvokeAsync>g__Awaited|17_0>d.MoveNext()
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<<Invoke>g__AwaitRequestTask|6_0>d.MoveNext()
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.<Invoke>d__6.MoveNext()
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<<Invoke>g__Awaited|6_0>d.MoveNext()
hankovich commented 1 year ago

The stacktrace shows that this error was thrown during response formatting, i.e. result was returned not only from service to controller, but also it was returned from controller itself.

If you want to return results from controller, you may use this extension to add corresponding converters for System.Text.Json.

namigmustafa commented 1 year ago

Is that link should have something? it is empty page? :)

hankovich commented 1 year ago

My bad, updated it 👍

namigmustafa commented 1 year ago

huge thanks