Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 845
natasha Avatar asked Dec 05 '25 07:12

natasha


1 Answers

You can use custom scope around WriteErrorToResponse:

using (JsConfig.With(...))  {
    await res.WriteErrorToResponse(req)
}
like image 190
mythz Avatar answered Dec 08 '25 01:12

mythz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!