From 8980b094191b9660bdf5c0c66d8ae48b01220bdf Mon Sep 17 00:00:00 2001 From: Wolfgang Walther Date: Thu, 27 Jan 2022 22:17:27 +0100 Subject: [PATCH] refactor: Sort error types Signed-off-by: Wolfgang Walther --- src/PostgREST/Error.hs | 70 ++++++++++++++--------------- src/PostgREST/Request/ApiRequest.hs | 15 ++++--- src/PostgREST/Request/Types.hs | 18 ++++---- 3 files changed, 54 insertions(+), 49 deletions(-) diff --git a/src/PostgREST/Error.hs b/src/PostgREST/Error.hs index 6a2eb9304..761e17a7b 100644 --- a/src/PostgREST/Error.hs +++ b/src/PostgREST/Error.hs @@ -55,20 +55,20 @@ class (JSON.ToJSON a) => PgrstError a where errorResponseFor err = responseLBS (status err) (headers err) $ errorPayload err instance PgrstError ApiRequestError where - status InvalidRange = HTTP.status416 - status InvalidFilters = HTTP.status405 - status (InvalidBody _) = HTTP.status400 - status UnsupportedVerb = HTTP.status405 - status ActionInappropriate = HTTP.status405 - status (ParseRequestError _ _) = HTTP.status400 - status (QueryParamError _) = HTTP.status400 - status NoRelBetween{} = HTTP.status400 - status AmbiguousRelBetween{} = HTTP.status300 - status (AmbiguousRpc _) = HTTP.status300 - status NoRpc{} = HTTP.status404 - status (UnacceptableSchema _) = HTTP.status406 - status (ContentTypeError _) = HTTP.status415 - status (NotEmbedded _) = HTTP.status400 + status ActionInappropriate = HTTP.status405 + status AmbiguousRelBetween{} = HTTP.status300 + status AmbiguousRpc{} = HTTP.status300 + status ContentTypeError{} = HTTP.status415 + status InvalidBody{} = HTTP.status400 + status InvalidFilters = HTTP.status405 + status InvalidRange = HTTP.status416 + status NoRelBetween{} = HTTP.status400 + status NoRpc{} = HTTP.status404 + status NotEmbedded{} = HTTP.status400 + status ParseRequestError{} = HTTP.status400 + status QueryParamError{} = HTTP.status400 + status UnacceptableSchema{} = HTTP.status406 + status UnsupportedVerb = HTTP.status405 headers _ = [ContentType.toHeader CTApplicationJSON] @@ -270,41 +270,41 @@ checkIsFatal _ = Nothing data Error - = GucHeadersError - | GucStatusError + = ApiRequestError ApiRequestError | BinaryFieldError ContentType + | GucHeadersError + | GucStatusError + | JwtTokenInvalid Text + | JwtTokenMissing + | JwtTokenRequired | NoSchemaCacheError + | NotFound + | PgErr PgError | PutMatchingPkError | PutRangeNotAllowedError - | JwtTokenMissing - | JwtTokenInvalid Text - | JwtTokenRequired | SingularityError Integer - | NotFound - | ApiRequestError ApiRequestError - | PgErr PgError instance PgrstError Error where + status (ApiRequestError err) = status err + status BinaryFieldError{} = HTTP.status406 status GucHeadersError = HTTP.status500 status GucStatusError = HTTP.status500 - status (BinaryFieldError _) = HTTP.status406 - status NoSchemaCacheError = HTTP.status503 - status PutMatchingPkError = HTTP.status400 - status PutRangeNotAllowedError = HTTP.status400 + status JwtTokenInvalid{} = HTTP.unauthorized401 status JwtTokenMissing = HTTP.status500 - status (JwtTokenInvalid _) = HTTP.unauthorized401 status JwtTokenRequired = HTTP.unauthorized401 - status (SingularityError _) = HTTP.status406 + status NoSchemaCacheError = HTTP.status503 status NotFound = HTTP.status404 status (PgErr err) = status err - status (ApiRequestError err) = status err + status PutMatchingPkError = HTTP.status400 + status PutRangeNotAllowedError = HTTP.status400 + status SingularityError{} = HTTP.status406 - headers (SingularityError _) = [ContentType.toHeader CTSingularJSON] - headers (JwtTokenInvalid m) = [ContentType.toHeader CTApplicationJSON, invalidTokenHeader m] - headers JwtTokenRequired = [ContentType.toHeader CTApplicationJSON, requiredTokenHeader] - headers (PgErr err) = headers err - headers (ApiRequestError err) = headers err - headers _ = [ContentType.toHeader CTApplicationJSON] + headers (ApiRequestError err) = headers err + headers (JwtTokenInvalid m) = [ContentType.toHeader CTApplicationJSON, invalidTokenHeader m] + headers JwtTokenRequired = [ContentType.toHeader CTApplicationJSON, requiredTokenHeader] + headers (PgErr err) = headers err + headers SingularityError{} = [ContentType.toHeader CTSingularJSON] + headers _ = [ContentType.toHeader CTApplicationJSON] instance JSON.ToJSON Error where toJSON GucHeadersError = JSON.object [ diff --git a/src/PostgREST/Request/ApiRequest.hs b/src/PostgREST/Request/ApiRequest.hs index 5bbf6b5c5..8f44e8a9b 100644 --- a/src/PostgREST/Request/ApiRequest.hs +++ b/src/PostgREST/Request/ApiRequest.hs @@ -83,11 +83,16 @@ data Payload data InvokeMethod = InvHead | InvGet | InvPost deriving Eq -- | Types of things a user wants to do to tables/views/procs -data Action = ActionCreate | ActionRead{isHead :: Bool} - | ActionUpdate | ActionDelete - | ActionSingleUpsert | ActionInvoke InvokeMethod - | ActionInfo | ActionInspect{isHead :: Bool} - deriving Eq +data Action + = ActionCreate + | ActionRead {isHead :: Bool} + | ActionUpdate + | ActionDelete + | ActionSingleUpsert + | ActionInvoke InvokeMethod + | ActionInfo + | ActionInspect {isHead :: Bool} + deriving Eq -- | The path info that will be mapped to a target (used to handle validations and errors before defining the Target) data Path = PathInfo diff --git a/src/PostgREST/Request/Types.hs b/src/PostgREST/Request/Types.hs index 38a58b452..f76fc0b8b 100644 --- a/src/PostgREST/Request/Types.hs +++ b/src/PostgREST/Request/Types.hs @@ -59,19 +59,19 @@ import Protolude data ApiRequestError = ActionInappropriate - | InvalidRange - | InvalidBody ByteString - | ParseRequestError Text Text - | QueryParamError QPError - | NoRelBetween Text Text Text | AmbiguousRelBetween Text Text [Relationship] | AmbiguousRpc [ProcDescription] - | NoRpc Text Text [Text] Bool ContentType Bool - | InvalidFilters - | UnacceptableSchema [Text] | ContentTypeError [ByteString] - | UnsupportedVerb -- Unreachable? + | InvalidBody ByteString + | InvalidFilters + | InvalidRange + | NoRelBetween Text Text Text + | NoRpc Text Text [Text] Bool ContentType Bool | NotEmbedded Text + | ParseRequestError Text Text + | QueryParamError QPError + | UnacceptableSchema [Text] + | UnsupportedVerb -- Unreachable? data QPError = QPError Text Text