ServiceStack / Issues

Issue Tracker for the commercial versions of ServiceStack
11 stars 6 forks source link

Response DTO is not populated when throwing Exception #727

Closed labilbe closed 4 years ago

labilbe commented 4 years ago

Environment

Steps to reproduce the behavior

Asp.Net MVC controller

public ActionResult Index()
{
    try {
        Gateway.Send(new UpdateProductPhase());
    }
    catch (WebServiceException ex) {
        var response = ex.ResponseDto as UpdateProductPhaseResponse;
        'At this point, response should have ErrorMessages property populated but it is null
    }
}

Service DTO layer

public class UpdateProductPhaseResponse : IHasResponseStatus
{
    public IEnumerable<string> ErrorMessages { get; set; } = new List<string>();
    public ResponseStatus ResponseStatus { get; set; }
}
public class UpdateProductPhase : IPut, IReturn<UpdateProductPhaseResponse>
{
}

Service Layer

public class ProductService : Service
{
    public UpdateProductPhaseResponse Put(UpdateProductPhase request)
    {
        throw new WebServiceException
        {
            ResponseDto = new UpdateProductPhaseResponse
            {
                ErrorMessages = new List<string> { "toto", "test" }
            }
        }
    }
}

When calling Gateway.Send, Exception is thrown and I enter on the catch block. The only problem is that ErrorMessages property is null (or empty). It should have two elements, "toto" and "test".

I don't know if it's due to the ServiceStack version because I am migrating from autowired services (inside controllers) to Gateway.Send.

Thank you for your help!

mythz commented 4 years ago

It doesn't preserve custom data in a custom Exception, ServiceStack serializes the Exception into a serializable ResponseStatus DTO that it injects in the Response DTO following the Error Response Type rules.

Have a look at the Error Handling docs for how to return a custom exception. Everything needs to be captured in the ResponseStatus DTO which also has a Meta dictionary for additional custom info that doesn't fit in existing properties.

labilbe commented 4 years ago

Thank you!