Skip to content

Commit

Permalink
[#500] Trails: Error when the version is not supported.
Browse files Browse the repository at this point in the history
  • Loading branch information
a-stacey committed Aug 14, 2019
1 parent 578b662 commit db1e06c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
12 changes: 12 additions & 0 deletions projects/trails/src/Mirza/Trails/Handlers/Trails.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import Database.Beam.Query hiding (time)
import Servant

import Control.Lens.Extras
import Control.Lens.Type (AReview)
import Control.Monad.Error.Lens (throwing_)

import Control.Monad.Identity

Expand Down Expand Up @@ -138,6 +140,13 @@ addTrail trail = do
pure NoContent


throwing_If :: MonadError e m => Control.Lens.Type.AReview e () -> Bool -> m ()
throwing_If x result = if result then
throwing_ x
else
pure ()


addEntryQuery :: (AsTrailsServiceError err)
=> [TrailEntry] -> DB context err ()
addEntryQuery entriesRaw = do
Expand All @@ -146,6 +155,9 @@ addEntryQuery entriesRaw = do
-- Note: We only need to check if the signature exists (and not that the full event contents match) since the signature guarantees that they events are the same.
let newEntries = filter (\entry -> not $ elem (trailEntrySignature entry) $ trailEntrySignature <$> existingEntries) entriesRaw

-- Check that the event versions are all valid.
throwing_If _InvalidEntryVersionTSE $ not $ all (== 1) $ trailEntryVersion <$> newEntries

let entries = trailEntryToEntriesT <$> newEntries
let previous = concat $ trailEntryToParentsT <$> newEntries
_ <- pg $ runInsertReturningList $ insert (_entries trailsDB)
Expand Down
1 change: 1 addition & 0 deletions projects/trails/src/Mirza/Trails/Service.hs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ trailsErrorToHttpError trailsError =
(DBErrorTE _) -> unexpectedError trailsError
(SignatureNotFoundTSE) -> httpError err404 "A trail with a matching signature was not found."
(EventIdNotFoundTSE) -> httpError err404 "A trail with the matching EventId was not found."
(InvalidEntryVersionTSE) -> httpError err400 "Only version 1 trail entries are currently supported by this service."
(UnmatchedUniqueViolationTSE _) -> unexpectedError trailsError

-- | A generic internal server error has occured. We include no more information in the result returned to the user to
Expand Down
1 change: 1 addition & 0 deletions projects/trails/src/Mirza/Trails/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ data TrailsServiceError
= DBErrorTE SqlError
| SignatureNotFoundTSE
| EventIdNotFoundTSE
| InvalidEntryVersionTSE
| UnmatchedUniqueViolationTSE SqlError
deriving (Show)
$(makeClassyPrisms ''TrailsServiceError)
Expand Down

0 comments on commit db1e06c

Please sign in to comment.