CacheResponse with ServiceExceptionHandlers

I have created a global exception handler:

                ServiceExceptionHandlers.Add((req, request, exception) =>
                    // log exception
                    Logger.Info(req.OperationName + " - " + req.Headers["Device"] + "\n" + request.Dump());

                    // call default exception handler
                    return DtoUtils.CreateErrorResponse(request, exception);

If an exception happens the exception handler above fires as expected. DtoUtils.CreateErrorResponse returns a proper object with a status code of 500 (which is correct) and this is correctly outputted to swagger or postman.

However when the service is marked with the [CacheResponse] attribute and an exception happens, the status code in swagger/postman is now 200 and the error response has been cached.

How do I get the status code to be correct when using the Cache Response attribute and not have the error response cached?

Yeah it shouldn’t cache Error responses which is now fixed in this commit.

This change is available from v4.0.61 which is now available on MyGet.

Note: if you want to use default Exception Handler in ServiceExceptionHandlers you can just return null

Thanks @mythz for the swift resolution!

1 Like