Skip to content

Commit 1cdbdbc

Browse files
committed
Better errors
1 parent 48e505f commit 1cdbdbc

3 files changed

Lines changed: 51 additions & 15 deletions

File tree

errors.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package sqliteadmin
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
)
7+
8+
var (
9+
ErrMissingTableName = errors.New("missing table name")
10+
ErrMissingRow = errors.New("missing row")
11+
ErrInvalidOrMissingIds = errors.New("invalid or missing ids")
12+
ErrInvalidInput = errors.New("invalid input")
13+
)
14+
15+
type ApiError struct {
16+
StatusCode int `json:"statusCode"`
17+
Message string `json:"message"`
18+
}
19+
20+
func (e ApiError) Error() string {
21+
return fmt.Sprintf("api error: %d, %s", e.StatusCode, e.Message)
22+
}
23+
24+
func apiErrUnauthorized() ApiError {
25+
return ApiError{StatusCode: 401, Message: "Invalid credentials"}
26+
}
27+
28+
func apiErrBadRequest(details string) ApiError {
29+
return ApiError{StatusCode: 400, Message: "Bad request: " + details}
30+
}
31+
32+
func apiErrSomethingWentWrong() ApiError {
33+
return ApiError{StatusCode: 500, Message: "Something went wrong"}
34+
}

queryhandlers.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (h *Handler) getTable(w http.ResponseWriter, params map[string]interface{})
4141
// Parse table name
4242
table, ok := params["tableName"].(string)
4343
if !ok {
44-
writeError(w, fmt.Errorf("Missing table name"), http.StatusBadRequest)
44+
writeError(w, apiErrBadRequest(ErrMissingTableName.Error()))
4545
return
4646
}
4747

@@ -82,7 +82,7 @@ func (h *Handler) getTable(w http.ResponseWriter, params map[string]interface{})
8282
if err != nil {
8383
// TODO: use better logging
8484
fmt.Printf("Error querying table: %v\n", err)
85-
writeError(w, fmt.Errorf("Something went wrong"), http.StatusInternalServerError)
85+
writeError(w, apiErrSomethingWentWrong())
8686
return
8787
}
8888
response := map[string]interface{}{"rows": data}
@@ -92,7 +92,7 @@ func (h *Handler) getTable(w http.ResponseWriter, params map[string]interface{})
9292
if err != nil {
9393
// TODO: use better logging
9494
fmt.Printf("Error getting table info: %v\n", err)
95-
writeError(w, fmt.Errorf("Something went wrong"), http.StatusInternalServerError)
95+
writeError(w, apiErrSomethingWentWrong())
9696
return
9797
}
9898
response["tableInfo"] = tableInfo
@@ -104,35 +104,35 @@ func (h *Handler) getTable(w http.ResponseWriter, params map[string]interface{})
104104
func (h *Handler) deleteRows(w http.ResponseWriter, params map[string]interface{}) {
105105
table, ok := params["tableName"].(string)
106106
if !ok {
107-
writeError(w, fmt.Errorf("Missing table name"), http.StatusBadRequest)
107+
writeError(w, apiErrBadRequest(ErrMissingTableName.Error()))
108108
return
109109
}
110110

111111
ids, ok := convertToStrSlice(params["ids"])
112112
if !ok {
113-
writeError(w, fmt.Errorf("Invalid or missing ids"), http.StatusBadRequest)
113+
writeError(w, apiErrBadRequest(ErrInvalidOrMissingIds.Error()))
114114
return
115115
}
116116

117117
exists, err := checkTableExists(h.db, table)
118118
if err != nil {
119119
// TODO: use better logging
120120
fmt.Printf("Error checking table existence: %v\n", err)
121-
writeError(w, fmt.Errorf("Bad Request"), http.StatusBadRequest)
121+
writeError(w, apiErrSomethingWentWrong())
122122
return
123123
}
124124
if !exists {
125125
// TODO: use better logging
126126
fmt.Printf("Error table does not exist: %s\n", table)
127-
writeError(w, fmt.Errorf("Bad Request"), http.StatusBadRequest)
127+
writeError(w, apiErrBadRequest(ErrInvalidInput.Error()))
128128
return
129129
}
130130

131131
rowsAffected, err := batchDelete(h.db, table, ids)
132132
if err != nil {
133133
// TODO: use better logging
134134
fmt.Printf("Error deleting rows from table: %v\n", err)
135-
writeError(w, fmt.Errorf("Something went wrong"), http.StatusInternalServerError)
135+
writeError(w, apiErrSomethingWentWrong())
136136
return
137137
}
138138

@@ -142,19 +142,21 @@ func (h *Handler) deleteRows(w http.ResponseWriter, params map[string]interface{
142142
func (h *Handler) updateRow(w http.ResponseWriter, params map[string]interface{}) {
143143
table, ok := params["tableName"].(string)
144144
if !ok {
145-
writeError(w, fmt.Errorf("Missing table name"), http.StatusBadRequest)
145+
writeError(w, apiErrBadRequest(ErrMissingTableName.Error()))
146146
return
147147
}
148148

149149
row, ok := params["row"].(map[string]interface{})
150150
if !ok {
151-
writeError(w, fmt.Errorf("Missing row"), http.StatusBadRequest)
151+
writeError(w, apiErrBadRequest(ErrMissingRow.Error()))
152152
return
153153
}
154154

155155
err := editRow(h.db, table, row)
156156
if err != nil {
157-
writeError(w, fmt.Errorf("Failed to update row"), http.StatusInternalServerError)
157+
// TODO: use better logging
158+
fmt.Printf("Error editing row: %v\n", err)
159+
writeError(w, apiErrSomethingWentWrong())
158160
return
159161
}
160162

@@ -478,9 +480,9 @@ func editRow(db *sql.DB, tableName string, row map[string]interface{}) error {
478480
return nil
479481
}
480482

481-
func writeError(w http.ResponseWriter, err error, status int) {
482-
w.WriteHeader(status)
483-
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
483+
func writeError(w http.ResponseWriter, err ApiError) {
484+
w.WriteHeader(err.StatusCode)
485+
json.NewEncoder(w).Encode(err)
484486
}
485487

486488
func convertToStrSlice(val interface{}) ([]any, bool) {

sqliteadmin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (h *Handler) Handle(w http.ResponseWriter, r *http.Request) {
9494
if h.username != "" && h.password != "" {
9595
authHeader := r.Header.Get("Authorization")
9696
if h.username+":"+h.password != authHeader {
97-
w.WriteHeader(http.StatusUnauthorized)
97+
writeError(w, apiErrUnauthorized())
9898
return
9999
}
100100
}

0 commit comments

Comments
 (0)