title: Return postgres errors in api response
body: <p>I have two simple api methods in my code. Method with endpoind <code>/api/user/create</code> creates user. Field <code>username</code> is unique. When i trying to create user with same username that already exists in database, i have an error in console:</p>
  <pre><code>(/home/andrej/go/src/go_contacts/models/users.go:19) 
  [2020-12-23 22:03:10]  pq: duplicate key value violates unique constraint &quot;users_username_key&quot;
  </code></pre>
  <p>I want to show this error in response to user, or somehow identify type of error in my code, to show different error messages for user. I know only that if i have error <code>user</code> returns me id=0. But it doesnt seems like a good message for user.</p>
  <p><code>main.go</code></p>
  <pre><code>package main
  
  import (
      &quot;fmt&quot;
      &quot;go_contacts/controllers&quot;
      &quot;net/http&quot;
      &quot;os&quot;
  
      &quot;github.com/gorilla/mux&quot;
      &quot;github.com/joho/godotenv&quot;
  )
  
  func main() {
      godotenv.Load(&quot;.env&quot;)
  
  
      router := mux.NewRouter()
      router.HandleFunc(&quot;/&quot;, controllers.ReturnHello).Methods(&quot;GET&quot;)
      router.HandleFunc(&quot;/api/user/create&quot;, controllers.CreateUser).Methods(&quot;POST&quot;)
  
      port := os.Getenv(&quot;PORT&quot;)
      if port == &quot;&quot; {
          port = &quot;8000&quot;
      }
  
      err := http.ListenAndServe(&quot;:&quot;+port, router)
      if err != nil {
          fmt.Print(err)
      }
  }
  </code></pre>
  <p><code>models.go</code> with user struct:</p>
  <pre><code>package models
  
  import (
      u &quot;go_contacts/utils&quot;
  
      &quot;github.com/jinzhu/gorm&quot;
  )
  
  // User base model
  type User struct {
      gorm.Model
      Username string `json:&quot;username&quot; gorm:&quot;unique&quot;`
      Password string `json:&quot;password&quot;`
      Email    string `json:&quot;email&quot;`
  }
  
  // Create new user
  func (user *User) Create() map[string]interface{} {
      GetDB().Create(user)
  
      if user.ID &lt;= 0 {
          return u.Message(false, &quot;Failed to create user, connection error.&quot;)
      }
  
      response := u.Message(true, &quot;Account has been created&quot;)
      response[&quot;user&quot;] = user
      return response
  }
  </code></pre>