Skip to content

Commit a642e14

Browse files
author
Gabriel Cataldo
committed
Bug fix in the Details function where I was unable to translate the message into fields due to regex.
1 parent 469b3b9 commit a642e14

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Go Errors Detail
33
<!--suppress ALL -->
44
<img align="right" src="gopher-debug.png" alt="">
55

6-
[![Project status](https://img.shields.io/badge/version-v1.1.3-vividgreen.svg)](https://github.com/GabrielHCataldo/go-errors/releases/tag/v1.1.3)
6+
[![Project status](https://img.shields.io/badge/version-v1.1.4-vividgreen.svg)](https://github.com/GabrielHCataldo/go-errors/releases/tag/v1.1.4)
77
[![Go Report Card](https://goreportcard.com/badge/github.com/GabrielHCataldo/go-errors)](https://goreportcard.com/report/github.com/GabrielHCataldo/go-errors)
88
[![Coverage Status](https://coveralls.io/repos/GabrielHCataldo/go-errors/badge.svg?branch=main&service=github)](https://coveralls.io/github/GabrielHCataldo/go-errors?branch=main)
99
[![Open Source Helpers](https://www.codetriage.com/gabrielhcataldo/go-errors/badges/users.svg)](https://www.codetriage.com/gabrielhcataldo/go-errors)

errors/errors.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"strings"
1111
)
1212

13-
const regexErrorDetail = `CAUSE: \(([^:]+):(\d+)\) (.+): (.+) STACK:\s*(.+)`
13+
const regexErrorDetail = `\[CAUSE]: \(([^:]+):(\d+)\) ([^:]+): (.+?) \[STACK]:\s*(.+)`
1414

1515
type ErrorDetail struct {
1616
file string
@@ -94,7 +94,7 @@ func NotContains(err, target error) bool {
9494

9595
// Error print the error as a string, genetic implementation of error in go
9696
func (e *ErrorDetail) Error() string {
97-
return fmt.Sprint("CAUSE: ", e.GetCause(), " STACK: \n", e.debugStack)
97+
return fmt.Sprint("[CAUSE]: ", e.GetCause(), " [STACK]: \n", e.debugStack)
9898
}
9999

100100
// PrintStackTrace print red message with detail error and debug stack
@@ -178,5 +178,8 @@ func Details(err error) *ErrorDetail {
178178
}
179179

180180
func printMessage(v ...any) string {
181-
return helper.Sprintln(v...)
181+
msg := helper.Sprintln(v...)
182+
msg = strings.ReplaceAll(msg, "[STACK]", "")
183+
msg = strings.ReplaceAll(msg, "[CAUSE]", "")
184+
return msg
182185
}

errors/errors_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,18 @@ func TestErrorGetDebugStack(t *testing.T) {
100100
}
101101

102102
func TestIsErrorDetail(t *testing.T) {
103-
err := New("test error detail")
103+
err := New("test error detail:", 1, "test empty:", "another test", true)
104+
logger.Info("err:", IsErrorDetail(err))
105+
}
106+
107+
func TestDetail(t *testing.T) {
108+
err := New("test error detail:", 1, "test empty:", "another test", true)
104109
logger.Info("err:", IsErrorDetail(err))
105110
}
106111

107112
func TestDetails(t *testing.T) {
113+
err := New("test error detail:", nil, "test empty:", "another test: - STACK", true, "empty:")
114+
logger.Info("err details:", Details(err))
108115
logger.Info("err details:", Details(nil))
109116
logger.Info("err details:", Details(errors.New("test")))
110117
logger.Info("err details:", Details(New("test")))

0 commit comments

Comments
 (0)