OK< I am using AbstractValidator<T> for my DTO’s as they come into my services. They are working fine.
My question is that when I send a request will invalid content I get the expected WebServiceException in the JsonClient, however, the exception message may simply say: “Predicate” when I have a rule that uses the Must() method.
Can I customize the error message that is sent to the client to give more helpful guidance?
I have read the validation wiki, but not able to get what I would need other than “Predicate”.
Any pointers?
Do you mean the .WithMessage() ext method?
https://github.com/ServiceStack/EmailContacts/#fluent-validation
Jezz Santos:
Yes, WithMessage() did’nt seem to have any effect on the message text of the WebServiceException.
What do you mean by “message text” the HTTP StatusDescription? or ResponseStatus.Message field?
Jezz Santos:
WSE.ErrorCode = Predicate
WSE.ErrorMessage = null
WSE.Message = Predicate
WSE.Statuscode = 400
WSE.StatusDescription = Predicate
When I have a ResponseStatus property in my Request DTO:
WSE.ErrorCode = Predicate
WSE.ErrorMessage = whatever WithMessage() says
WSE.Message = Predicate
WSE.Statuscode = 400
WSE.StatusDescription = Predicate
It would be handy to somehow give the consumer (JsonClient) a clue as to what caused the 400 in the exception, with perhaps some guidance on how to fix it/pointer to API doco
As to which properties of this exception to reflect that (or whether to do that in the response headers), I guess we can debate that, largely based on the intention of this exception class. Either way the text “Predicate” does little to help anyone consuming the API discover what they did wrong in the request.
How can we make this experience better?
How did you get the “Predicate” text?
Here’s an example of the expected response of using the validator:
https://github.com/ServiceStack/ServiceStack/blob/master/tests/ServiceStack.WebHost.Endpoints.Tests/CustomerServiceValidationTests.cs#L321-L338
You can also use the .WithErrorCode() to specify what the ErrorCode should be.
Jezz Santos:
Predicate comes from calling the Must() method.
ok cool, so the .WithErrorCode() should allow you to specify a user-defined code? By default the ResponseStatus.Message takes the Message of the first error
Jezz Santos:
Yes, I conclude now that an implementer requires the ResponseStatus property in the response DTO in order for either WithMessage() or WithErrorCode() to have any effect on the top level fields in the WebServiceException. That looks to be the root cause of the problem at this point. i.e. thinking the ResponseStatus property was optional or automatic in the new API.
Perhaps a note in the wiki to this kind of effect would help others coming after, do you think?
A generic ErrorResponse that includes a ResponseStatus property should be returned when your Response DTO doesn’t have the “{RequestDto}Response” naming format. This is documented here:
https://github.com/ServiceStack/ServiceStack/wiki/Error-Handling#error-response-types
Jezz Santos:
wikid, thanks! I must have missed that nugget many times past. I apologise.