Skip to content

Commit

Permalink
Merge pull request #26 from lefinal/add-mehhttp-exports
Browse files Browse the repository at this point in the history
feat(mehhttp): add func for retrieving http status code by set mapping
  • Loading branch information
lefinal authored Jul 12, 2023
2 parents 030c8ef + 6427f80 commit 53a7761
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions mehhttp/mehhttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{
Expand Down

0 comments on commit 53a7761

Please sign in to comment.