Introduce application error

This commit is contained in:
Paul Brinkmeier
2025-08-25 08:52:34 +02:00
parent 4c5ff2f319
commit c5846599ea
7 changed files with 65 additions and 48 deletions
+3 -6
View File
@@ -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
+11
View File
@@ -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
View File
@@ -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
View File
@@ -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