title: How to handle the chained Except?
body: <p>I have a question about chained <code>Control.Monad.Except</code>. To make the question clear, consider the following example:</p>
  <p>Firstly, we define the errors as below:</p>
  <pre><code>import Control.Monad.Except
  import Prelude hiding (lex)
  
  data BufError   = BufClosedError      deriving Show
  data LexError   = NoSuchTokenError    deriving Show
  data ParseError = MismatchSymbolError deriving Show
  </code></pre>
  <p>the <code>BufError</code> come from reading buffer</p>
  <pre><code>readBuf::Except BufError a
  readBuf = throwError BufClosedError
  </code></pre>
  <p>and when it occurs, such error embedded in the exception of <code>lex</code></p>
  <pre><code>lex::ExceptT LexError (Except BufError) a
  lex = lift $ readBuf
  </code></pre>
  <p>and a parser can handle any sources and lift the errors occurred from the source to <code>ExceptT ParseError m a</code>:</p>
  <pre><code>parse::Monad m=&gt;m a-&gt;ExceptT ParseError m a
  parse source = lift source
  </code></pre>
  <p>and now, when run the parser with different source, it need different handlers to extract the result from the <code>ExceptT</code> as</p>
  <pre><code>handleError3 = runExceptT . runExceptT . runExceptT
  handleError2 = runExceptT . runExceptT
  
  runParse3 = handleError3 $ parse lex
  runParse2 = handleError2 $ parse readBuf
  </code></pre>
  <p>The question is how to combine <code>handleError3</code> and <code>handleError2</code> to one generic function <code>handlerError</code> such that we can handle <code>parse lex</code> and <code>parse readBuf</code> with single function as:</p>
  <pre><code>runParse = handlerError . parse
  
  runParse lex
  runParse readBuf
  </code></pre>
  <p>Furthermore, If the chained <code>ExceptT</code> with &quot;variable length&quot; such as</p>
  <pre><code>ExceptT e1 (ExceptT e2 (....(Except e'))) a
  </code></pre>
  <p>Is it possible to define a function to handle it as</p>
  <pre><code>handler = runExceptT . runExceptT ... . runExceptT
  </code></pre>
  <p>with correct number of <code>runExceptT</code> corresponding to each <code>ExceptT</code>?</p>