From 6427f803869ab82409337deefb9a36f1cf906ae5 Mon Sep 17 00:00:00 2001 From: Lennart Altenhof Date: Wed, 12 Jul 2023 19:03:56 +0200 Subject: [PATCH] feat(mehhttp): add func for retrieving http status code by set mapping --- mehhttp/mehhttp.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/mehhttp/mehhttp.go b/mehhttp/mehhttp.go index fcd065e..0e0c8de 100644 --- a/mehhttp/mehhttp.go +++ b/mehhttp/mehhttp.go @@ -31,6 +31,13 @@ func SetHTTPStatusCodeMapping(mapping HTTPStatusCodeMapper) { httpStatusCodeMapper = mapping } +// HTTPStatusCode retrieves the HTTP status code for the given error. +func HTTPStatusCode(e error) int { + httpStatusCodeMapperMutex.RLock() + defer httpStatusCodeMapperMutex.RUnlock() + return httpStatusCodeMapper(meh.ErrorCode(e)) +} + const ( // ErrCommunication is used for all problems regarding client communication. As // communication is unstable by nature, this should not be reported as classic @@ -54,9 +61,7 @@ func LogAndRespondError(logger *zap.Logger, w http.ResponseWriter, r *http.Reque "http_req_remote_addr": r.RemoteAddr, }) mehlog.Log(logger, e) - httpStatusCodeMapperMutex.RLock() - httpStatus := httpStatusCodeMapper(meh.ErrorCode(e)) - httpStatusCodeMapperMutex.RUnlock() + httpStatus := HTTPStatusCode(e) err := respondHTTP(w, "", httpStatus) if err != nil { mehlog.Log(logger, meh.Wrap(err, "respond http", meh.Details{