Custom attribute error response

Hi, I’m having some trouble trying to get a custom response error from my Attribute:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
    public class TestAttribute : AuthenticateAttribute
    {
        public override void Execute(IRequest req, IResponse res, object requestDto)
        {
            if (req.Verb == "POST")
            {
                var responseDto = new ErrorResponse
                {
                    ResponseStatus = new ResponseStatus
                    {
                        ErrorCode = GetType().Name,
                        Message = "Test Conflict",
                        Errors = new List<ResponseError>
                        {
                            new ResponseError
                            {
                                ErrorCode = "NotEmpty",
                                FieldName = "Company",
                                Message = "'Company' should not be empty."
                            }
                        },
                        Meta = new Dictionary<string, string>
                        {
                            {"foo", "bar"},
                            {"qwe", "asd"}
                        }
                    }
                };

                throw new HttpError(HttpStatusCode.Conflict, "Conflict")
                {
                    Response = responseDto
                };
            }
        }
    }

For semplicity I just created a test attribute to return the error object on my service POST request.
I’m getting the correct status code ErrorCode (409) and Message (Test Conflict) but nothing is returned as response, just a {} where I want the Errors and Meta objects.
To be complete I have this config setup:

var hostConfig = new HostConfig();

hostConfig.DebugMode = true;
hostConfig.WriteErrorsToResponse = true;
hostConfig.AllowJsonpRequests = false;

SetConfig(hostConfig);

What am I missing? Is there a way to get that ErrorResponse back from my attribute?

You can’t throw Error Responses inside filters, you need to write Errors directly to the Response

I’ve tried this to set the status code:

res.StatusCode = (int)HttpStatusCode.Conflict;
res.StatusDescription = "Test Conflict";

but what should I to use to add other data (for example the Errors list) to the response?

If you also want a response body you’ll need to write the populated Response DTO to the Response Stream, e.g:

res.WriteToResponse(req, errorResponse);

Thank you very much!