title: Golang, mongodb: getting database error details/codes
body: <p>I'd like to return an appropriate HTTP status code depending on the returned DB error.
  For instance, if a record not found, I'd send 404, if it is something else - 500, and so on ...</p>
  
  <p>At the moment, I just get the standard <code>error</code> type from mgo.
  How can I get an int error code so that I can analyse it and return appropriate HTTP code? </p>
  
  <p>Example:</p>
  
  <pre><code>func (db *DB) GetRecord() (*Person, error) {
      c := db.C("people")
      res := Person{}
      err := c.Find(bson.M{"name": "Alexandra"}).One(&amp;res)
      if err != nil {
          return nil, err
      }
      return &amp;res, nil
  }
  </code></pre>
  
  <p>So this function just gets a record and returns an error (in case of a failure) which is threaded to HTTP handler.</p>
  
  <pre><code>func (s *State) Index(c *gin.Context) {
      res, err := s.DB.GetArticles()
      if err != nil {
          d := gin.H{
              "error": gin.H{
                  "status": "404",
                  "title":  "Get record error!",
                  "detail": err.Error(),
              },
          }
          c.JSON(404, d)
      }
      content := gin.H{
          "data": gin.H{
              "type": "records",
              "id":   res.Id,
              "attributes": gin.H{
                  "Phone": res.Phone,
              },
          },
      }
      c.JSON(200, content)
  }
  </code></pre>
  
  <p>The JSON error reply has a detail field for the actual DB error and status field for the HTTP status code. HTTP status code has to be determined based on the DB error.</p>
  
  <p>So how do I get a detailed error with an int error code so I can <code>switch</code> through it and return a proper HTTP status?</p>
  
  <p>I can see the <code>QueryError</code> and <code>LastError</code> in the documentation but I cannot figure out how to return them. I suppose this question boils down to the correct way of using <code>QueryError</code> and <code>LastError</code> types.</p>
  
  <p>Thank you.</p>