Instantly share code, notes, and snippets.

View go-2-error-handling-draft-design-issues.md

go 2 has laid out the problem of error handling (Please read first). A wiki page was suggested for feedback. The feedback overwhelmingly suggested ways to not use scoped handlers, essentially creating alternative proposals

It is amazing to see the error handling problem being properly addressed. The existing proposal is good, but I think we can iterate to make it better. I think we can simplify it and address these issues:

  • Defining handlers as a stack is presumptious: it forces handlers to be side-effecting, and ruins interop with defer.
  • return statements are placed in handlers, ruining composition.
  • using check as a prefix function reduces code clarity.
  • a new anonymous function syntax is introduced into the proposal, but only avail
View error-inspection-composition.md

The current error proposal for inspection is great. I have two minor suggestions:

  • rename Wrapper to Unwrapper
  • change the function name from "As" to "Find"

The words "As" someties indicates casting, but we are just doing a retrieval.

Handling error groups

The current error inspection proposal laments not having a way to deal with error groups.

View go2-error-handling-functions-with-check.md

Its amazing to see the error handling problem being properly addressed. The existing proposal is good, but I think we can iterate to make it better. I think we can simplify it and address these issues:

  • Defining handlers as a stack is presumptious: it forces handlers to be side-effecting, and ruins interop with defer.
  • return statements are placed in handlers, ruining composition.
  • using check as a prefix function reduces code clarity.
  • a new anonymous function syntax is introduced into the proposal, but only available to handle.
  • the proposal introduces two keywords, but one may suffice

It's useful to take a step back and clearly state what we are trying to do:

View StaticRewrite.hs
-- something like this can be used to make friendlier urls for the static pages
-- import Network.Wai.Middleware.Rewrite (rewritePure)
-- rewritePure rewriteConvert
rewriteConvert :: [Text] -> H.RequestHeaders -> [Text]
rewriteConvert pieces _ = staticRewrite pieces
where
staticRewrite :: [Text] -> [Text]
staticRewrite [] = homePage
staticRewrite ("static":"html":_) = homePage -- prevent direct access, not really necessary
staticRewrite route@("pages":_) | ".html" `T.isSuffixOf` last route = staticHtml ++ route
View combine.html.hamlet
$with scripts <- ["Prelude", "Config", "KV"]
^{combineScripts "app" scripts}
View static-pages.hs
import Prelude
import Yesod.Routes.Parse (staticPageRoutes)
import Yesod hiding (Request)
import Text.Hamlet
import Network.Wai
import Network.Wai.Test
import Data.Conduit (runResourceT)
import Blaze.ByteString.Builder (toLazyByteString)
import qualified Data.ByteString.Char8 as BS8
import qualified Data.ByteString.Lazy as LBS
View External.hs
module External where
import Language.Haskell.TH.Syntax
import Language.Haskell.TH.Lib
loadStringFromFile :: Q Exp
loadStringFromFile = do
let externalDependency = "external.txt"
qAddDependentFile externalDependency
s <- qRunIO $ readFile externalDependency
View mongoApp.hs
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
import Yesod
import Database.Persist
import Database.Persist.MongoDB
View pid-wrap
#!/bin/bash
DIR="$( cd "$( dirname "$0" )" && pwd )"
cd $DIR/..
name=`basename "$2"`
case $1 in
'start')
echo $ > ./tmp/$name.pid
exec $2 2>&1 1> ./log/$name.log
View memoized methods
def self.cached_method(meth)
eval <<-CODE
def #{meth}_with_cache
return @#{meth} if @#{meth}
result = #{meth}_without_cache
@#{meth} ||= result
end
CODE
alias_method_chain meth, :cache
end