Skip to content

Commit

Permalink
Move from fail to SMTPException everywhere in the module
Browse files Browse the repository at this point in the history
  • Loading branch information
qnikst committed Dec 27, 2020
1 parent 27f8e4c commit b2f3ff0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
14 changes: 9 additions & 5 deletions src/Network/HaskellNet/SMTP.hs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ module Network.HaskellNet.SMTP
, connectStream
, closeSMTP
, gracefullyCloseSMTP
, SMTPException(..)
) where

import Network.HaskellNet.BSStream
Expand Down Expand Up @@ -174,13 +175,16 @@ connectSMTP :: String -- ^ name of the server
-> IO SMTPConnection
connectSMTP = flip connectSMTPPort 25

-- | create SMTPConnection from already connected Stream
connectStream :: BSStream -> IO SMTPConnection
-- | Create SMTPConnection from already connected Stream
--
-- Throws @CantConnect :: SMTPException@ in case if got illegal
-- greeting.
connectStream :: HasCallStack => BSStream -> IO SMTPConnection
connectStream st =
do (code1, _) <- parseResponse st
unless (code1 == 220) $
do bsClose st
fail "cannot connect to the server"
unless (code1 == 220) $ do
bsClose st
throwIO $ UnexpectedGreeting code1
senderHost <- T.pack <$> getHostName
msg <- tryCommand (SMTPC st []) (EHLO senderHost) 3 [250]
return (SMTPC st (tail $ BS.lines msg))
Expand Down
5 changes: 4 additions & 1 deletion src/Network/HaskellNet/SMTP/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ data SMTPException
| AuthNegotiationFailed ReplyCode BS.ByteString
-- | Can't send email because no recipients were specified.
| NoRecipients Mail
-- | Received an unexpected greeting from the server.
| UnexpectedGreeting ReplyCode
deriving (Show)
deriving (Typeable)

Expand All @@ -178,6 +180,8 @@ instance Exception SMTPException where
"Authentication failed. code: " ++ show code ++ ", msg: " ++ BS.unpack msg
displayException (NoRecipients _mail) =
"No recipients were specified"
displayException (UnexpectedGreeting code) =
"Expected greeting from the server, but got: " <> show code


-- | Safe wrapper for running a client command over the SMTP
Expand Down Expand Up @@ -341,7 +345,6 @@ sendMailData sender receivers dat conn = do
sendAndCheck (DATA dat)
return ()
where
-- Try the command once and @fail@ if the response isn't 250.
sendAndCheck cmd = tryCommand conn cmd 1 [250, 251]

-- | Just a crlf constant.
Expand Down

0 comments on commit b2f3ff0

Please sign in to comment.