refactor: DRY application/json on Error.hs
Small refactor to help with: https://github.com/PostgREST/postgrest/issues/2901
This commit is contained in:
committed by
Steve Chavez
parent
0703f27d2b
commit
3dd292be46
+10
-8
@@ -57,7 +57,9 @@ class (JSON.ToJSON a) => PgrstError a where
|
|||||||
errorPayload = JSON.encode
|
errorPayload = JSON.encode
|
||||||
|
|
||||||
errorResponseFor :: a -> Response
|
errorResponseFor :: a -> Response
|
||||||
errorResponseFor err = responseLBS (status err) (headers err) $ errorPayload err
|
errorResponseFor err =
|
||||||
|
let baseHeader = MediaType.toContentType MTApplicationJSON in
|
||||||
|
responseLBS (status err) (baseHeader : headers err) $ errorPayload err
|
||||||
|
|
||||||
instance PgrstError ApiRequestError where
|
instance PgrstError ApiRequestError where
|
||||||
status AmbiguousRelBetween{} = HTTP.status300
|
status AmbiguousRelBetween{} = HTTP.status300
|
||||||
@@ -84,7 +86,7 @@ instance PgrstError ApiRequestError where
|
|||||||
status LimitNoOrderError = HTTP.status400
|
status LimitNoOrderError = HTTP.status400
|
||||||
status ColumnNotFound{} = HTTP.status400
|
status ColumnNotFound{} = HTTP.status400
|
||||||
|
|
||||||
headers _ = [MediaType.toContentType MTApplicationJSON]
|
headers _ = mempty
|
||||||
|
|
||||||
instance JSON.ToJSON ApiRequestError where
|
instance JSON.ToJSON ApiRequestError where
|
||||||
toJSON (QueryParamError (QPError message details)) = JSON.object [
|
toJSON (QueryParamError (QPError message details)) = JSON.object [
|
||||||
@@ -376,8 +378,8 @@ instance PgrstError PgError where
|
|||||||
|
|
||||||
headers err =
|
headers err =
|
||||||
if status err == HTTP.status401
|
if status err == HTTP.status401
|
||||||
then [MediaType.toContentType MTApplicationJSON, ("WWW-Authenticate", "Bearer") :: Header]
|
then [("WWW-Authenticate", "Bearer") :: Header]
|
||||||
else [MediaType.toContentType MTApplicationJSON]
|
else mempty
|
||||||
|
|
||||||
instance JSON.ToJSON PgError where
|
instance JSON.ToJSON PgError where
|
||||||
toJSON (PgError _ usageError) = JSON.toJSON usageError
|
toJSON (PgError _ usageError) = JSON.toJSON usageError
|
||||||
@@ -506,11 +508,11 @@ instance PgrstError Error where
|
|||||||
status JSONParseError = HTTP.status500
|
status JSONParseError = HTTP.status500
|
||||||
|
|
||||||
headers (ApiRequestError err) = headers err
|
headers (ApiRequestError err) = headers err
|
||||||
headers (JwtTokenInvalid m) = [MediaType.toContentType MTApplicationJSON, invalidTokenHeader m]
|
headers (JwtTokenInvalid m) = [invalidTokenHeader m]
|
||||||
headers JwtTokenRequired = [MediaType.toContentType MTApplicationJSON, requiredTokenHeader]
|
headers JwtTokenRequired = [requiredTokenHeader]
|
||||||
headers (PgErr err) = headers err
|
headers (PgErr err) = headers err
|
||||||
headers SingularityError{} = [MediaType.toContentType (MTSingularJSON False)]
|
headers SingularityError{} = [MediaType.toContentType $ MTSingularJSON False]
|
||||||
headers _ = [MediaType.toContentType MTApplicationJSON]
|
headers _ = mempty
|
||||||
|
|
||||||
instance JSON.ToJSON Error where
|
instance JSON.ToJSON Error where
|
||||||
toJSON NoSchemaCacheError = JSON.object [
|
toJSON NoSchemaCacheError = JSON.object [
|
||||||
|
|||||||
Reference in New Issue
Block a user