2
votes

I'm writing a backend using ServiceStack. our main front end client is an Angular 2 application using TypeScript. To that end, we are using the DTOs that are generated by the services when hitting /types/typescript and /types/typescript.d. This all works fine and good using the JsonServiceClient... but it seems that the response status code is somehow wrapped up in the call and not returned as it as when using a standard XHR call.

Finding the AddResponseStatus configuration item, I changed the service configuration to add this on any DTO that didn't already have the property (which mine didn't):

var ntf = new NativeTypesFeature();
ntf.MetadataTypesConfig.AddResponseStatus = true;
Plugins.Add(ntf);

After refreshing the TypeScript reference, I can see that all DTO types returned now have a ResponseStatus property on them.

export class QueryReportResponse
{
    Data: string;
    ResponseStatus: string;
}

Here is a scrubbed return (removed the 'Data' portion) showing the property exists on the object:

<QueryReportResponse xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/blah.blah.ServiceModel.Messages">
  <Data>
    blah blah data here
  </Data>
  <ResponseStatus i:nil="true"/>
</QueryReportResponse>

Now, I assumed (wrongly so) that by doing this, some sort of status would be set 'automatically'. I'm obviously not right here, as this property is not set. My front end guy is asking to be able to see the status on all returned calls, like he was able to before when using straight XHR prior to using the JsonServiceClient, as now he cannot see the return status.

What is the intent of this field? I cannot set it manually, as it's added by ServiceStack dynamically at runtime. I can only assume that I would have to create my own base class return DTO of sorts and set that on the way back to the caller... can someone help me understand the purpose of this field? Thanks.

1

1 Answers

3
votes

ServiceStack's Add TypeScript Reference is typically used with the TypeScript servicestack-client. The ResponseStatus is used in ServiceStack's Error Handling which is used to capture structured Error Information. It's not populated for successful responses and it's distinct from the HTTP Response Status code although if throwing a HTTP Error the ResponseStatus.ErrorCode will typically contain the HttpStatusCode enum string.

Adding ResponseStatus on DTOs

Adding the ResponseStatus on DTOs, e.g:

ntf.MetadataTypesConfig.AddResponseStatus = true;

Just adds the ResponseStatus on generated DTOs where they didn't previously exist. It doesn't have any effect on Response DTOs which already includes the ResponseStatus property, e.g:

public class MyResponse
{
    public ResponseStatus ResponseStatus { get; set; }
}

Accessing HTTP Status Responses

Developers shouldn't care what the HTTP Status code is for successful responses (which is almost always 200 OK). ServiceStack's TypeScript JsonServiceClient will just return the Typed Response DTO for successful responses, e.g:

var response = await client.post(request)

They should only be interested for handling error responses, however it's expected to use the ResponseStatus.ErrorCode to determine the type of Error and apply application error handling logic, e.g:

try {
    var response = await client.post(request)
} catch (e) {
    console.log(e.responseStatus.errorCode);
}

If they really want the HTTP Status they can get it using a response filter, e.g:

var status = null;
try {
    client.responseFilter = res => status = res.status;
    var response = await client.post(request)
} catch (e) {
    console.log(status, e.responseStatus.errorCode);
}