title: Go Gorilla panic handler to respond with custom status
body: <p>In Gorilla, using <a href="http://www.gorillatoolkit.org/pkg/handlers#RecoveryHandler" rel="nofollow">RecoveryHandler</a> we could suppress the panics. However is there a handler or a library method to respond with a specific Http status code and message for given error type.<br>
  For example, in case of a Panic for <code>Mandatory field missing error</code>, one would want to respond with <code>Http 400</code> and a meaningful message of what exactly is wrong with the payload.<br></p>
  
  <p>What is the recommended approach to do this? <br> 
  <strong>UPDATE</strong>
  In code: 2 approaches are listed</p>
  
  <ol>
  <li>Handle errors returned at each method call and build the response.</li>
  <li>Instead of returning errors, panic with custom error types and defer the error recovery to a func to build the response. This makes the code easy to read and less repetitive.</li>
  </ol>
  
  <pre><code>func fooHandler(w http.ResponseWriter, r *http.Request) {
      //decode the request body into a struct instance
      if err := decode(r, myInstance); err != nil {
          sendErrorResponse(w,err,http.StatusBadRequest)
          return
      }
      //validate the struct instance for all mandatory keys presence
      if err := file.validate(); err != nil {
          sendErrorResponse(w,err,http.StatusBadRequest)
          return
      }
      //call DB and validate the response and handle the error
  
      //do some computation and again handle error.
  
      //finally construct response 
  }
  
  func barHandler(w http.ResponseWriter, r *http.Request) {
      //similar to above handler many funcs are called before the response is contruscted
  }
  
  func tomHandler(w http.ResponseWriter, r *http.Request) {
      //similar to above handler many funcs are called before the response is contruscted
  }
  
  func differentHandler(w http.ResponseWriter, r *http.Request) {
      defer recoverForErrors(w,r)
      // call as many funcs as you need.
      // validation, decoding etc will panic instead of returning errors.
      // This will avoid the repetitive boiler plate code of handling error and converting to meaningful error response
      // instead all this logic is pushed to recoverForErrors func. Which retrieves the error from panic and checks for 
      // specific error type to construct the error http response
  }
  </code></pre>