aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2017-08-10 23:38:50 +0200
committerTeddy Wing2017-08-10 23:50:26 +0200
commit465f32c216bd9a9bd695f0a2f0f3a08369c51e9a (patch)
treea6dc3a96f5cd84bc383f111111c0df52156f842b
parenta13026cbdf42ebc159b202588d10dffba47c1c1b (diff)
downloadsorbot-465f32c216bd9a9bd695f0a2f0f3a08369c51e9a.tar.bz2
Try to set up error handling for GitHub Commit plugin
This doesn't really work, but I wanted to hold onto this step. I've been reading about error handling, notably this article: http://www.randomhacks.net/2007/03/10/haskell-8-ways-to-report-errors/ I kind of like the idea of just using `fail`, and being able to have that set up some polymorphism to handle `Maybe`, `Either`, and `IO` situations, but today read some literature that discouraged using fail, since not all monads implement it. Anyway, this code doesn't print out the error string like I intended it to, so I guess I'll have to use `Either` instead.
-rw-r--r--src/Lib.hs10
-rw-r--r--src/Plugin/GitHubCommit.hs11
2 files changed, 16 insertions, 5 deletions
diff --git a/src/Lib.hs b/src/Lib.hs
index b98c18d..3b49136 100644
--- a/src/Lib.hs
+++ b/src/Lib.hs
@@ -2,6 +2,11 @@ module Lib
( someFunc
) where
+import Control.Exception (catch)
+-- import System.Environment
+-- import System.IO
+-- import System.IO.Error
+
import Database.SQLite.Simple
import Message
@@ -17,5 +22,8 @@ someFunc = do
Just plugin = matchPlugin message
dbConn <- open "db/sorbot_development.sqlite3"
response <- performPlugin plugin message dbConn
- putStrLn response
+ putStrLn response `catch` handleError
close dbConn
+
+handleError :: IOError -> IO ()
+handleError err = putStrLn $ show err
diff --git a/src/Plugin/GitHubCommit.hs b/src/Plugin/GitHubCommit.hs
index cac74a4..8c11a12 100644
--- a/src/Plugin/GitHubCommit.hs
+++ b/src/Plugin/GitHubCommit.hs
@@ -24,12 +24,15 @@ gitHubCommitAction message dbConn = do
\ LIMIT 1"
(Only (M.channel message))
:: IO [RepoUrlRow]
- return $ response rs
+ -- return $ response rs
+ respond rs
where
- response [] = ""
- response ((RepoUrlRow r):rs) =
- r ++ "/commits/" ++ M.text message =~ matchRegex gitHubCommit
+ respond [] = fail "I couldn't find a repo URL for this channel. \
+ \ Try `git remote set origin REPO_URL`"
+ respond ((RepoUrlRow r):rs) =
+ return r ++ "/commits/" ++ M.text message =~ matchRegex gitHubCommit
-- TODO: Make an Either type for plugins to return errors
+-- if empty query result, return an empty error, otherwise return the string
type Id = Int