diff options
Diffstat (limited to 'src/http/apiutil')
-rw-r--r-- | src/http/apiutil/apiutil.go | 59 |
1 files changed, 26 insertions, 33 deletions
diff --git a/src/http/apiutil/apiutil.go b/src/http/apiutil/apiutil.go index 2151d83..a27d0d5 100644 --- a/src/http/apiutil/apiutil.go +++ b/src/http/apiutil/apiutil.go @@ -16,35 +16,13 @@ import ( "dev.mediocregopher.com/mediocre-go-lib.git/mlog" ) -// TODO I don't think Set/GetRequestLogger are necessary? Seems sufficient to -// just annotate the request's context - -type loggerCtxKey int - -// SetRequestLogger sets the given Logger onto the given Request's Context, -// returning a copy. -func SetRequestLogger(r *http.Request, logger *mlog.Logger) *http.Request { - ctx := r.Context() - ctx = context.WithValue(ctx, loggerCtxKey(0), logger) - return r.WithContext(ctx) -} - -// GetRequestLogger returns the Logger which was set by SetRequestLogger onto -// this Request, or nil. -func GetRequestLogger(r *http.Request) *mlog.Logger { - ctx := r.Context() - logger, _ := ctx.Value(loggerCtxKey(0)).(*mlog.Logger) - if logger == nil { - logger = mlog.Null - } - return logger -} - // JSONResult writes the JSON encoding of the given value as the response body. -func JSONResult(rw http.ResponseWriter, r *http.Request, v interface{}) { +func JSONResult( + ctx context.Context, logger *mlog.Logger, rw http.ResponseWriter, v any, +) { b, err := json.Marshal(v) if err != nil { - InternalServerError(rw, r, err) + InternalServerError(ctx, logger, rw, "%w", err) return } b = append(b, '\n') @@ -54,12 +32,20 @@ func JSONResult(rw http.ResponseWriter, r *http.Request, v interface{}) { } // BadRequest writes a 400 status and a JSON encoded error struct containing the -// given error as the response body. -func BadRequest(rw http.ResponseWriter, r *http.Request, err error) { - GetRequestLogger(r).Warn(r.Context(), "bad request", err) +// given error (created using `fmt.Errorf(fmtStr, args...)`) as the response +// body. +func BadRequest( + ctx context.Context, + logger *mlog.Logger, + rw http.ResponseWriter, + fmtStr string, + args ...any, +) { + err := fmt.Errorf(fmtStr, args...) + logger.Warn(ctx, "bad request", err) rw.WriteHeader(400) - JSONResult(rw, r, struct { + JSONResult(ctx, logger, rw, struct { Error string `json:"error"` }{ Error: err.Error(), @@ -69,11 +55,18 @@ func BadRequest(rw http.ResponseWriter, r *http.Request, err error) { // InternalServerError writes a 500 status and a JSON encoded error struct // containing a generic error as the response body (though it will log the given // one). -func InternalServerError(rw http.ResponseWriter, r *http.Request, err error) { - GetRequestLogger(r).Error(r.Context(), "internal server error", err) +func InternalServerError( + ctx context.Context, + logger *mlog.Logger, + rw http.ResponseWriter, + fmtStr string, + args ...any, +) { + err := fmt.Errorf(fmtStr, args...) + logger.Error(ctx, "internal server error", err) rw.WriteHeader(500) - JSONResult(rw, r, struct { + JSONResult(ctx, logger, rw, struct { Error string `json:"error"` }{ Error: "internal server error", |