Skip to content

Commit 055efe7

Browse files
taimoorzaeemsteve-chavez
authored andcommitted
refactor: move Proxy-Status header to error type class
Headers that apply to all errors should all be in one place. Signed-off-by: Taimoor Zaeem <taimoorzaeem@gmail.com>
1 parent ca96328 commit 055efe7

1 file changed

Lines changed: 7 additions & 9 deletions

File tree

src/PostgREST/Error.hs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ class (ErrorBody a, JSON.ToJSON a) => PgrstError a where
6565
let
6666
baseHeader = MediaType.toContentType MTApplicationJSON
6767
cLHeader body = (,) "Content-Length" (show $ LBS.length body) :: Header
68+
pSHeader code' = ("Proxy-Status", "PostgREST; error=" <> T.encodeUtf8 code')
6869
in
69-
responseLBS (status err) (baseHeader : cLHeader (errorPayload err) : headers err) $ errorPayload err
70+
responseLBS (status err) (baseHeader : cLHeader (errorPayload err) : pSHeader (code err) : headers err) $ errorPayload err
7071

7172
class ErrorBody a where
7273
code :: a -> Text
@@ -505,9 +506,6 @@ instance PgrstError PgError where
505506
then [("WWW-Authenticate", "Bearer") :: Header]
506507
else mempty
507508

508-
proxyStatusHeader :: Text -> Header
509-
proxyStatusHeader code' = ("Proxy-Status", "PostgREST; error=" <> T.encodeUtf8 code')
510-
511509
instance JSON.ToJSON PgError where
512510
toJSON (PgError _ usageError) = toJsonPgrstError
513511
(code usageError) (message usageError) (details usageError) (hint usageError)
@@ -683,11 +681,11 @@ instance PgrstError Error where
683681
status NoSchemaCacheError = HTTP.status503
684682
status (PgErr err) = status err
685683

686-
headers (ApiRequestError err) = proxyStatusHeader (code err) : headers err
687-
headers (SchemaCacheErr err) = proxyStatusHeader (code err) : headers err
688-
headers (JwtErr err) = proxyStatusHeader (code err) : headers err
689-
headers (PgErr err) = proxyStatusHeader (code err) : headers err
690-
headers err@NoSchemaCacheError = proxyStatusHeader (code err) : mempty
684+
headers (ApiRequestError err) = headers err
685+
headers (SchemaCacheErr err) = headers err
686+
headers (JwtErr err) = headers err
687+
headers (PgErr err) = headers err
688+
headers NoSchemaCacheError = mempty
691689

692690
instance JSON.ToJSON Error where
693691
toJSON err = toJsonPgrstError

0 commit comments

Comments
 (0)