Skip to content

Commit

Permalink
Fix logger
Browse files Browse the repository at this point in the history
  • Loading branch information
mpraski committed Sep 8, 2022
1 parent f5348b0 commit bf4c3ac
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
10 changes: 5 additions & 5 deletions logger/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type ValidationError struct {
Details validator.ValidationErrors
}

var errorMap = map[string]int{}
var errorMap = map[error]int{}

var (
matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
Expand All @@ -22,11 +22,11 @@ var (

func RegisterErrors(m map[error]int) {
for k, v := range m {
errorMap[k.Error()] = v
errorMap[k] = v
}
}

func FailValidation(root, details error) ValidationError {
func FailValidation(root, details error) *ValidationError {
var v ValidationError

v.Root = root
Expand All @@ -35,10 +35,10 @@ func FailValidation(root, details error) ValidationError {
v.Details = e
}

return v
return &v
}

func (e ValidationError) Error() string {
func (e *ValidationError) Error() string {
var sb strings.Builder

if e.Root != nil {
Expand Down
8 changes: 4 additions & 4 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,24 @@ func (l Logger) LogResponseMessage(w http.ResponseWriter, r *http.Request, s int
func (l Logger) LogServiceError(w http.ResponseWriter, r *http.Request, e error) {
var (
u = unwrap(e)
m = e.Error()
m = u.Error()
s = http.StatusInternalServerError
f []apiField
)

if v, ok := errorMap[e.Error()]; ok {
if v, ok := errorMap[u]; ok {
s = v
}

switch g := u.(type) {
case ValidationError:
case *ValidationError:
e = g

if g.Root != nil {
n := unwrap(g.Root)
m = n.Error()

if v, ok := errorMap[n.Error()]; ok {
if v, ok := errorMap[n]; ok {
s = v
}
}
Expand Down
2 changes: 1 addition & 1 deletion logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TestLogServiceError(t *testing.T) {
name: "validation error",
severity: logging.Error,
statusCode: http.StatusInternalServerError,
err: blueLogging.ValidationError{Root: errors.New("root error")},
err: &blueLogging.ValidationError{Root: errors.New("root error")},
msg: "root error",
},
}
Expand Down

0 comments on commit bf4c3ac

Please sign in to comment.