diff options
author | pg9182 <96569817+pg9182@users.noreply.github.com> | 2022-10-13 12:05:24 -0400 |
---|---|---|
committer | pg9182 <96569817+pg9182@users.noreply.github.com> | 2022-10-13 12:05:24 -0400 |
commit | f2ca8dfe327f87672f0f82ac9318c5bf7d2243a0 (patch) | |
tree | aadc2552653410122310adc8c4eab456b06202a9 | |
parent | 2acb97145a08f0ff7f07f95ae077e868b5bb687d (diff) | |
download | Atlas-f2ca8dfe327f87672f0f82ac9318c5bf7d2243a0.tar.gz Atlas-f2ca8dfe327f87672f0f82ac9318c5bf7d2243a0.zip |
pkg/api/api0: Refactor error message formatting
-rw-r--r-- | pkg/api/api0/errors.go | 7 | ||||
-rw-r--r-- | pkg/api/api0/playerinfo.go | 9 |
2 files changed, 11 insertions, 5 deletions
diff --git a/pkg/api/api0/errors.go b/pkg/api/api0/errors.go index ad58224..2d06719 100644 --- a/pkg/api/api0/errors.go +++ b/pkg/api/api0/errors.go @@ -1,5 +1,7 @@ package api0 +import "fmt" + // ErrorCode represents a known Northstar error code. type ErrorCode string @@ -59,3 +61,8 @@ func (n ErrorCode) Message() string { return string(n) } } + +// Messagef returns Message() with additional text appended after ": ". +func (n ErrorCode) Messagef(format string, a ...interface{}) string { + return n.Message() + ": " + fmt.Sprintf(format, a...) +} diff --git a/pkg/api/api0/playerinfo.go b/pkg/api/api0/playerinfo.go index b47ef4c..9a35bf3 100644 --- a/pkg/api/api0/playerinfo.go +++ b/pkg/api/api0/playerinfo.go @@ -3,7 +3,6 @@ package api0 import ( "crypto/sha256" "encoding/hex" - "fmt" "net/http" "strconv" "time" @@ -84,7 +83,7 @@ func (h *Handler) handlePlayer(w http.ResponseWriter, r *http.Request) { respJSON(w, r, http.StatusBadRequest, map[string]any{ "success": false, "error": ErrorCode_BAD_REQUEST, - "msg": fmt.Sprintf("%s: id param is required", ErrorCode_BAD_REQUEST.Message()), + "msg": ErrorCode_BAD_REQUEST.Messagef("id param is required"), }) return } @@ -127,7 +126,7 @@ func (h *Handler) handlePlayer(w http.ResponseWriter, r *http.Request) { respJSON(w, r, http.StatusInternalServerError, map[string]any{ "success": false, "error": ErrorCode_INTERNAL_SERVER_ERROR, - "msg": fmt.Sprintf("%s: failed to read pdata hash from storage", ErrorCode_INTERNAL_SERVER_ERROR.Message()), + "msg": ErrorCode_INTERNAL_SERVER_ERROR.Message(), }) return } @@ -152,7 +151,7 @@ func (h *Handler) handlePlayer(w http.ResponseWriter, r *http.Request) { respJSON(w, r, http.StatusInternalServerError, map[string]any{ "success": false, "error": ErrorCode_INTERNAL_SERVER_ERROR, - "msg": fmt.Sprintf("%s: failed to parse pdata from storage", ErrorCode_INTERNAL_SERVER_ERROR.Message()), + "msg": ErrorCode_INTERNAL_SERVER_ERROR.Messagef("failed to parse pdata from storage"), }) return } @@ -167,7 +166,7 @@ func (h *Handler) handlePlayer(w http.ResponseWriter, r *http.Request) { respJSON(w, r, http.StatusInternalServerError, map[string]any{ "success": false, "error": ErrorCode_INTERNAL_SERVER_ERROR, - "msg": fmt.Sprintf("%s: failed to encode pdata as json", ErrorCode_INTERNAL_SERVER_ERROR.Message()), + "msg": ErrorCode_INTERNAL_SERVER_ERROR.Messagef("failed to encode pdata as json"), }) return } |