How to return/throw ServiceStack's HttpError using scoped JsConfig?

Viewed 59

I have multiple API versions that uses the same AppHost, which is why I don't want to modify the static JsConfig--I only want the HttpError response to be serialized using a scoped JsConfig. I tried using:

throw new HttpError(statuscode){
    Response = errorResponseDto,
    ResultScope = () => JsConfig.With(...)
};

and also attempted using:

UncaughtExceptionHandlers.Add(async (req, res, name, exception) =>
{
    await res.WriteErrorToResponse(req, req.ContentType, req.OperationName, "test", new Exception("test2"), 401);
});

but not sure how to apply the scoped JsConfig in this case. Am I missing something here? Is this possible at all?

Add: if it matters at all, I'm trying to return the error response in camel case, but only for the specific API version which returns responses in camel case as well.

1 Answers

You can use custom scope around WriteErrorToResponse:

using (JsConfig.With(...))  {
    await res.WriteErrorToResponse(req)
}
Related