Result.Net icon indicating copy to clipboard operation
Result.Net copied to clipboard

When I use `WithError(ex)` within `Exception ex`, I come across this error.

Open kennykanp opened this issue 2 years ago • 1 comments

catch (Exception ex)
{
	return Result.Failure()
		.WithMessage("an internal exception has been thrown")
		.WithCode(ResultCode.OperationFailedException)
		.WithError(ex);
}
System.NotSupportedException
  HResult=0x80131515
  Message=Serialization and deserialization of 'System.Reflection.MethodBase' instances is not supported. Path: $.Errors.Exception.TargetSite.
  Source=System.Text.Json
  StackTrace:
   at System.Text.Json.ThrowHelper.ThrowNotSupportedException(WriteStack& state, NotSupportedException ex)
   at System.Text.Json.Serialization.JsonConverter`1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.<SerializeAsync>d__9.MoveNext()
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.<SerializeAsync>d__9.MoveNext()
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.<SerializeAsync>d__9.MoveNext()
   at Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter.<WriteResponseBodyAsync>d__5.MoveNext()
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<<InvokeResultAsync>g__Logged|22_0>d.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__Logged|17_1>d.MoveNext()
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<<InvokeAsync>g__Logged|17_1>d.MoveNext()
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<<Invoke>g__AwaitRequestTask|7_0>d.MoveNext()
   at CaseManagement.Configurations.MiddlewareStatusCodeByResult.<Invoke>d__2.MoveNext() in D:\02_Projects\VRS 2.0\VRS.CaseManagement\src\CaseManagement.WebAPI\Configurations\MiddlewareStatusCodeByResult.cs:line 26

  This exception was originally thrown at this call stack:

Inner Exception 1:
NotSupportedException: Serialization and deserialization of 'System.Reflection.MethodBase' instances is not supported.

kennykanp avatar Apr 21 '24 20:04 kennykanp

as you can see from the call stack the error is not from the Result object, it is thrown in MiddlewareStatusCodeByResult.cs:line 26 and it is related to JSON Serialization or deserialization of the exception object. based on the call stack, I think you are returning the Result object as a response from your web-app, the issue is you are returning it with the exception object, and that is what causes the issue with System.Text.Json.

YoussefSell avatar Apr 23 '24 12:04 YoussefSell