-
Notifications
You must be signed in to change notification settings - Fork 43
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
Add persistence exception handling section #7
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
eaa7e7a
Add section for Handling exception in persistence
psibi 0306121
Link the page in cookbook
psibi 0d34acd
Fix code import
psibi 5dc8c87
Add explanation that it fails for postgresql
psibi 7026baa
Example code for handling exception in postgresql
psibi 1e06e07
Add comment
psibi 2e9e6df
capitalize
psibi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# Handling exception in persistence | ||
|
||
Say, you have a code like this which throws exception: | ||
|
||
``` haskell | ||
share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase| | ||
User | ||
age Int | ||
UniqueAge age | ||
deriving Show | ||
|] | ||
|
||
getUser :: MonadIO m => ReaderT SqlBackend m [Entity User] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor recommendation: |
||
getUser = selectList [] [] | ||
|
||
insertJane :: MonadIO m => ReaderT SqlBackend m () | ||
insertJane = insert_ $ User 40 | ||
|
||
sqliteTest :: IO () | ||
sqliteTest = runSqlite ":memory:" $ do | ||
runMigration migrateAll | ||
|
||
insertJane | ||
insertJane | ||
|
||
users <- getUser | ||
liftIO $ print (users :: [Entity User]) | ||
``` | ||
|
||
The above code will throw exception when executed because the uniqueness constraint is being violated: | ||
|
||
``` | ||
$ ./sqlite-code | ||
|
||
Migrating: CREATE TABLE "user"("id" INTEGER PRIMARY KEY,"age" INTEGER NOT NULL,CONSTRAINT "unique_age" UNIQUE ("age")) | ||
persistent-try-bugs: SQLite3 returned ErrorConstraint while attempting to perform step. | ||
``` | ||
|
||
You can use the [exceptions](https://hackage.haskell.org/package/exceptions) package to handle exceptions. Have the appropriate imports: | ||
|
||
``` | ||
import Control.Exception (SomeException) | ||
import Control.Monad.Catch | ||
``` | ||
|
||
And the rest of the code is like this: | ||
|
||
``` haskell | ||
share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase| | ||
User | ||
age Int | ||
UniqueAge age | ||
deriving Show | ||
|] | ||
|
||
getUser :: MonadIO m => ReaderT SqlBackend m [Entity User] | ||
getUser = selectList [] [] | ||
|
||
insertJane :: (MonadCatch m, MonadIO m) => ReaderT SqlBackend m () | ||
insertJane = (insert_ (User 40)) `catch` (\(SomeException e) -> return ()) | ||
|
||
sqliteTest :: IO () | ||
sqliteTest = runSqlite ":memory:" $ do | ||
runMigration migrateAll | ||
|
||
insertJane | ||
insertJane | ||
|
||
users <- getUser | ||
liftIO $ print (users :: [Entity User]) | ||
``` | ||
|
||
And this time, it will execute without crashing: | ||
|
||
``` | ||
$ ./sqlite-code | ||
|
||
Migrating: CREATE TABLE "user"("id" INTEGER PRIMARY KEY,"age" INTEGER NOT NULL,CONSTRAINT "unique_age" UNIQUE ("age")) | ||
[Entity {entityKey = UserKey {unUserKey = SqlBackendKey {unSqlBackendKey = 1}}, entityVal = User {userAge = 40}}] | ||
``` | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps "throws an exception"?