Repository: nuveo/prest
Pull request #137 (2017-05-05 19:44:22)

Improve error return
Normalizes responses to always return JSON or XML even when there are errors and thus avoiding problems with parsers on client systems.
Also some minor fixes.

codecov[bot]: # [Codecov](https://codecov.io/gh/nuveo/prest/pull/137?src=pr&el=h1) Report
> Merging [#137](https://codecov.io/gh/nuveo/prest/pull/137?src=pr&el=desc) into [master](https://codecov.io/gh/nuveo/prest/commit/efbc6a66dcf3fc39b2ed909565d669e25580da80?src=pr&el=desc) will **decrease** coverage by `2.91%`.
> The diff coverage is `68.36%`.

[![Impacted file tree graph](https://codecov.io/gh/nuveo/prest/pull/137/graphs/tree.svg?width=650&height=150&src=pr&token=eVD9urwIEv)](https://codecov.io/gh/nuveo/prest/pull/137?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #137      +/-   ##
==========================================
- Coverage   88.91%   85.99%   -2.92%     
==========================================
  Files          10       10              
  Lines         830      857      +27     
==========================================
- Hits          738      737       -1     
- Misses         59       78      +19     
- Partials       33       42       +9
```


| [Impacted Files](https://codecov.io/gh/nuveo/prest/pull/137?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [controllers/sql.go](https://codecov.io/gh/nuveo/prest/pull/137?src=pr&el=tree#diff-Y29udHJvbGxlcnMvc3FsLmdv) | `100% <100%> (ø)` | :arrow_up: |
| [adapters/postgres/postgres.go](https://codecov.io/gh/nuveo/prest/pull/137?src=pr&el=tree#diff-YWRhcHRlcnMvcG9zdGdyZXMvcG9zdGdyZXMuZ28=) | `82.47% <20%> (-3%)` | :arrow_down: |
| [adapters/postgres/queries.go](https://codecov.io/gh/nuveo/prest/pull/137?src=pr&el=tree#diff-YWRhcHRlcnMvcG9zdGdyZXMvcXVlcmllcy5nbw==) | `85.33% <25%> (-3.56%)` | :arrow_down: |
| [controllers/schemas.go](https://codecov.io/gh/nuveo/prest/pull/137?src=pr&el=tree#diff-Y29udHJvbGxlcnMvc2NoZW1hcy5nbw==) | `88.46% <57.14%> (-11.54%)` | :arrow_down: |
| [controllers/databases.go](https://codecov.io/gh/nuveo/prest/pull/137?src=pr&el=tree#diff-Y29udHJvbGxlcnMvZGF0YWJhc2VzLmdv) | `89.28% <62.5%> (-10.72%)` | :arrow_down: |
| [adapters/postgres/connection/conn.go](https://codecov.io/gh/nuveo/prest/pull/137?src=pr&el=tree#diff-YWRhcHRlcnMvcG9zdGdyZXMvY29ubmVjdGlvbi9jb25uLmdv) | `75% <62.5%> (-2.78%)` | :arrow_down: |
| [controllers/tables.go](https://codecov.io/gh/nuveo/prest/pull/137?src=pr&el=tree#diff-Y29udHJvbGxlcnMvdGFibGVzLmdv) | `96.68% <93.02%> (-1.11%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/nuveo/prest/pull/137?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/nuveo/prest/pull/137?src=pr&el=footer). Last update [efbc6a6...2a6b061](https://codecov.io/gh/nuveo/prest/pull/137?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/CREATING_MODULES.md b/CREATING_MODULES.md
index 62c6a8d..68feae4 100644
--- a/CREATING_MODULES.md
+++ b/CREATING_MODULES.md
@@ -7,7 +7,7 @@
 ## Example:
 
 
-```
+```go
 package main
 
 import (
@@ -24,7 +24,7 @@ func main() {
 	// Reorder middlewares
 	middlewares.MiddlewareStack = []negroni.Handler{
 		negroni.Handler(negroni.NewRecovery()),
-		negroni.Handler(negroni.NewRecovery()),
+		negroni.Handler(negroni.NewLogger()),
 		negroni.Handler(negroni.HandlerFunc(CustomMiddleware)),
 	}
 
diff --git a/adapters/postgres/connection/conn.go b/adapters/postgres/connection/conn.go
index 4b7d4bf..7ad3a9c 100644
--- a/adapters/postgres/connection/conn.go
+++ b/adapters/postgres/connection/conn.go
@@ -16,8 +16,8 @@ var (
 	err error
 )
 
-// MustGet get postgres connection
-func MustGet() *sqlx.DB {
+// Get get postgres connection
+func Get() (*sqlx.DB, error) {
 	if db == nil {
 		dbURI := fmt.Sprintf("user=%s dbname=%s host=%s port=%v sslmode=disable connect_timeout=%d",
 			config.PrestConf.PGUser,
@@ -30,11 +30,21 @@ func MustGet() *sqlx.DB {
 		}
 		db, err = sqlx.Connect("postgres", dbURI)
 		if err != nil {
-			panic(fmt.Sprintf("Unable to connection to database: %v\n", err))
+			return nil, err
 		}
 		db.SetMaxIdleConns(config.PrestConf.PGMaxIdleConn)
 		db.SetMaxOpenConns(config.PrestConf.PGMAxOpenConn)
 	}
+	return db, nil
+}
+
+// MustGet get postgres connection
+func MustGet() *sqlx.DB {
+	var err error
+	db, err = Get()
+	if err != nil {
+		panic(fmt.Sprintf("Unable to connect to database: %v\n", err))
+	}
 	return db
 }
 
diff --git a/adapters/postgres/postgres.go b/adapters/postgres/postgres.go
index 3dc1a76..71bc2bb 100644
--- a/adapters/postgres/postgres.go
+++ b/adapters/postgres/postgres.go
@@ -256,7 +256,12 @@ func CountByRequest(req *http.Request) (countQuery string, err error) {
 
 // Query process queries
 func Query(SQL string, params ...interface{}) (jsonData []byte, err error) {
-	db := connection.MustGet()
+	db, err := connection.Get()
+	if err != nil {
+		log.Println(err)
+		return
+	}
+
 	prepare, err := db.Prepare(SQL)
 	if err != nil {
 		return
@@ -307,7 +312,12 @@ func Query(SQL string, params ...interface{}) (jsonData []byte, err error) {
 
 // QueryCount process queries with count
 func QueryCount(SQL string, params ...interface{}) ([]byte, error) {
-	db := connection.MustGet()
+	db, err := connection.Get()
+	if err != nil {
+		log.Println(err)
+		return nil, err
+	}
+	
 	prepare, err := db.Prepare(SQL)
 	if err != nil {
 		return nil, err
@@ -373,7 +383,12 @@ func Insert(database, schema, table string, body api.Request) (jsonData []byte,
 		}
 		colPlaceholder += fmt.Sprintf("$%d", i)
 	}
-	db := connection.MustGet()
+	db, err := connection.Get()
+	if err != nil {
+		log.Println(err)
+		return
+	}
+
 	tx, err := db.Begin()
 	if err != nil {
 		log.Printf("could not begin transaction: %v\n", err)
@@ -469,7 +484,12 @@ func Delete(database, schema, table, where string, whereValues []interface{}) (j
 			where)
 	}
 
-	db := connection.MustGet()
+	db, err := connection.Get()
+	if err != nil {
+		log.Println(err)
+		return
+	}
+
 	tx, err := db.Begin()
 	if err != nil {
 		log.Printf("could not begin transaction: %v\n", err)
@@ -531,7 +551,12 @@ func Update(database, schema, table, where string, whereValues []interface{}, bo
 		values = append(whereValues, values...)
 	}
 
-	db := connection.MustGet()
+	db, err := connection.Get()
+	if err != nil {
+		log.Println(err)
+		return
+	}
+
 	tx, err := db.Begin()
 	if err != nil {
 		log.Printf("could not begin transaction: %v\n", err)
diff --git a/adapters/postgres/queries.go b/adapters/postgres/queries.go
index 8545965..3535fb1 100644
--- a/adapters/postgres/queries.go
+++ b/adapters/postgres/queries.go
@@ -69,7 +69,12 @@ func ParseScript(scriptPath string, queryURL url.Values) (sqlQuery string, value
 
 // WriteSQL perform INSERT's, UPDATE's, DELETE's operations
 func WriteSQL(sql string, values []interface{}) (resultByte []byte, err error) {
-	db := connection.MustGet()
+	db, err := connection.Get()
+	if err != nil {
+		log.Println(err)
+		return
+	}
+
 	tx, err := db.Begin()
 	if err != nil {
 		log.Printf("could not begin transaction: %v\n", err)
diff --git a/controllers/databases.go b/controllers/databases.go
index 62b8e1c..404db86 100644
--- a/controllers/databases.go
+++ b/controllers/databases.go
@@ -2,7 +2,6 @@ package controllers
 
 import (
 	"fmt"
-	"log"
 	"net/http"
 
 	"github.com/nuveo/prest/adapters/postgres"
@@ -13,7 +12,6 @@ import (
 func GetDatabases(w http.ResponseWriter, r *http.Request) {
 	requestWhere, values, err := postgres.WhereByRequest(r, 1)
 	if err != nil {
-		log.Println(err)
 		http.Error(w, err.Error(), http.StatusBadRequest)
 		return
 	}
@@ -25,7 +23,12 @@ func GetDatabases(w http.ResponseWriter, r *http.Request) {
 		sqlDatabases = fmt.Sprint(sqlDatabases, " AND ", requestWhere)
 	}
 
-	order, _ := postgres.OrderByRequest(r)
+	order, err := postgres.OrderByRequest(r)
+	if err != nil {
+		http.Error(w, err.Error(), http.StatusBadRequest)
+		return
+	}
+
 	if order != "" {
 		sqlDatabases = fmt.Sprint(sqlDatabases, order)
 	} else if !hasCount {
@@ -34,17 +37,17 @@ func GetDatabases(w http.ResponseWriter, r *http.Request) {
 
 	page, err := postgres.PaginateIfPossible(r)
 	if err != nil {
-		http.Error(w, "Paging error", http.StatusBadRequest)
+		http.Error(w, err.Error(), http.StatusBadRequest)
 		return
 	}
 
 	sqlDatabases = fmt.Sprint(sqlDatabases, " ", page)
 	object, err := postgres.Query(sqlDatabases, values...)
 	if err != nil {
-		log.Println(err)
-		http.Error(w, err.Error(), http.StatusInternalServerError)
+		http.Error(w, err.Error(), http.StatusBadRequest)
 		return
 	}
 
+	w.Header().Set("Content-Type", "application/json")
 	w.Write(object)
 }
diff --git a/controllers/schemas.go b/controllers/schemas.go
index 66003b0..8da5e59 100644
--- a/controllers/schemas.go
+++ b/controllers/schemas.go
@@ -2,7 +2,6 @@ package controllers
 
 import (
 	"fmt"
-	"log"
 	"net/http"
 
 	"github.com/nuveo/prest/adapters/postgres"
@@ -13,7 +12,6 @@ import (
 func GetSchemas(w http.ResponseWriter, r *http.Request) {
 	requestWhere, values, err := postgres.WhereByRequest(r, 1)
 	if err != nil {
-		log.Println(err)
 		http.Error(w, err.Error(), http.StatusBadRequest)
 		return
 	}
@@ -24,7 +22,12 @@ func GetSchemas(w http.ResponseWriter, r *http.Request) {
 		sqlSchemas = fmt.Sprint(sqlSchemas, " WHERE ", requestWhere)
 	}
 
-	order, _ := postgres.OrderByRequest(r)
+	order, err := postgres.OrderByRequest(r)
+	if err != nil {
+		http.Error(w, err.Error(), http.StatusBadRequest)
+		return
+	}
+
 	if order != "" {
 		sqlSchemas = fmt.Sprint(sqlSchemas, order)
 	} else if !hasCount {
@@ -33,15 +36,14 @@ func GetSchemas(w http.ResponseWriter, r *http.Request) {
 
 	page, err := postgres.PaginateIfPossible(r)
 	if err != nil {
-		http.Error(w, "Paging error", http.StatusBadRequest)
+		http.Error(w, err.Error(), http.StatusBadRequest)
 		return
 	}
 
 	sqlSchemas = fmt.Sprint(sqlSchemas, " ", page)
 	object, err := postgres.Query(sqlSchemas, values...)
 	if err != nil {
-		log.Println(err)
-		http.Error(w, err.Error(), http.StatusInternalServerError)
+		http.Error(w, err.Error(), http.StatusBadRequest)
 		return
 	}
 
diff --git a/controllers/sql.go b/controllers/sql.go
index 2e8e8a8..13f220a 100644
--- a/controllers/sql.go
+++ b/controllers/sql.go
@@ -1,7 +1,7 @@
 package controllers
 
 import (
-	"log"
+	"fmt"
 	"net/http"
 
 	"github.com/gorilla/mux"
@@ -12,19 +12,19 @@ import (
 func ExecuteScriptQuery(rq *http.Request, queriesPath string, script string) ([]byte, error) {
 	sqlPath, err := postgres.GetScript(rq.Method, queriesPath, script)
 	if err != nil {
-		log.Printf("could not get script %s/%s, %+v", queriesPath, script, err)
+		err = fmt.Errorf("could not get script %s/%s, %+v", queriesPath, script, err)
 		return nil, err
 	}
 
 	sql, values, err := postgres.ParseScript(sqlPath, rq.URL.Query())
 	if err != nil {
-		log.Printf("could not parse script %s/%s, %+v", queriesPath, script, err)
+		err = fmt.Errorf("could not parse script %s/%s, %+v", queriesPath, script, err)
 		return nil, err
 	}
 
 	result, err := postgres.ExecuteScripts(rq.Method, sql, values)
 	if err != nil {
-		log.Printf("could not execute sql %+v, %s", err, sql)
+		err = fmt.Errorf("could not execute sql %+v, %s", err, sql)
 		return nil, err
 	}
 
@@ -32,16 +32,16 @@ func ExecuteScriptQuery(rq *http.Request, queriesPath string, script string) ([]
 }
 
 // ExecuteFromScripts is a controller to peform SQL in scripts created by users
-func ExecuteFromScripts(rw http.ResponseWriter, rq *http.Request) {
-	vars := mux.Vars(rq)
+func ExecuteFromScripts(w http.ResponseWriter, r *http.Request) {
+	vars := mux.Vars(r)
 	queriesPath := vars["queriesLocation"]
 	script := vars["script"]
 
-	result, err := ExecuteScriptQuery(rq, queriesPath, script)
+	result, err := ExecuteScriptQuery(r, queriesPath, script)
 	if err != nil {
-		http.Error(rw, err.Error(), http.StatusBadRequest)
+		http.Error(w, err.Error(), http.StatusBadRequest)
 		return
 	}
 
-	rw.Write(result)
+	w.Write(result)
 }
diff --git a/controllers/tables.go b/controllers/tables.go
index af8145e..7db141a 100644
--- a/controllers/tables.go
+++ b/controllers/tables.go
@@ -2,7 +2,6 @@ package controllers
 
 import (
 	"fmt"
-	"log"
 	"net/http"
 
 	"encoding/json"
@@ -17,17 +16,18 @@ import (
 func GetTables(w http.ResponseWriter, r *http.Request) {
 	requestWhere, values, err := postgres.WhereByRequest(r, 1)
 	if err != nil {
-		log.Println("could not perform WhereByRequest:", err)
+		err = fmt.Errorf("could not perform WhereByRequest: %v", err)
 		http.Error(w, err.Error(), http.StatusBadRequest)
 		return
 	}
 
 	order, err := postgres.OrderByRequest(r)
 	if err != nil {
-		log.Println("could not perform OrderByRequest:", err)
+		err = fmt.Errorf("could not perform OrderByRequest: %v", err)
 		http.Error(w, err.Error(), http.StatusBadRequest)
 		return
 	}
+
 	if order == "" {
 		order = statements.TablesOrderBy
 	}
@@ -44,8 +44,7 @@ func GetTables(w http.ResponseWriter, r *http.Request) {
 
 	object, err := postgres.Query(sqlTables, values...)
 	if err != nil {
-		log.Println(err)
-		http.Error(w, err.Error(), http.StatusInternalServerError)
+		http.Error(w, err.Error(), http.StatusBadRequest)
 		return
 	}
 
@@ -60,7 +59,7 @@ func GetTablesByDatabaseAndSchema(w http.ResponseWriter, r *http.Request) {
 
 	requestWhere, values, err := postgres.WhereByRequest(r, 3)
 	if err != nil {
-		log.Println("could not perform WhereByRequest:", err)
+		err = fmt.Errorf("could not perform WhereByRequest: %v", err)
 		http.Error(w, err.Error(), http.StatusBadRequest)
 		return
 	}
@@ -75,7 +74,7 @@ func GetTablesByDatabaseAndSchema(w http.ResponseWriter, r *http.Request) {
 
 	order, err := postgres.OrderByRequest(r)
 	if err != nil {
-		log.Println("could not perform OrderByRequest:", err)
+		err = fmt.Errorf("could not perform OrderByRequest: %v", err)
 		http.Error(w, err.Error(), http.StatusBadRequest)
 		return
 	}
@@ -87,8 +86,8 @@ func GetTablesByDatabaseAndSchema(w http.ResponseWriter, r *http.Request) {
 
 	page, err := postgres.PaginateIfPossible(r)
 	if err != nil {
-		log.Println("could not perform PaginateIfPossible:", err)
-		http.Error(w, "Paging error", http.StatusBadRequest)
+		err = fmt.Errorf("could not perform PaginateIfPossible: %v", err)
+		http.Error(w, err.Error(), http.StatusBadRequest)
 		return
 	}
 
@@ -101,8 +100,7 @@ func GetTablesByDatabaseAndSchema(w http.ResponseWriter, r *http.Request) {
 
 	object, err := postgres.Query(sqlSchemaTables, valuesAux...)
 	if err != nil {
-		log.Println(err)
-		http.Error(w, err.Error(), http.StatusInternalServerError)
+		http.Error(w, err.Error(), http.StatusBadRequest)
 		return
 	}
 
@@ -121,17 +119,22 @@ func SelectFromTables(w http.ResponseWriter, r *http.Request) {
 	cols = postgres.FieldsPermissions(table, cols, "read")
 
 	if len(cols) == 0 {
-		log.Println("You don't have permission for this action. Please check the permitted fields for this table.")
-		http.Error(w, "You don't have permission for this action. Please check the permitted fields for this table.", http.StatusUnauthorized)
+		err := fmt.Errorf("you don't have permission for this action, please check the permitted fields for this table")
+		http.Error(w, err.Error(), http.StatusBadRequest)
+		return
+	}
+
+	selectStr, err := postgres.SelectFields(cols)
+	if err != nil {
+		http.Error(w, err.Error(), http.StatusBadRequest)
 		return
 	}
 
-	selectStr, _ := postgres.SelectFields(cols)
 	query := fmt.Sprintf("%s %s.%s.%s", selectStr, database, schema, table)
 
 	countQuery, err := postgres.CountByRequest(r)
 	if err != nil {
-		log.Println("could not perform CountByRequest:", err)
+		err = fmt.Errorf("could not perform CountByRequest: %v", err)
 		http.Error(w, err.Error(), http.StatusBadRequest)
 		return
 	}
@@ -141,7 +144,7 @@ func SelectFromTables(w http.ResponseWriter, r *http.Request) {
 
 	joinValues, err := postgres.JoinByRequest(r)
 	if err != nil {
-		log.Println("could not perform JoinByRequest:", err)
+		err = fmt.Errorf("could not perform JoinByRequest: %v", err)
 		http.Error(w, err.Error(), http.StatusBadRequest)
 		return
 	}
@@ -152,7 +155,7 @@ func SelectFromTables(w http.ResponseWriter, r *http.Request) {
 
 	requestWhere, values, err := postgres.WhereByRequest(r, 1)
 	if err != nil {
-		log.Println("could not perform WhereByRequest:", err)
+		err = fmt.Errorf("could not perform WhereByRequest: %v", err)
 		http.Error(w, err.Error(), http.StatusBadRequest)
 		return
 	}
@@ -167,7 +170,7 @@ func SelectFromTables(w http.ResponseWriter, r *http.Request) {
 
 	order, err := postgres.OrderByRequest(r)
 	if err != nil {
-		log.Println("could not perform OrderByRequest:", err)
+		err = fmt.Errorf("could not perform OrderByRequest: %v", err)
 		http.Error(w, err.Error(), http.StatusBadRequest)
 		return
 	}
@@ -177,8 +180,8 @@ func SelectFromTables(w http.ResponseWriter, r *http.Request) {
 
 	page, err := postgres.PaginateIfPossible(r)
 	if err != nil {
-		log.Println("could not perform PaginateIfPossible:", err)
-		http.Error(w, "Paging error", http.StatusBadRequest)
+		err = fmt.Errorf("could not perform PaginateIfPossible: %v", err)
+		http.Error(w, err.Error(), http.StatusBadRequest)
 		return
 	}
 	sqlSelect = fmt.Sprint(sqlSelect, " ", page)
@@ -190,8 +193,7 @@ func SelectFromTables(w http.ResponseWriter, r *http.Request) {
 
 	object, err := runQuery(sqlSelect, values...)
 	if err != nil {
-		log.Println(err)
-		http.Error(w, err.Error(), http.StatusInternalServerError)
+		http.Error(w, err.Error(), http.StatusBadRequest)
 		return
 	}
 
@@ -209,14 +211,14 @@ func InsertInTables(w http.ResponseWriter, r *http.Request) {
 	defer r.Body.Close()
 	err := json.NewDecoder(r.Body).Decode(&req)
 	if err != nil {
-		log.Println("could not decode body:", err)
-		http.Error(w, err.Error(), http.StatusInternalServerError)
+		err = fmt.Errorf("could not decode body: %v", err)
+		http.Error(w, err.Error(), http.StatusBadRequest)
 		return
 	}
 	object, err := postgres.Insert(database, schema, table, req)
 	if err != nil {
-		log.Println("could not perform InsertInTables:", err)
-		http.Error(w, err.Error(), http.StatusInternalServerError)
+		err = fmt.Errorf("could not perform InsertInTables: %v", err)
+		http.Error(w, err.Error(), http.StatusBadRequest)
 		return
 	}
 
@@ -232,15 +234,15 @@ func DeleteFromTable(w http.ResponseWriter, r *http.Request) {
 
 	where, values, err := postgres.WhereByRequest(r, 1)
 	if err != nil {
-		log.Println("could not perform WhereByRequest:", err)
+		err = fmt.Errorf("could not perform WhereByRequest: %v", err)
 		http.Error(w, err.Error(), http.StatusBadRequest)
 		return
 	}
 
 	object, err := postgres.Delete(database, schema, table, where, values)
 	if err != nil {
-		log.Println("could not perform DELETE:", err)
-		http.Error(w, err.Error(), http.StatusInternalServerError)
+		err = fmt.Errorf("could not perform DELETE: %v", err)
+		http.Error(w, err.Error(), http.StatusBadRequest)
 		return
 	}
 
@@ -258,22 +260,22 @@ func UpdateTable(w http.ResponseWriter, r *http.Request) {
 	defer r.Body.Close()
 	err := json.NewDecoder(r.Body).Decode(&req)
 	if err != nil {
-		log.Println("could not decode body:", err)
-		http.Error(w, err.Error(), http.StatusInternalServerError)
+		err = fmt.Errorf("could not decode body: %v", err)
+		http.Error(w, err.Error(), http.StatusBadRequest)
 		return
 	}
 
 	where, values, err := postgres.WhereByRequest(r, 1)
 	if err != nil {
-		log.Println("could not perform WhereByRequest:", err)
+		err = fmt.Errorf("could not perform WhereByRequest: %v", err)
 		http.Error(w, err.Error(), http.StatusBadRequest)
 		return
 	}
 
 	object, err := postgres.Update(database, schema, table, where, values, req)
 	if err != nil {
-		log.Println("could not perform UPDATE:", err)
-		http.Error(w, err.Error(), http.StatusInternalServerError)
+		err = fmt.Errorf("could not perform UPDATE: %v", err)
+		http.Error(w, err.Error(), http.StatusBadRequest)
 		return
 	}
 
diff --git a/middlewares/utils.go b/middlewares/utils.go
index 7c4bf04..053bb7d 100644
--- a/middlewares/utils.go
+++ b/middlewares/utils.go
@@ -1,6 +1,7 @@
 package middlewares
 
 import (
+	"encoding/json"
 	"fmt"
 	"io/ioutil"
 	"net/http"
@@ -43,28 +44,28 @@ func permissionByMethod(method string) (permission string) {
 }
 
 func renderFormat(w http.ResponseWriter, recorder *httptest.ResponseRecorder, format string) {
-	byt, err := ioutil.ReadAll(recorder.Body)
-	if err != nil {
-		http.Error(w, err.Error(), http.StatusInternalServerError)
-		return
-	}
+	byt, _ := ioutil.ReadAll(recorder.Body)
+
 	if recorder.Code != http.StatusOK {
-		w.WriteHeader(recorder.Code)
-		w.Write(byt)
-		return
+		m := make(map[string]string)
+		m["error"] = strings.TrimSpace(string(byt))
+		byt, _ = json.MarshalIndent(m, "", "\t")
 	}
+
 	switch format {
 	case "xml":
 		xmldata, err := j2x.JsonToXml(byt)
 		if err != nil {
-			http.Error(w, err.Error(), http.StatusInternalServerError)
+			http.Error(w, err.Error(), http.StatusBadRequest)
 			return
 		}
 		xmlStr := fmt.Sprintf("<objects>%s</objects>", string(xmldata))
 		w.Header().Set("Content-Type", "application/xml")
+		w.WriteHeader(recorder.Code)
 		w.Write([]byte(xmlStr))
 	default:
 		w.Header().Set("Content-Type", "application/json")
+		w.WriteHeader(recorder.Code)
 		w.Write(byt)
 	}
 }


Test patch:
diff --git a/adapters/postgres/connection/conn_test.go b/adapters/postgres/connection/conn_test.go
index c7931b9..dcd25a7 100644
--- a/adapters/postgres/connection/conn_test.go
+++ b/adapters/postgres/connection/conn_test.go
@@ -6,17 +6,31 @@ import (
 	sqlmock "github.com/DATA-DOG/go-sqlmock"
 )
 
+func TestGet(t *testing.T) {
+	t.Log("Open connection")
+	db, err := Get()
+	if err != nil {
+		t.Fatalf("Expected err equal to nil but got %q", err.Error())
+	}
+
+	t.Log("Ping Pong")
+	err = db.Ping()
+	if err != nil {
+		t.Fatalf("expected no error, but got: %v", err)
+	}
+}
+
 func TestMustGet(t *testing.T) {
 	t.Log("Open connection")
 	db := MustGet()
 	if db == nil {
-		t.Errorf("expected db connection, but no was!")
+		t.Fatalf("expected db connection, but no was!")
 	}
 
 	t.Log("Ping Pong")
 	err := db.Ping()
 	if err != nil {
-		t.Errorf("expected no error, but got: %v", err)
+		t.Fatalf("expected no error, but got: %v", err)
 	}
 }
 
diff --git a/config/middlewares/middlewares_test.go b/config/middlewares/middlewares_test.go
index 2ed902e..3fdfb3f 100644
--- a/config/middlewares/middlewares_test.go
+++ b/config/middlewares/middlewares_test.go
@@ -1,6 +1,7 @@
 package middlewares
 
 import (
+	"encoding/json"
 	"io/ioutil"
 	"net/http"
 	"net/http/httptest"
@@ -56,8 +57,8 @@ func TestGetAppWithReorderedMiddleware(t *testing.T) {
 	if !strings.Contains(string(body), "Calling custom middleware") {
 		t.Error("do not contains 'Calling custom middleware'")
 	}
-	if strings.Contains(resp.Header.Get("Content-Type"), "application/json") {
-		t.Error("content type shouldn't be application/json but was")
+	if !strings.Contains(resp.Header.Get("Content-Type"), "application/json") {
+		t.Error("content type should application/json but wasn't")
 	}
 	MiddlewareStack = []negroni.Handler{}
 }
@@ -71,6 +72,7 @@ func TestGetAppWithoutReorderedMiddleware(t *testing.T) {
 	server := httptest.NewServer(n)
 	defer server.Close()
 	resp, err := http.Get(server.URL)
+
 	if err != nil {
 		t.Fatal("Expected run without errors but was", err.Error())
 	}
@@ -110,7 +112,7 @@ func TestMiddlewareAccessNoblockingCustomRoutes(t *testing.T) {
 		t.Error("do not contains 'custom route'")
 	}
 	if !strings.Contains(resp.Header.Get("Content-Type"), "application/json") {
-		t.Error("content type should be application/json but was")
+		t.Error("content type should be application/json but was", resp.Header.Get("Content-Type"))
 	}
 	resp, err = http.Get(server.URL + "/prest/public/test_write_and_delete_access")
 	if err != nil {
@@ -124,8 +126,8 @@ func TestMiddlewareAccessNoblockingCustomRoutes(t *testing.T) {
 	if resp.StatusCode != http.StatusUnauthorized {
 		t.Errorf("content type should be http.StatusUnauthorized but was %s", resp.Status)
 	}
-	if strings.Contains(resp.Header.Get("Content-Type"), "application/json") {
-		t.Error("content type shouldn't be application/json but was")
+	if !strings.Contains(resp.Header.Get("Content-Type"), "application/json") {
+		t.Error("content type should be application/json but wasn't")
 	}
 	if !strings.Contains(string(body), "required authorization to table") {
 		t.Error("do not contains 'required authorization to table'")
@@ -135,6 +137,13 @@ func TestMiddlewareAccessNoblockingCustomRoutes(t *testing.T) {
 }
 
 func customMiddleware(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
-	w.Write([]byte("Calling custom middleware"))
+
+	m := make(map[string]string)
+	m["msg"] = "Calling custom middleware"
+	b, _ := json.Marshal(m)
+
+	w.Header().Set("Content-Type", "application/json")
+	w.Write(b)
+
 	next(w, r)
 }
diff --git a/controllers/databases_test.go b/controllers/databases_test.go
index 3092a7b..8d2afbe 100644
--- a/controllers/databases_test.go
+++ b/controllers/databases_test.go
@@ -3,6 +3,7 @@ package controllers
 import (
 	"testing"
 
+	"net/http"
 	"net/http/httptest"
 
 	"github.com/gorilla/mux"
@@ -16,14 +17,14 @@ func TestGetDatabases(t *testing.T) {
 		method      string
 		status      int
 	}{
-		{"Get databases without custom where clause", "/databases", "GET", 200},
-		{"Get databases with custom where clause", "/databases?datname=$eq.prest", "GET", 200},
-		{"Get databases with custom order clause", "/databases?_order=datname", "GET", 200},
-		{"Get databases with custom where clause and pagination", "/databases?datname=$eq.prest&_page=1&_page_size=20", "GET", 200},
-		{"Get databases with COUNT clause", "/databases?_count=*", "GET", 200},
-		{"Get databases with custom where invalid clause", "/databases?0datname=prest", "GET", 400},
-		{"Get databases with custom where and pagination invalid", "/databases?datname=$eq.prest&_page=A", "GET", 400},
-		{"Get databases with noexistent column", "/databases?datatata=$eq.test", "GET", 500},
+		{"Get databases without custom where clause", "/databases", "GET", http.StatusOK},
+		{"Get databases with custom where clause", "/databases?datname=$eq.prest", "GET", http.StatusOK},
+		{"Get databases with custom order clause", "/databases?_order=datname", "GET", http.StatusOK},
+		{"Get databases with custom where clause and pagination", "/databases?datname=$eq.prest&_page=1&_page_size=20", "GET", http.StatusOK},
+		{"Get databases with COUNT clause", "/databases?_count=*", "GET", http.StatusOK},
+		{"Get databases with custom where invalid clause", "/databases?0datname=prest", "GET", http.StatusBadRequest},
+		{"Get databases with custom where and pagination invalid", "/databases?datname=$eq.prest&_page=A", "GET", http.StatusBadRequest},
+		{"Get databases with noexistent column", "/databases?datatata=$eq.test", "GET", http.StatusBadRequest},
 	}
 
 	r := api.Request{}
diff --git a/controllers/schemas_test.go b/controllers/schemas_test.go
index 886841d..be3c306 100644
--- a/controllers/schemas_test.go
+++ b/controllers/schemas_test.go
@@ -1,6 +1,7 @@
 package controllers
 
 import (
+	"net/http"
 	"net/http/httptest"
 	"testing"
 
@@ -17,14 +18,13 @@ func TestGetSchemas(t *testing.T) {
 		status      int
 		body        string
 	}{
-		{"Get schemas without custom where clause", "/schemas", "GET", 200, "[{\"schema_name\":\"information_schema\"},{\"schema_name\":\"pg_catalog\"},{\"schema_name\":\"pg_temp_1\"},{\"schema_name\":\"pg_toast\"},{\"schema_name\":\"pg_toast_temp_1\"},{\"schema_name\":\"public\"}]"},
-		{"Get schemas with custom where clause", "/schemas?schema_name=$eq.public", "GET", 200, "[{\"schema_name\":\"public\"}]"},
-		{"Get schemas with custom order clause", "/schemas?schema_name=$eq.public&_order=schema_name", "GET", 200, "[{\"schema_name\":\"public\"}]"},
-		{"Get schemas with custom where clause and pagination", "/schemas?schema_name=$eq.public&_page=1&_page_size=20", "GET", 200, "[{\"schema_name\":\"public\"}]"},
-		{"Get schemas with COUNT clause", "/schemas?_count=*", "GET", 200, "[{\"count\":6}]"},
-		{"Get schemas with custom where invalid clause", "/schemas?0schema_name=$eq.public", "GET", 400, "invalid identifier: 0schema_name\n"},
-		{"Get schemas with custom where and pagination invalid", "/schemas?schema_name=$eq.public&_page=A", "GET", 400, "Paging error\n"},
-		{"Get schemas with noexistent column", "/schemas?schematame=$eq.test", "GET", 500, "pq: column \"schematame\" does not exist\n"},
+		{"Get schemas without custom where clause", "/schemas", "GET", http.StatusOK, "[{\"schema_name\":\"information_schema\"},{\"schema_name\":\"pg_catalog\"},{\"schema_name\":\"pg_temp_1\"},{\"schema_name\":\"pg_toast\"},{\"schema_name\":\"pg_toast_temp_1\"},{\"schema_name\":\"public\"}]"},
+		{"Get schemas with custom where clause", "/schemas?schema_name=$eq.public", "GET", http.StatusOK, "[{\"schema_name\":\"public\"}]"},
+		{"Get schemas with custom order clause", "/schemas?schema_name=$eq.public&_order=schema_name", "GET", http.StatusOK, "[{\"schema_name\":\"public\"}]"},
+		{"Get schemas with custom where clause and pagination", "/schemas?schema_name=$eq.public&_page=1&_page_size=20", "GET", http.StatusOK, "[{\"schema_name\":\"public\"}]"},
+		{"Get schemas with COUNT clause", "/schemas?_count=*", "GET", http.StatusOK, "[{\"count\":6}]"},
+		{"Get schemas with custom where invalid clause", "/schemas?0schema_name=$eq.public", "GET", http.StatusBadRequest, "invalid identifier: 0schema_name\n"},
+		{"Get schemas with noexistent column", "/schemas?schematame=$eq.test", "GET", http.StatusBadRequest, "pq: column \"schematame\" does not exist\n"},
 	}
 
 	router := mux.NewRouter()
@@ -36,6 +36,38 @@ func TestGetSchemas(t *testing.T) {
 	for _, tc := range testCases {
 		t.Log(tc.description)
 		doRequest(t, server.URL+tc.url, r, tc.method, tc.status, "GetSchemas", tc.body)
+	}
+}
 
+func TestVersionDependentGetSchemas(t *testing.T) {
+	//This test supports error messages from different versions of Go.
+	var testCases = []struct {
+		description string
+		url         string
+		method      string
+		status      int
+		body        []string
+	}{
+		{"Get schemas with custom where and pagination invalid",
+			"/schemas?schema_name=$eq.public&_page=A",
+			"GET",
+			http.StatusBadRequest,
+			[]string{
+				"strconv.ParseInt: parsing \"A\": invalid syntax\n",
+				"strconv.Atoi: parsing \"A\": invalid syntax\n",
+			},
+		},
 	}
+
+	router := mux.NewRouter()
+	router.HandleFunc("/schemas", GetSchemas).Methods("GET")
+	server := httptest.NewServer(router)
+	defer server.Close()
+
+	r := api.Request{}
+	for _, tc := range testCases {
+		t.Log(tc.description)
+		doRequest(t, server.URL+tc.url, r, tc.method, tc.status, "GetSchemas", tc.body...)
+	}
+
 }
diff --git a/controllers/sql_test.go b/controllers/sql_test.go
index 15a729d..36db517 100644
--- a/controllers/sql_test.go
+++ b/controllers/sql_test.go
@@ -53,11 +53,11 @@ func TestExecuteScriptQuery(t *testing.T) {
 		method      string
 		status      int
 	}{
-		{"Execute script GET method", "/testing/script-get/?field1=gopher", "GET", 200},
-		{"Execute script POST method", "/testing/script-post/?field1=gopherzin&field2=pereira", "POST", 200},
+		{"Execute script GET method", "/testing/script-get/?field1=gopher", "GET", http.StatusOK},
+		{"Execute script POST method", "/testing/script-post/?field1=gopherzin&field2=pereira", "POST", http.StatusOK},
 		// errors
-		{"Execute script GET method invalid", "/testing/script-get/?nonexistent=gopher", "GET", 400},
-		{"Execute script POST method invalid", "/testing/script-post/?nonexistent=gopher", "POST", 400},
+		{"Execute script GET method invalid", "/testing/script-get/?nonexistent=gopher", "GET", http.StatusBadRequest},
+		{"Execute script POST method invalid", "/testing/script-post/?nonexistent=gopher", "POST", http.StatusBadRequest},
 	}
 
 	apiReq := api.Request{}
@@ -81,16 +81,16 @@ func TestExecuteFromScripts(t *testing.T) {
 		method      string
 		status      int
 	}{
-		{"Get results using scripts by GET method", "/_QUERIES/fulltable/get_all?field1=gopher", "GET", 200},
-		{"Get results using scripts by POST method", "/_QUERIES/fulltable/write_all?field1=gopherzin&field2=pereira", "POST", 200},
-		{"Get results using scripts by PUT method", "/_QUERIES/fulltable/put_all?field1=trump&field2=pereira", "PUT", 200},
-		{"Get results using scripts by PATCH method", "/_QUERIES/fulltable/patch_all?field1=temer&field2=trump", "PATCH", 200},
-		{"Get results using scripts by DELETE method", "/_QUERIES/fulltable/delete_all?field1=trump", "DELETE", 200},
+		{"Get results using scripts by GET method", "/_QUERIES/fulltable/get_all?field1=gopher", "GET", http.StatusOK},
+		{"Get results using scripts by POST method", "/_QUERIES/fulltable/write_all?field1=gopherzin&field2=pereira", "POST", http.StatusOK},
+		{"Get results using scripts by PUT method", "/_QUERIES/fulltable/put_all?field1=trump&field2=pereira", "PUT", http.StatusOK},
+		{"Get results using scripts by PATCH method", "/_QUERIES/fulltable/patch_all?field1=temer&field2=trump", "PATCH", http.StatusOK},
+		{"Get results using scripts by DELETE method", "/_QUERIES/fulltable/delete_all?field1=trump", "DELETE", http.StatusOK},
 		// errors
-		{"Get errors using nonexistent folder", "/_QUERIES/fullnon/delete_all?field1=trump", "DELETE", 400},
-		{"Get errors using nonexistent script", "/_QUERIES/fulltable/some_com_all?field1=trump", "DELETE", 400},
-		{"Get errors with invalid params in script", "/_QUERIES/fulltable/get_all?column1=gopher", "GET", 400},
-		{"Get errors with invalid execution of sql", "/_QUERIES/fulltable/create_table?field1=test7", "POST", 400},
+		{"Get errors using nonexistent folder", "/_QUERIES/fullnon/delete_all?field1=trump", "DELETE", http.StatusBadRequest},
+		{"Get errors using nonexistent script", "/_QUERIES/fulltable/some_com_all?field1=trump", "DELETE", http.StatusBadRequest},
+		{"Get errors with invalid params in script", "/_QUERIES/fulltable/get_all?column1=gopher", "GET", http.StatusBadRequest},
+		{"Get errors with invalid execution of sql", "/_QUERIES/fulltable/create_table?field1=test7", "POST", http.StatusBadRequest},
 	}
 
 	for _, tc := range testCases {
diff --git a/controllers/tables_test.go b/controllers/tables_test.go
index ee32997..21508c9 100644
--- a/controllers/tables_test.go
+++ b/controllers/tables_test.go
@@ -1,6 +1,7 @@
 package controllers
 
 import (
+	"net/http"
 	"net/http/httptest"
 	"testing"
 
@@ -15,14 +16,14 @@ func TestGetTables(t *testing.T) {
 		method      string
 		status      int
 	}{
-		{"Get tables without custom where clause", "/tables", "GET", 200},
-		{"Get tables with custom where clause", "/tables?c.relname=$eq.test", "GET", 200},
-		{"Get tables with custom order clause", "/tables?_order=c.relname", "GET", 200},
-		{"Get tables with custom where clause and pagination", "/tables?c.relname=$eq.test&_page=1&_page_size=20", "GET", 200},
-		{"Get tables with COUNT clause", "/tables?_count=*", "GET", 200},
-		{"Get tables with custom where invalid clause", "/tables?0c.relname=$eq.test", "GET", 400},
-		{"Get tables with ORDER BY and invalid column", "/tables?_order=0c.relname", "GET", 400},
-		{"Get tables with noexistent column", "/tables?c.rolooo=$eq.test", "GET", 500},
+		{"Get tables without custom where clause", "/tables", "GET", http.StatusOK},
+		{"Get tables with custom where clause", "/tables?c.relname=$eq.test", "GET", http.StatusOK},
+		{"Get tables with custom order clause", "/tables?_order=c.relname", "GET", http.StatusOK},
+		{"Get tables with custom where clause and pagination", "/tables?c.relname=$eq.test&_page=1&_page_size=20", "GET", http.StatusOK},
+		{"Get tables with COUNT clause", "/tables?_count=*", "GET", http.StatusOK},
+		{"Get tables with custom where invalid clause", "/tables?0c.relname=$eq.test", "GET", http.StatusBadRequest},
+		{"Get tables with ORDER BY and invalid column", "/tables?_order=0c.relname", "GET", http.StatusBadRequest},
+		{"Get tables with noexistent column", "/tables?c.rolooo=$eq.test", "GET", http.StatusBadRequest},
 	}
 
 	router := mux.NewRouter()
@@ -44,15 +45,15 @@ func TestGetTablesByDatabaseAndSchema(t *testing.T) {
 		method      string
 		status      int
 	}{
-		{"Get tables by database and schema without custom where clause", "/prest/public", "GET", 200},
-		{"Get tables by database and schema with custom where clause", "/prest/public?t.tablename=$eq.test", "GET", 200},
-		{"Get tables by database and schema with order clause", "/prest/public?t.tablename=$eq.test&_order=t.tablename", "GET", 200},
-		{"Get tables by database and schema with custom where clause and pagination", "/prest/public?t.tablename=$eq.test&_page=1&_page_size=20", "GET", 200},
+		{"Get tables by database and schema without custom where clause", "/prest/public", "GET", http.StatusOK},
+		{"Get tables by database and schema with custom where clause", "/prest/public?t.tablename=$eq.test", "GET", http.StatusOK},
+		{"Get tables by database and schema with order clause", "/prest/public?t.tablename=$eq.test&_order=t.tablename", "GET", http.StatusOK},
+		{"Get tables by database and schema with custom where clause and pagination", "/prest/public?t.tablename=$eq.test&_page=1&_page_size=20", "GET", http.StatusOK},
 		// errors
-		{"Get tables by database and schema with custom where invalid clause", "/prest/public?0t.tablename=$eq.test", "GET", 400},
-		{"Get tables by databases and schema with custom where and pagination invalid", "/prest/public?t.tablename=$eq.test&_page=A&_page_size=20", "GET", 400},
-		{"Get tables by databases and schema with ORDER BY and column invalid", "/prest/public?_order=0t.tablename", "GET", 400},
-		{"Get tables by databases with noexistent column", "/prest/public?t.taababa=$eq.test", "GET", 500},
+		{"Get tables by database and schema with custom where invalid clause", "/prest/public?0t.tablename=$eq.test", "GET", http.StatusBadRequest},
+		{"Get tables by databases and schema with custom where and pagination invalid", "/prest/public?t.tablename=$eq.test&_page=A&_page_size=20", "GET", http.StatusBadRequest},
+		{"Get tables by databases and schema with ORDER BY and column invalid", "/prest/public?_order=0t.tablename", "GET", http.StatusBadRequest},
+		{"Get tables by databases with noexistent column", "/prest/public?t.taababa=$eq.test", "GET", http.StatusBadRequest},
 	}
 
 	r := api.Request{}
@@ -80,39 +81,39 @@ func TestSelectFromTables(t *testing.T) {
 		method      string
 		status      int
 	}{
-		{"execute select in a table without custom where clause", "/prest/public/test", "GET", 200},
-		{"execute select in a table with count all fields *", "/prest/public/test?_count=*", "GET", 200},
-		{"execute select in a table with count function", "/prest/public/test?_count=name", "GET", 200},
-		{"execute select in a table with custom where clause", "/prest/public/test?name=$eq.nuveo", "GET", 200},
-		{"execute select in a table with custom join clause", "/prest/public/test?_join=inner:test8:test8.nameforjoin:$eq:test.name", "GET", 200},
-		{"execute select in a table with order clause", "/prest/public/test?_order=name", "GET", 200},
-		{"execute select in a table with order clause empty", "/prest/public/test?_order=", "GET", 200},
-		{"execute select in a table with custom where clause and pagination", "/prest/public/test?name=$eq.nuveo&_page=1&_page_size=20", "GET", 200},
-		{"execute select in a table with select fields", "/prest/public/test5?_select=celphone,name", "GET", 200},
-		{"execute select in a table with select *", "/prest/public/test5?_select=*", "GET", 200},
-		{"execute select in a view without custom where clause", "/prest/public/view_test", "GET", 200},
-		{"execute select in a view with count all fields *", "/prest/public/view_test?_count=*", "GET", 200},
-		{"execute select in a view with count function", "/prest/public/view_test?_count=player", "GET", 200},
-		{"execute select in a view with order function", "/prest/public/view_test?_order=-player", "GET", 200},
-		{"execute select in a view with custom where clause", "/prest/public/view_test?player=$eq.gopher", "GET", 200},
-		{"execute select in a view with custom join clause", "/prest/public/view_test?_join=inner:test2:test2.name:eq:view_test.player", "GET", 200},
-		{"execute select in a view with custom where clause and pagination", "/prest/public/view_test?player=$eq.gopher&_page=1&_page_size=20", "GET", 200},
-		{"execute select in a view with select fields", "/prest/public/view_test?_select=player", "GET", 200},
+		{"execute select in a table without custom where clause", "/prest/public/test", "GET", http.StatusOK},
+		{"execute select in a table with count all fields *", "/prest/public/test?_count=*", "GET", http.StatusOK},
+		{"execute select in a table with count function", "/prest/public/test?_count=name", "GET", http.StatusOK},
+		{"execute select in a table with custom where clause", "/prest/public/test?name=$eq.nuveo", "GET", http.StatusOK},
+		{"execute select in a table with custom join clause", "/prest/public/test?_join=inner:test8:test8.nameforjoin:$eq:test.name", "GET", http.StatusOK},
+		{"execute select in a table with order clause", "/prest/public/test?_order=name", "GET", http.StatusOK},
+		{"execute select in a table with order clause empty", "/prest/public/test?_order=", "GET", http.StatusOK},
+		{"execute select in a table with custom where clause and pagination", "/prest/public/test?name=$eq.nuveo&_page=1&_page_size=20", "GET", http.StatusOK},
+		{"execute select in a table with select fields", "/prest/public/test5?_select=celphone,name", "GET", http.StatusOK},
+		{"execute select in a table with select *", "/prest/public/test5?_select=*", "GET", http.StatusOK},
+		{"execute select in a view without custom where clause", "/prest/public/view_test", "GET", http.StatusOK},
+		{"execute select in a view with count all fields *", "/prest/public/view_test?_count=*", "GET", http.StatusOK},
+		{"execute select in a view with count function", "/prest/public/view_test?_count=player", "GET", http.StatusOK},
+		{"execute select in a view with order function", "/prest/public/view_test?_order=-player", "GET", http.StatusOK},
+		{"execute select in a view with custom where clause", "/prest/public/view_test?player=$eq.gopher", "GET", http.StatusOK},
+		{"execute select in a view with custom join clause", "/prest/public/view_test?_join=inner:test2:test2.name:eq:view_test.player", "GET", http.StatusOK},
+		{"execute select in a view with custom where clause and pagination", "/prest/public/view_test?player=$eq.gopher&_page=1&_page_size=20", "GET", http.StatusOK},
+		{"execute select in a view with select fields", "/prest/public/view_test?_select=player", "GET", http.StatusOK},
 
 		// errors
-		{"execute select in a table with invalid join clause", "/prest/public/test?_join=inner:test2:test2.name", "GET", 400},
-		{"execute select in a table with invalid where clause", "/prest/public/test?0name=$eq.nuveo", "GET", 400},
-		{"execute select in a table with order clause and column invalid", "/prest/public/test?_order=0name", "GET", 400},
-		{"execute select in a table with invalid pagination clause", "/prest/public/test?name=$eq.nuveo&_page=A", "GET", 400},
-		{"execute select in a table with invalid where clause", "/prest/public/test?0name=$eq.nuveo", "GET", 400},
-		{"execute select in a table with invalid count clause", "/prest/public/test?_count=0name", "GET", 400},
-		{"execute select in a table with invalid order clause", "/prest/public/test?_order=0name", "GET", 400},
-		{"execute select in a view with an other column", "/prest/public/view_test?_select=celphone", "GET", 401},
-		{"execute select in a view with where and column invalid", "/prest/public/view_test?0celphone=$eq.888888", "GET", 400},
-		{"execute select in a view with custom join clause invalid", "/prest/public/view_test?_join=inner:test2.name:eq:view_test.player", "GET", 400},
-		{"execute select in a view with custom where clause and pagination invalid", "/prest/public/view_test?player=$eq.gopher&_page=A&_page_size=20", "GET", 400},
-		{"execute select in a view with order by and column invalid", "/prest/public/view_test?_order=0celphone", "GET", 400},
-		{"execute select in a view with count column invalid", "/prest/public/view_test?_count=0celphone", "GET", 400},
+		{"execute select in a table with invalid join clause", "/prest/public/test?_join=inner:test2:test2.name", "GET", http.StatusBadRequest},
+		{"execute select in a table with invalid where clause", "/prest/public/test?0name=$eq.nuveo", "GET", http.StatusBadRequest},
+		{"execute select in a table with order clause and column invalid", "/prest/public/test?_order=0name", "GET", http.StatusBadRequest},
+		{"execute select in a table with invalid pagination clause", "/prest/public/test?name=$eq.nuveo&_page=A", "GET", http.StatusBadRequest},
+		{"execute select in a table with invalid where clause", "/prest/public/test?0name=$eq.nuveo", "GET", http.StatusBadRequest},
+		{"execute select in a table with invalid count clause", "/prest/public/test?_count=0name", "GET", http.StatusBadRequest},
+		{"execute select in a table with invalid order clause", "/prest/public/test?_order=0name", "GET", http.StatusBadRequest},
+		{"execute select in a view with an other column", "/prest/public/view_test?_select=celphone", "GET", http.StatusBadRequest},
+		{"execute select in a view with where and column invalid", "/prest/public/view_test?0celphone=$eq.888888", "GET", http.StatusBadRequest},
+		{"execute select in a view with custom join clause invalid", "/prest/public/view_test?_join=inner:test2.name:eq:view_test.player", "GET", http.StatusBadRequest},
+		{"execute select in a view with custom where clause and pagination invalid", "/prest/public/view_test?player=$eq.gopher&_page=A&_page_size=20", "GET", http.StatusBadRequest},
+		{"execute select in a view with order by and column invalid", "/prest/public/view_test?_order=0celphone", "GET", http.StatusBadRequest},
+		{"execute select in a view with count column invalid", "/prest/public/view_test?_count=0celphone", "GET", http.StatusBadRequest},
 	}
 
 	for _, tc := range testCases {
@@ -140,11 +141,11 @@ func TestInsertInTables(t *testing.T) {
 		request     api.Request
 		status      int
 	}{
-		{"execute insert in a table without custom where clause", "/prest/public/test", r, 200},
-		{"execute insert in a table with invalid database", "/0prest/public/test", r, 500},
-		{"execute insert in a table with invalid schema", "/prest/0public/test", r, 500},
-		{"execute insert in a table with invalid table", "/prest/public/0test", r, 500},
-		{"execute insert in a table with invalid body", "/prest/public/test", api.Request{}, 500},
+		{"execute insert in a table without custom where clause", "/prest/public/test", r, http.StatusOK},
+		{"execute insert in a table with invalid database", "/0prest/public/test", r, http.StatusBadRequest},
+		{"execute insert in a table with invalid schema", "/prest/0public/test", r, http.StatusBadRequest},
+		{"execute insert in a table with invalid table", "/prest/public/0test", r, http.StatusBadRequest},
+		{"execute insert in a table with invalid body", "/prest/public/test", api.Request{}, http.StatusBadRequest},
 	}
 
 	for _, tc := range testCases {
@@ -165,12 +166,12 @@ func TestDeleteFromTable(t *testing.T) {
 		request     api.Request
 		status      int
 	}{
-		{"execute delete in a table without custom where clause", "/prest/public/test", api.Request{}, 200},
-		{"excute delete in a table with where clause", "/prest/public/test?name=$eq.nuveo", api.Request{}, 200},
-		{"execute delete in a table with invalid database", "/0prest/public/test", api.Request{}, 500},
-		{"execute delete in a table with invalid schema", "/prest/0public/test", api.Request{}, 500},
-		{"execute delete in a table with invalid table", "/prest/public/0test", api.Request{}, 500},
-		{"execute delete in a table with invalid where clause", "/prest/public/test?0name=$eq.nuveo", api.Request{}, 400},
+		{"execute delete in a table without custom where clause", "/prest/public/test", api.Request{}, http.StatusOK},
+		{"excute delete in a table with where clause", "/prest/public/test?name=$eq.nuveo", api.Request{}, http.StatusOK},
+		{"execute delete in a table with invalid database", "/0prest/public/test", api.Request{}, http.StatusBadRequest},
+		{"execute delete in a table with invalid schema", "/prest/0public/test", api.Request{}, http.StatusBadRequest},
+		{"execute delete in a table with invalid table", "/prest/public/0test", api.Request{}, http.StatusBadRequest},
+		{"execute delete in a table with invalid where clause", "/prest/public/test?0name=$eq.nuveo", api.Request{}, http.StatusBadRequest},
 	}
 
 	for _, tc := range testCases {
@@ -198,13 +199,13 @@ func TestUpdateFromTable(t *testing.T) {
 		request     api.Request
 		status      int
 	}{
-		{"execute update in a table without custom where clause", "/prest/public/test", r, 200},
-		{"excute update in a table with where clause", "/prest/public/test?name=$eq.nuveo", r, 200},
-		{"execute update in a table with invalid database", "/0prest/public/test", r, 500},
-		{"execute update in a table with invalid schema", "/prest/0public/test", r, 500},
-		{"execute update in a table with invalid table", "/prest/public/0test", r, 500},
-		{"execute update in a table with invalid where clause", "/prest/public/test?0name=$eq.nuveo", r, 400},
-		{"execute update in a table with invalid body", "/prest/public/test?name=$eq.nuveo", api.Request{}, 500},
+		{"execute update in a table without custom where clause", "/prest/public/test", r, http.StatusOK},
+		{"excute update in a table with where clause", "/prest/public/test?name=$eq.nuveo", r, http.StatusOK},
+		{"execute update in a table with invalid database", "/0prest/public/test", r, http.StatusBadRequest},
+		{"execute update in a table with invalid schema", "/prest/0public/test", r, http.StatusBadRequest},
+		{"execute update in a table with invalid table", "/prest/public/0test", r, http.StatusBadRequest},
+		{"execute update in a table with invalid where clause", "/prest/public/test?0name=$eq.nuveo", r, http.StatusBadRequest},
+		{"execute update in a table with invalid body", "/prest/public/test?name=$eq.nuveo", api.Request{}, http.StatusBadRequest},
 	}
 
 	for _, tc := range testCases {
diff --git a/controllers/utils_test.go b/controllers/utils_test.go
index 9328ee9..e57d320 100644
--- a/controllers/utils_test.go
+++ b/controllers/utils_test.go
@@ -181,12 +181,21 @@ func doRequest(t *testing.T, url string, r api.Request, method string, expectedS
 	}
 
 	if len(expectedBody) > 0 {
-		if string(body) != expectedBody[0] {
+		if !containsStringInSlice(expectedBody, string(body)) {
 			t.Errorf("expected %q, got: %q", expectedBody, string(body))
 		}
 	}
 }
 
+func containsStringInSlice(s []string, e string) bool {
+	for _, a := range s {
+		if a == e {
+			return true
+		}
+	}
+	return false
+}
+
 func createMockScripts(base string) {
 	log.Println("Create scripts on", base)
 	err := os.Mkdir(fmt.Sprint(base, "/fulltable"), 0777)