From c96dc3ee90e28f36149924d3c8facd8c8aec7603 Mon Sep 17 00:00:00 2001 From: "M. Taimoor Zaeem" Date: Wed, 5 Feb 2025 11:01:11 +0500 Subject: [PATCH] fix: log 503 client error to stderr --- CHANGELOG.md | 1 + src/PostgREST/AppState.hs | 5 +++-- test/io/test_io.py | 19 +++++++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 62543da460..b26ed38673 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). + Fixed `"column . does not exist"` error when selecting `?select=...table(aias:count())` - #3727, Clarify "listening" logs - @steve-chavez - #3795, Clarify `Accept: vnd.pgrst.object` error message - @steve-chavez + - #3841, Log `503` client error to stderr - @taimoorzaeem ### Changed diff --git a/src/PostgREST/AppState.hs b/src/PostgREST/AppState.hs index f5f0bd36ee..7974009498 100644 --- a/src/PostgREST/AppState.hs +++ b/src/PostgREST/AppState.hs @@ -261,8 +261,9 @@ usePool AppState{stateObserver=observer, stateMainThreadId=mainThreadId, ..} ses SQL.ServerError{} -> when (Error.status (Error.PgError False err) >= HTTP.status500) $ observer $ QueryErrorCodeHighObs err - SQL.SessionUsageError (SQL.QueryError _ _ (SQL.ClientError _)) -> - pure () + err@(SQL.SessionUsageError (SQL.QueryError _ _ (SQL.ClientError _))) -> + -- An error on the client-side, usually indicates problems wth connection + observer $ QueryErrorCodeHighObs err ) return res diff --git a/test/io/test_io.py b/test/io/test_io.py index 7d06ffcaa6..ff5619fb97 100644 --- a/test/io/test_io.py +++ b/test/io/test_io.py @@ -1696,3 +1696,22 @@ def test_jwt_cache_purges_expired_entries(defaultenv): response = postgrest.session.get("/authors_only", headers=hdrs3) assert response.status_code == 200 + + +def test_pgrst_log_503_client_error_to_stderr(defaultenv): + "PostgREST should log 503 errors to stderr" + + env = { + **defaultenv, + "PGAPPNAME": "test-io", + } + + with run(env=env) as postgrest: + + postgrest.session.get("/rpc/terminate_pgrst?appname=test-io") + + output = postgrest.read_stdout(nlines=6) + + log_message = '{"code":"PGRST001","details":"no connection to the server\\n","hint":null,"message":"Database client error. Retrying the connection."}\n' + + assert any(log_message in line for line in output)