Setting Response Status Code and Text from a Service

We have an XData service which we use as a proxy to call another third party service. We need to return the same Status Code and Status Text that we receive from that call.

In the call to our service we have tried

TXDATAOperationContext.Current.Response.StatusCode = NNN;

but that doesn't seem to do it.

Any ideas?

Thanks

Does raising an exception help? This is what I've been using, for example, if an endpoint is called without a valid JWT or with some other issue that prevents the service endpoint from working normally.

 raise EXDataHttpException.Create(401, 'Unauthorized');

We've tried that, but it wraps the response in an Error wrapper and we need to return the response as is.

For error status codes (greater or equal than 400), raise an exception like @AndrewSimard mentioned.

For non-error status codes (lower than 400), use SetStatusCode method of the handler:

TXDataOperationContext.Current.Handler.SetStatusCode(204);
1 Like

We are using the Exception, but were trying not to get the error wrapper

{
"error" : {
    "code": 400,
    "message": "an error"
  }
}

Although we can live with the standard, is there a way to tell the error that the message is a JSON object so doesn't need the quote marks escaped?

or perhaps provide a more detailed error response such as

{
    "errors": [
        {
            "error": "auth-0001",
            "message": "Incorrect username and password",
            "detail": "Ensure that the username and password included in the request are correct",
            "help": "https://example.com/help/error/auth-0001"
        },
        ...
    ]
}

as discussed in https://blog.restcase.com/rest-api-error-codes-101/

The "correct" way to do this in XData is to raise your custom exceptions and handle them in the OnModuleException event.

In that event, you can check if the exception is your special exception type, then get the additional properties from the exception object you want, set Action parameter to Ignore, and then provide the full custom JSON response yourself using the

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.