title: Error context in error handling in Scala
body: <p>Suppose I need to call a remote JSON/HTTP service. I make a JSON request, send it by HTTP to the server, and receive and parse the JSON response. </p>
  
  <p>Suppose I have data type <code>MyError</code>for errors and all my functions return <code>Either[MyError, R]</code> </p>
  
  <pre><code>type Result[A] = Either[MyError, A]    
  
  def makeJsonRequest(requestData: RequestData): Result[String] = ...
  
  def invoke(url: URL, jsonRequest: String): Result[String] = ...
  
  def parseJsonResponse(jsonResponse: String): Result[ResponseData] = ...
  </code></pre>
  
  <p>I can combine them to write a new function:</p>
  
  <pre><code>def invokeService(url: URL, requestData: RequestData) Result[ResponseData] = for {
     jsonRequest &lt;- makeJsonRequest(requestData).right
     jsonResponse &lt;- invoke(url, req).right 
     responseData &lt;- parseJsonResponse(jsonResponse).right
  } yield responseData
  </code></pre>
  
  <p>Now what if <code>parseJsonResponse</code> fails ?  </p>
  
  <p>I get the error but I need also the whole <em>context</em>. That is I need <code>url</code>, <code>requestData</code>, and <code>jsonRequest</code>. How would you suggest me do it ?</p>