2
votes

Server Side

public class MyServices : Service { public object Get(Hello request) { throw new InvalidOperationException("test error message"); //return new HelloResponse { Result = "Hello, {0}!".Fmt(request.Name) }; } }

Client Side

try { var client = new JsonServiceClient("http://localhost:28586/"); var response = client.Get<HelloResponse>(new Hello { Name = "DHJ" }); } catch (WebServiceException ex) { // ex.ErrorCode = "InvalidOperationException" // No Problem. // ex.ErrorMessage = null // always null. Why? }

And i saw the docs of ServiceStack like below:

Throwing C# Exceptions

In most cases you won’t need to be concerned with ServiceStack’s error handling since it provides native support for the normal use-case of throwing C# Exceptions, e.g.:

public object Post(User request) { if (string.IsNullOrEmpty(request.Name)) throw new ArgumentNullException("Name"); } Default Mapping of C# Exceptions to HTTP Errors

By Default C# Exceptions:

Inheriting from ArgumentException are returned with a HTTP StatusCode of 400 BadRequest NotImplementedException or NotSupportedException is returned as a 405 MethodNotAllowed AuthenticationException is returned as 401 Unauthorized UnauthorizedAccessException is returned as 403 Forbidden OptimisticConcurrencyException is returned as 409 Conflict Other normal C# Exceptions are returned as 500 InternalServerError This list can be extended with user-defined mappings on Config.MapExceptionToStatusCode.

1

1 Answers

1
votes

Your HelloResponse class needs a ResponseStatus property, from the Error Handling docs:

Error Response Types

The Error Response that gets returned when an Exception is thrown varies on whether a conventionally-named {RequestDto}Response DTO exists or not.

If it exists:

The {RequestDto}Response is returned, regardless of the service method's response type. If the {RequestDto}Response DTO has a ResponseStatus property, it is populated otherwise no ResponseStatus will be returned. (If you have decorated the {ResponseDto}Response class and properties with [DataContract]/[DataMember] attributes, then ResponseStatus also needs to be decorated, to get populated).

Otherwise, if it doesn't:

A generic ErrorResponse gets returned with a populated ResponseStatus property.