From e0ac2504175e62179500c202e97aa98bba9769ca Mon Sep 17 00:00:00 2001 From: pg9182 <96569817+pg9182@users.noreply.github.com> Date: Sat, 15 Oct 2022 20:30:55 -0400 Subject: 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. --- pkg/api/api0/api.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'pkg/api/api0/api.go') 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 { -- cgit v1.2.3