Since https://github.com/elastic/elasticsearch/pull/55774 we log exceptions reaching the RestResponse class on their way back to the client in the "rest.suppressed" logger. Status codes <500 are logged on the Debug and >=500 codes are logged on the Warn level.
However there is some logic that doesn't log these errors if the user has requested "error_trace" in the request parameters.
In this case we don't log. To improve visibility of 500s on the server side logs and to be able to measure and alert on occasions of this status code we should remove this condition and always log 500s (and probably 400s on Debug like already done today).
server/src/test/java/org/elasticsearch/rest/RestResponseTests.java
} else {
                        assertThat(response.content().utf8ToString(), not(containsString(ElasticsearchException.STACK_TRACE)));
                    }
                }
            }
        }
    }
    public void testGuessRootCause() throws IOException {
        RestRequest request = new FakeRestRequest();
        {
            Exception e = new ElasticsearchException("an error occurred reading data", new FileNotFoundException("/foo/bar"));
            RestResponse response = new RestResponse(new DetailedExceptionRestChannel(request), e);
            String text = response.content().utf8ToString();
            assertThat(text, containsString("""
                {"root_cause":[{"type":"exception","reason":"an error occurred reading data"}]"""));
        }
        {
            Exception e = new FileNotFoundException("/foo/bar");
            RestResponse response = new RestResponse(new DetailedExceptionRestChannel(request), e);
            String text = response.content().utf8ToString();
            assertThat(text, containsString("""
                {"root_cause":[{"type":"file_not_found_exception","reason":"/foo/bar"}]"""));
        }
    }
    public void testNullThrowable() throws Exception {
        RestRequest request = new FakeRestRequest();
        RestChannel channel = new SimpleExceptionRestChannel(request);
        RestResponse response = new RestResponse(channel, null);
        String text = response.content().utf8ToString();
        assertThat(text, containsString("\"error\":\"unknown\""));