From fd29c35b1a57100b15fde31cb2b36b2e856c535d Mon Sep 17 00:00:00 2001 From: Tigor Hutasuhut Date: Wed, 24 Apr 2024 22:26:04 +0700 Subject: [PATCH] http: better error messages --- pkg/errs/query.go | 8 ++++++++ rest/devices/create.http | 6 +++--- rest/devices/update.http | 3 ++- server/routes/device_create.go | 5 +++-- server/routes/device_list.go | 9 ++++++--- server/routes/device_update.go | 5 +++-- 6 files changed, 25 insertions(+), 11 deletions(-) diff --git a/pkg/errs/query.go b/pkg/errs/query.go index b768aea..358cb94 100644 --- a/pkg/errs/query.go +++ b/pkg/errs/query.go @@ -30,3 +30,11 @@ func FindMessage(err error) string { return err.Error() } + +func HTTPMessage(err error) (code int, message string) { + code = FindCodeOrDefault(err, 500) + if code >= 500 { + return code, err.Error() + } + return code, FindMessage(err) +} diff --git a/rest/devices/create.http b/rest/devices/create.http index dd39dac..da80778 100644 --- a/rest/devices/create.http +++ b/rest/devices/create.http @@ -1,11 +1,11 @@ POST http://localhost:8080/api/v1/devices HTTP/1.1 Host: localhost:8080 Content-Type: application/json -Content-Length: 155 +Content-Length: 160 { - "name": "Laptop", - "slug": "laptop", + "name": "Sync Laptop", + "slug": "sync-l", "resolution_x": 1920, "resolution_y": 1080, "nsfw": 1, diff --git a/rest/devices/update.http b/rest/devices/update.http index 1ff95f9..d16eddb 100644 --- a/rest/devices/update.http +++ b/rest/devices/update.http @@ -1,9 +1,10 @@ PATCH http://localhost:8080/api/v1/devices/1 HTTP/1.1 Host: localhost:8080 Content-Type: application/json -Content-Length: 55 +Content-Length: 78 { + "slug": "sync-l", "aspect_ratio_tolerance": 0.2, "nsfw": 1 } diff --git a/server/routes/device_create.go b/server/routes/device_create.go index 535a63e..70000c3 100644 --- a/server/routes/device_create.go +++ b/server/routes/device_create.go @@ -48,8 +48,9 @@ func (routes *Routes) APIDeviceCreate(rw http.ResponseWriter, r *http.Request) { WindowsWallpaperMode: omit.From(body.WindowsWallpaperMode), }) if err != nil { - rw.WriteHeader(errs.FindCodeOrDefault(err, http.StatusInternalServerError)) - _ = json.NewEncoder(rw).Encode(map[string]string{"error": errs.FindMessage(err)}) + code, message := errs.HTTPMessage(err) + rw.WriteHeader(code) + _ = json.NewEncoder(rw).Encode(map[string]string{"error": message}) return } diff --git a/server/routes/device_list.go b/server/routes/device_list.go index 8e7513d..edf171d 100644 --- a/server/routes/device_list.go +++ b/server/routes/device_list.go @@ -4,8 +4,10 @@ import ( "encoding/json" "net/http" "strconv" + "strings" "github.com/tigorlazuardi/redmage/api" + "github.com/tigorlazuardi/redmage/pkg/errs" "github.com/tigorlazuardi/redmage/pkg/log" ) @@ -17,8 +19,9 @@ func (routes *Routes) APIDeviceList(rw http.ResponseWriter, r *http.Request) { result, err := routes.API.DevicesList(ctx, query) if err != nil { - rw.WriteHeader(http.StatusInternalServerError) - _ = json.NewEncoder(rw).Encode(map[string]string{"error": err.Error()}) + code, message := errs.HTTPMessage(err) + rw.WriteHeader(code) + _ = json.NewEncoder(rw).Encode(map[string]string{"error": message}) return } @@ -33,7 +36,7 @@ func parseApiDeviceListQueries(req *http.Request) (params api.DevicesListParams) params.Limit, _ = strconv.ParseInt(req.FormValue("limit"), 10, 64) params.Q = req.FormValue("q") params.OrderBy = req.FormValue("order") - params.Sort = req.FormValue("sort") + params.Sort = strings.ToLower(req.FormValue("sort")) if params.Limit < 1 { params.Limit = 10 diff --git a/server/routes/device_update.go b/server/routes/device_update.go index 5d6d0d4..dd52f72 100644 --- a/server/routes/device_update.go +++ b/server/routes/device_update.go @@ -61,8 +61,9 @@ func (routes *Routes) APIDeviceUpdate(rw http.ResponseWriter, r *http.Request) { WindowsWallpaperMode: omit.FromPtr(body.WindowsWallpaperMode), }) if err != nil { - rw.WriteHeader(errs.FindCodeOrDefault(err, http.StatusInternalServerError)) - _ = json.NewEncoder(rw).Encode(map[string]string{"error": errs.FindMessage(err)}) + code, message := errs.HTTPMessage(err) + rw.WriteHeader(code) + _ = json.NewEncoder(rw).Encode(map[string]string{"error": message}) return }