Skip to content
This repository has been archived by the owner on Jul 29, 2020. It is now read-only.

Return 404 response for inserts,updates and deletes #21

Merged
merged 2 commits into from Dec 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 39 additions & 9 deletions tables.go
Expand Up @@ -191,8 +191,14 @@ func SelectFromTables(w http.ResponseWriter, r *http.Request) {
}

sc := runQuery(sqlSelect, values...)
if sc.Err() != nil {
http.Error(w, sc.Err().Error(), http.StatusBadRequest)
if err = sc.Err(); err != nil {
errorMessage := sc.Err().Error()
if errorMessage == fmt.Sprintf(`pq: relation "%s.%s" does not exist`, schema, table) {
fmt.Println(errorMessage)
http.Error(w, errorMessage, http.StatusNotFound)
return
}
http.Error(w, errorMessage, http.StatusBadRequest)
return
}
w.Write(sc.Bytes())
Expand All @@ -217,8 +223,14 @@ func InsertInTables(w http.ResponseWriter, r *http.Request) {
sql := config.PrestConf.Adapter.InsertSQL(database, schema, table, names, placeholders)

sc := config.PrestConf.Adapter.Insert(sql, values...)
if sc.Err() != nil {
http.Error(w, sc.Err().Error(), http.StatusBadRequest)
if err = sc.Err(); err != nil {
errorMessage := sc.Err().Error()
if errorMessage == fmt.Sprintf(`pq: relation "%s.%s" does not exist`, schema, table) {
fmt.Println(errorMessage)
http.Error(w, errorMessage, http.StatusNotFound)
return
}
http.Error(w, errorMessage, http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusCreated)
Expand Down Expand Up @@ -249,7 +261,13 @@ func BatchInsertInTables(w http.ResponseWriter, r *http.Request) {
sc = config.PrestConf.Adapter.BatchInsertCopy(database, schema, table, strings.Split(names, ","), values...)
}
if err = sc.Err(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
errorMessage := sc.Err().Error()
if errorMessage == fmt.Sprintf(`pq: relation "%s.%s" does not exist`, schema, table) {
fmt.Println(errorMessage)
http.Error(w, errorMessage, http.StatusNotFound)
return
}
http.Error(w, errorMessage, http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusCreated)
Expand Down Expand Up @@ -292,8 +310,14 @@ func DeleteFromTable(w http.ResponseWriter, r *http.Request) {
}

sc := config.PrestConf.Adapter.Delete(sql, values...)
if sc.Err() != nil {
http.Error(w, sc.Err().Error(), http.StatusBadRequest)
if err = sc.Err(); err != nil {
errorMessage := sc.Err().Error()
if errorMessage == fmt.Sprintf(`pq: relation "%s.%s" does not exist`, schema, table) {
fmt.Println(errorMessage)
http.Error(w, errorMessage, http.StatusNotFound)
return
}
http.Error(w, errorMessage, http.StatusBadRequest)
return
}
w.Write(sc.Bytes())
Expand Down Expand Up @@ -348,8 +372,14 @@ func UpdateTable(w http.ResponseWriter, r *http.Request) {
}

sc := config.PrestConf.Adapter.Update(sql, values...)
if sc.Err() != nil {
http.Error(w, sc.Err().Error(), http.StatusBadRequest)
if err = sc.Err(); err != nil {
errorMessage := sc.Err().Error()
if errorMessage == fmt.Sprintf(`pq: relation "%s.%s" does not exist`, schema, table) {
fmt.Println(errorMessage)
http.Error(w, errorMessage, http.StatusNotFound)
return
}
http.Error(w, errorMessage, http.StatusBadRequest)
return
}
w.Write(sc.Bytes())
Expand Down
16 changes: 8 additions & 8 deletions tables_test.go
Expand Up @@ -172,8 +172,8 @@ func TestInsertInTables(t *testing.T) {
{"execute insert in a table with jsonb field", "/prest/public/testjson", mJSON, http.StatusCreated},
{"execute insert in a table without custom where clause", "/prest/public/test", m, http.StatusCreated},
{"execute insert in a table with invalid database", "/0prest/public/test", m, http.StatusBadRequest},
{"execute insert in a table with invalid schema", "/prest/0public/test", m, http.StatusBadRequest},
{"execute insert in a table with invalid table", "/prest/public/0test", m, http.StatusBadRequest},
{"execute insert in a table with invalid schema", "/prest/0public/test", m, http.StatusNotFound},
{"execute insert in a table with invalid table", "/prest/public/0test", m, http.StatusNotFound},
{"execute insert in a table with invalid body", "/prest/public/test", nil, http.StatusBadRequest},
}

Expand Down Expand Up @@ -213,8 +213,8 @@ func TestBatchInsertInTables(t *testing.T) {
{"execute insert in a table with jsonb field", "/batch/prest/public/testjson", mJSON, http.StatusCreated, false},
{"execute insert in a table without custom where clause", "/batch/prest/public/test", m, http.StatusCreated, false},
{"execute insert in a table with invalid database", "/batch/0prest/public/test", m, http.StatusBadRequest, false},
{"execute insert in a table with invalid schema", "/batch/prest/0public/test", m, http.StatusBadRequest, false},
{"execute insert in a table with invalid table", "/batch/prest/public/0test", m, http.StatusBadRequest, false},
{"execute insert in a table with invalid schema", "/batch/prest/0public/test", m, http.StatusNotFound, false},
{"execute insert in a table with invalid table", "/batch/prest/public/0test", m, http.StatusNotFound, false},
{"execute insert in a table with invalid body", "/batch/prest/public/test", nil, http.StatusBadRequest, false},
{"execute insert in a table with array field with copy", "/batch/prest/public/testarray", mARRAY, http.StatusCreated, true},
{"execute insert in a table with jsonb field with copy", "/batch/prest/public/testjson", mJSON, http.StatusCreated, true},
Expand Down Expand Up @@ -267,8 +267,8 @@ func TestDeleteFromTable(t *testing.T) {
{"execute delete in a table without custom where clause", "/prest/public/test", nil, http.StatusOK},
{"excute delete in a table with where clause", "/prest/public/test?name=$eq.nuveo", nil, http.StatusOK},
{"execute delete in a table with invalid database", "/0prest/public/test", nil, http.StatusBadRequest},
{"execute delete in a table with invalid schema", "/prest/0public/test", nil, http.StatusBadRequest},
{"execute delete in a table with invalid table", "/prest/public/0test", nil, http.StatusBadRequest},
{"execute delete in a table with invalid schema", "/prest/0public/test", nil, http.StatusNotFound},
{"execute delete in a table with invalid table", "/prest/public/0test", nil, http.StatusNotFound},
{"execute delete in a table with invalid where clause", "/prest/public/test?0name=$eq.nuveo", nil, http.StatusBadRequest},
}

Expand Down Expand Up @@ -298,8 +298,8 @@ func TestUpdateFromTable(t *testing.T) {
{"execute update in a table with where clause and returning all fields", "/prest/public/test?id=1&_returning=*", m, http.StatusOK},
{"execute update in a table with where clause and returning name field", "/prest/public/test?id=2&_returning=name", m, http.StatusOK},
{"execute update in a table with invalid database", "/0prest/public/test", m, http.StatusBadRequest},
{"execute update in a table with invalid schema", "/prest/0public/test", m, http.StatusBadRequest},
{"execute update in a table with invalid table", "/prest/public/0test", m, http.StatusBadRequest},
{"execute update in a table with invalid schema", "/prest/0public/test", m, http.StatusNotFound},
{"execute update in a table with invalid table", "/prest/public/0test", m, http.StatusNotFound},
{"execute update in a table with invalid where clause", "/prest/public/test?0name=$eq.nuveo", m, http.StatusBadRequest},
{"execute update in a table with invalid body", "/prest/public/test?name=$eq.nuveo", nil, http.StatusBadRequest},
}
Expand Down