diff options
author | pg9182 <96569817+pg9182@users.noreply.github.com> | 2022-10-15 20:30:55 -0400 |
---|---|---|
committer | pg9182 <96569817+pg9182@users.noreply.github.com> | 2022-10-15 20:30:55 -0400 |
commit | e0ac2504175e62179500c202e97aa98bba9769ca (patch) | |
tree | da71180e7a53268c7da99ff4f58d17ae8b4264f3 /pkg/api/api0/api.go | |
parent | f84e5fba3dc581bb96f9b61820b14462a49ea2b6 (diff) | |
download | Atlas-e0ac2504175e62179500c202e97aa98bba9769ca.tar.gz Atlas-e0ac2504175e62179500c202e97aa98bba9769ca.zip |
pkg/api/api0: Rework error responses
* NorthstarLauncher expects the error field to be an object with enum
and msg fields as strings, and often crashes if this isn't the case.
* Shorter code, less duplication.
Diffstat (limited to 'pkg/api/api0/api.go')
-rw-r--r-- | pkg/api/api0/api.go | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/pkg/api/api0/api.go b/pkg/api/api0/api.go index 818f6b9..398ac17 100644 --- a/pkg/api/api0/api.go +++ b/pkg/api/api0/api.go @@ -151,6 +151,23 @@ func (h *Handler) checkLauncherVersion(r *http.Request) bool { return semver.Compare(rver, mver) >= 0 } +// respFail writes a {success:false,error:ErrorObj} response with the provided +// response status. +func respFail(w http.ResponseWriter, r *http.Request, status int, obj ErrorObj) { + if rid, ok := hlog.IDFromRequest(r); ok { + respJSON(w, r, status, map[string]any{ + "success": false, + "error": obj, + "request_id": rid.String(), + }) + } else { + respJSON(w, r, status, map[string]any{ + "success": false, + "error": obj, + }) + } +} + // respJSON writes the JSON encoding of obj with the provided response status. func respJSON(w http.ResponseWriter, r *http.Request, status int, obj any) { if r.Method == http.MethodHead { |