-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: add Proxy-Status header for better error response #3955
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
2609059
to
6427f86
Compare
6427f86
to
95a0446
Compare
Loadtest looking good, no perf loss https://github.com/PostgREST/postgrest/actions/runs/13937206620?pr=3955 |
@steve-chavez @laurenceisla Currently, we assign error codes to errors at the time of generating the error json: postgrest/src/PostgREST/Error.hs Lines 227 to 231 in 3c0baec
The issue with that design is that we can't reuse the same assignment when generating So, I think we need a refactor here where we assign codes somewhere else. I am thinking of refactoring in this way: class PgrstError a where
status :: a -> HTTP.Status
headers :: a -> [Header]
-- this will also ensure all error have these attributes at a typeclass level
code :: a -> ErrorCode
message :: a -> Text
details :: a -> Maybe JSON.Value
hint :: a -> Maybe JSON.Value
errorPayload :: a -> LByteString
errorPayload = JSON.encode
errorResponseFor :: a -> Response
errorResponseFor err =
let baseHeader = MediaType.toContentType MTApplicationJSON in
responseLBS (status err) (baseHeader : headers err) $ errorPayload err
-- then we can reuse these when generating the error json
instance JSON.ToJSON PgrstError where
toJSON err = toJsonPgrstError (code err) (message err) (details err) (hint err)
|
@taimoorzaeem LGTM, please go ahead. |
2833c87
to
2e48175
Compare
@steve-chavez Is returning |
It should be only on error responses. Given your refactors on #3987, I think it's not necessary to have so much extra testing since we already prove in code that all error responses will contain a code? Given that, only some tests would suffice. Say just one proxy-status error for each different group? (not adamant on this if there are some error groups that are hard to repro, like the X00 ones). |
2e48175
to
128ce08
Compare
@taimoorzaeem Great work! 🚀 We should document this. Maybe on https://docs.postgrest.org/en/v12/references/observability.html#traces, after trace header. |
This is awesome. I just played it with it and it even works with custom errors:
@taimoorzaeem How about adding tests for these two cases, to ensure they don't break? (fixtures already included, so it should be easy) Also, I've noticed that the docs for Proxy-Status would better fit in the errors page too. It should mention that it will contain error codes from PostgREST, PostgreSQL and Custom Errors (no need for snippets on each one, just keeping the one about statement_timeout is good). We can still keep a reference (link) to it in the Observability page, same place; for more visilibity. |
7d9c70f
to
25d759a
Compare
25d759a
to
cd3c000
Compare
With this feature, PostgREST responds with error code in the
Proxy-Status
header.Closes #2967.