Introduce application error
This commit is contained in:
+3
-6
@@ -28,12 +28,9 @@ import GHC.Generics (Generic)
|
||||
|
||||
import qualified Database.PostgreSQL.Opium as Opium
|
||||
|
||||
newtype DB = DB (Pool (Either Opium.ConnectionError Opium.Connection))
|
||||
import Yore.Error (Error (..))
|
||||
|
||||
data Error
|
||||
= ConnectionError Opium.ConnectionError
|
||||
| Error Opium.Error
|
||||
deriving (Show)
|
||||
newtype DB = DB (Pool (Either Opium.ConnectionError Opium.Connection))
|
||||
|
||||
initDB :: Text -> IO DB
|
||||
initDB connString = do
|
||||
@@ -54,7 +51,7 @@ withConn (DB connPool) f =
|
||||
Left connectionError ->
|
||||
pure $ Left $ ConnectionError connectionError
|
||||
Right conn ->
|
||||
either (Left . Error) Right <$> f conn
|
||||
either (Left . DBError) Right <$> f conn
|
||||
|
||||
data DayIndex = DayIndex
|
||||
{ day_index_id :: Int
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
module Yore.Error (Error (..)) where
|
||||
|
||||
import Data.Text (Text)
|
||||
|
||||
import qualified Database.PostgreSQL.Opium as Opium
|
||||
|
||||
data Error
|
||||
= ConnectionError Opium.ConnectionError
|
||||
| DBError Opium.Error
|
||||
| GenericError Text
|
||||
deriving (Show)
|
||||
+2
-2
@@ -34,9 +34,9 @@ getToday = do
|
||||
print now
|
||||
let currentDay = localDay $ zonedTimeToLocalTime now
|
||||
case addYears (-100) currentDay of
|
||||
Just then_ ->
|
||||
Right then_ ->
|
||||
getIssuesByDay then_
|
||||
Nothing ->
|
||||
Left _ ->
|
||||
pure []
|
||||
|
||||
indexDay :: Day -> IO ()
|
||||
|
||||
+7
-3
@@ -1,5 +1,6 @@
|
||||
module Yore.Time (addYears, getCurrentDay, getSecondsUntilMidnight) where
|
||||
|
||||
import Data.Text (Text)
|
||||
import Data.Time
|
||||
( Day
|
||||
, addGregorianYearsClip
|
||||
@@ -11,13 +12,16 @@ import Data.Time
|
||||
, toGregorian
|
||||
, zonedTimeToLocalTime
|
||||
)
|
||||
import Text.Printf (printf)
|
||||
|
||||
addYears :: Integer -> Day -> Maybe Day
|
||||
import qualified Data.Text as Text
|
||||
|
||||
addYears :: Integer -> Day -> Either Text Day
|
||||
addYears yearOffset date
|
||||
| isFebruary29th && not (isLeapYear year') =
|
||||
Nothing
|
||||
Left $ Text.pack $ printf "year %d is not a leap year"
|
||||
| otherwise =
|
||||
Just $ addGregorianYearsClip yearOffset date
|
||||
Right $ addGregorianYearsClip yearOffset date
|
||||
where
|
||||
(year, month, day) = toGregorian date
|
||||
year' = year + yearOffset
|
||||
|
||||
Reference in New Issue
Block a user