Skip to content

Commit

Permalink
Formatters refactorized to commons (#6)
Browse files Browse the repository at this point in the history
Co-authored-by: ext_christleo <[email protected]>
  • Loading branch information
cdleo and ext_christleo committed Mar 28, 2022
1 parent 6bb0d49 commit edd16e8
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 85 deletions.
19 changes: 10 additions & 9 deletions e2h_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"
"runtime"

"github.com/cdleo/go-commons/formatter"
"github.com/cdleo/go-e2h"
e2hformat "github.com/cdleo/go-e2h/formatter"
)
Expand Down Expand Up @@ -39,7 +40,7 @@ func ExampleEnhancedError() {
params := e2hformat.Params{
Beautify: false,
InvertCallstack: true,
PathHidingMethod: e2hformat.HidingMethod_FullBaseline,
PathHidingMethod: formatter.HidingMethod_FullBaseline,
PathHidingValue: hideThisPath,
}

Expand Down Expand Up @@ -79,39 +80,39 @@ func ExampleEnhancedError() {
// Just cause => TheError
//
// Full info (inverted stack) =>
// github.com/cdleo/go-e2h_test.ExampleEnhancedError (e2h_example_test.go:46) [Error executing [bar()] function]; github.com/cdleo/go-e2h_test.bar (e2h_example_test.go:32) [Error executing foo()]; github.com/cdleo/go-e2h_test.foo (e2h_example_test.go:23); TheError;
// github.com/cdleo/go-e2h_test.ExampleEnhancedError (e2h_example_test.go:47) [Error executing [bar()] function]; github.com/cdleo/go-e2h_test.bar (e2h_example_test.go:33) [Error executing foo()]; github.com/cdleo/go-e2h_test.foo (e2h_example_test.go:24); TheError;
//
// Full info (beautified / inverted stack) =>
// github.com/cdleo/go-e2h_test.ExampleEnhancedError (e2h_example_test.go:46)
// github.com/cdleo/go-e2h_test.ExampleEnhancedError (e2h_example_test.go:47)
// Error executing [bar()] function
// github.com/cdleo/go-e2h_test.bar (e2h_example_test.go:32)
// github.com/cdleo/go-e2h_test.bar (e2h_example_test.go:33)
// Error executing foo()
// github.com/cdleo/go-e2h_test.foo (e2h_example_test.go:23)
// github.com/cdleo/go-e2h_test.foo (e2h_example_test.go:24)
// TheError
//
// **** JSON Formatter ****
//
// Just cause => {"error":"TheError"}
//
// Full info =>
// {"error":"TheError","stack_trace":[{"func":"github.com/cdleo/go-e2h_test.foo","caller":"e2h_example_test.go:23"},{"func":"github.com/cdleo/go-e2h_test.bar","caller":"e2h_example_test.go:32","context":"Error executing foo()"},{"func":"github.com/cdleo/go-e2h_test.ExampleEnhancedError","caller":"e2h_example_test.go:46","context":"Error executing [bar()] function"}]}
// {"error":"TheError","stack_trace":[{"func":"github.com/cdleo/go-e2h_test.foo","caller":"e2h_example_test.go:24"},{"func":"github.com/cdleo/go-e2h_test.bar","caller":"e2h_example_test.go:33","context":"Error executing foo()"},{"func":"github.com/cdleo/go-e2h_test.ExampleEnhancedError","caller":"e2h_example_test.go:47","context":"Error executing [bar()] function"}]}
//
// Full info (beautified) =>
// {
// "error": "TheError",
// "stack_trace": [
// {
// "func": "github.com/cdleo/go-e2h_test.foo",
// "caller": "e2h_example_test.go:23"
// "caller": "e2h_example_test.go:24"
// },
// {
// "func": "github.com/cdleo/go-e2h_test.bar",
// "caller": "e2h_example_test.go:32",
// "caller": "e2h_example_test.go:33",
// "context": "Error executing foo()"
// },
// {
// "func": "github.com/cdleo/go-e2h_test.ExampleEnhancedError",
// "caller": "e2h_example_test.go:46",
// "caller": "e2h_example_test.go:47",
// "context": "Error executing [bar()] function"
// }
// ]
Expand Down
45 changes: 23 additions & 22 deletions e2h_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"
"testing"

"github.com/cdleo/go-commons/formatter"
"github.com/cdleo/go-e2h"
e2hformat "github.com/cdleo/go-e2h/formatter"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -106,15 +107,15 @@ func TestEnhancedError_EnhErr_RawFormatter_Format(t *testing.T) {
_, b, _, _ := runtime.Caller(0)
hideThisPath := filepath.Dir(b) + string(os.PathSeparator)
params := e2hformat.Params{
PathHidingMethod: e2hformat.HidingMethod_FullBaseline,
PathHidingMethod: formatter.HidingMethod_FullBaseline,
PathHidingValue: hideThisPath,
}

// Execute
output := rawFormatter.Format(enhancedErr, params)

// Check
require.Equal(t, output, "This is a standard error; github.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_RawFormatter_Format (e2h_test.go:104) [Error wrapped with additional info];")
require.Equal(t, output, "This is a standard error; github.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_RawFormatter_Format (e2h_test.go:105) [Error wrapped with additional info];")
}

func TestEnhancedError_EnhErr_JSONFormatter_Format(t *testing.T) {
Expand All @@ -125,15 +126,15 @@ func TestEnhancedError_EnhErr_JSONFormatter_Format(t *testing.T) {
_, b, _, _ := runtime.Caller(0)
hideThisPath := filepath.Dir(b) + string(os.PathSeparator)
params := e2hformat.Params{
PathHidingMethod: e2hformat.HidingMethod_FullBaseline,
PathHidingMethod: formatter.HidingMethod_FullBaseline,
PathHidingValue: hideThisPath,
}

// Execute
output := jsonFormatter.Format(enhancedErr, params)

// Check
require.Equal(t, output, "{\"error\":\"This is a standard error\",\"stack_trace\":[{\"func\":\"github.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_JSONFormatter_Format\",\"caller\":\"e2h_test.go:123\",\"context\":\"Error wrapped with additional info\"}]}")
require.Equal(t, output, "{\"error\":\"This is a standard error\",\"stack_trace\":[{\"func\":\"github.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_JSONFormatter_Format\",\"caller\":\"e2h_test.go:124\",\"context\":\"Error wrapped with additional info\"}]}")
}

func TestEnhancedError_EnhErr_RawFormatter_Format_Beautified(t *testing.T) {
Expand All @@ -144,7 +145,7 @@ func TestEnhancedError_EnhErr_RawFormatter_Format_Beautified(t *testing.T) {
_, b, _, _ := runtime.Caller(0)
hideThisPath := filepath.Dir(b) + string(os.PathSeparator)
params := e2hformat.Params{
PathHidingMethod: e2hformat.HidingMethod_FullBaseline,
PathHidingMethod: formatter.HidingMethod_FullBaseline,
PathHidingValue: hideThisPath,
Beautify: true,
}
Expand All @@ -153,7 +154,7 @@ func TestEnhancedError_EnhErr_RawFormatter_Format_Beautified(t *testing.T) {
output := rawFormatter.Format(enhancedErr, params)

// Check
require.Equal(t, output, "This is a standard error\ngithub.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_RawFormatter_Format_Beautified (e2h_test.go:142)\n\tError wrapped with additional info")
require.Equal(t, output, "This is a standard error\ngithub.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_RawFormatter_Format_Beautified (e2h_test.go:143)\n\tError wrapped with additional info")
}

func TestEnhancedError_EnhErr_JSONFormatter_Format_Beautified(t *testing.T) {
Expand All @@ -164,7 +165,7 @@ func TestEnhancedError_EnhErr_JSONFormatter_Format_Beautified(t *testing.T) {
_, b, _, _ := runtime.Caller(0)
hideThisPath := filepath.Dir(b) + string(os.PathSeparator)
params := e2hformat.Params{
PathHidingMethod: e2hformat.HidingMethod_FullBaseline,
PathHidingMethod: formatter.HidingMethod_FullBaseline,
PathHidingValue: hideThisPath,
Beautify: true,
}
Expand All @@ -173,7 +174,7 @@ func TestEnhancedError_EnhErr_JSONFormatter_Format_Beautified(t *testing.T) {
output := jsonFormatter.Format(enhancedErr, params)

// Check
require.Equal(t, output, "{\n\t\"error\": \"This is a standard error\",\n\t\"stack_trace\": [\n\t\t{\n\t\t\t\"func\": \"github.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_JSONFormatter_Format_Beautified\",\n\t\t\t\"caller\": \"e2h_test.go:162\",\n\t\t\t\"context\": \"Error wrapped with additional info\"\n\t\t}\n\t]\n}")
require.Equal(t, output, "{\n\t\"error\": \"This is a standard error\",\n\t\"stack_trace\": [\n\t\t{\n\t\t\t\"func\": \"github.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_JSONFormatter_Format_Beautified\",\n\t\t\t\"caller\": \"e2h_test.go:163\",\n\t\t\t\"context\": \"Error wrapped with additional info\"\n\t\t}\n\t]\n}")
}

func TestEnhancedError_EnhErr_RawFormatter_Format_Inverted(t *testing.T) {
Expand All @@ -184,7 +185,7 @@ func TestEnhancedError_EnhErr_RawFormatter_Format_Inverted(t *testing.T) {
_, b, _, _ := runtime.Caller(0)
hideThisPath := filepath.Dir(b) + string(os.PathSeparator)
params := e2hformat.Params{
PathHidingMethod: e2hformat.HidingMethod_FullBaseline,
PathHidingMethod: formatter.HidingMethod_FullBaseline,
PathHidingValue: hideThisPath,
InvertCallstack: true,
}
Expand All @@ -193,7 +194,7 @@ func TestEnhancedError_EnhErr_RawFormatter_Format_Inverted(t *testing.T) {
output := rawFormatter.Format(enhancedErr, params)

// Check
require.Equal(t, output, "github.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_RawFormatter_Format_Inverted (e2h_test.go:182) [Error wrapped with additional info]; This is a standard error;")
require.Equal(t, output, "github.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_RawFormatter_Format_Inverted (e2h_test.go:183) [Error wrapped with additional info]; This is a standard error;")
}

func TestEnhancedError_EnhErr_JSONFormatter_Format_Inverted(t *testing.T) {
Expand All @@ -204,7 +205,7 @@ func TestEnhancedError_EnhErr_JSONFormatter_Format_Inverted(t *testing.T) {
_, b, _, _ := runtime.Caller(0)
hideThisPath := filepath.Dir(b) + string(os.PathSeparator)
params := e2hformat.Params{
PathHidingMethod: e2hformat.HidingMethod_FullBaseline,
PathHidingMethod: formatter.HidingMethod_FullBaseline,
PathHidingValue: hideThisPath,
InvertCallstack: true,
}
Expand All @@ -213,7 +214,7 @@ func TestEnhancedError_EnhErr_JSONFormatter_Format_Inverted(t *testing.T) {
output := jsonFormatter.Format(enhancedErr, params)

// Check
require.Equal(t, output, "{\"error\":\"This is a standard error\",\"stack_trace\":[{\"func\":\"github.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_JSONFormatter_Format_Inverted\",\"caller\":\"e2h_test.go:202\",\"context\":\"Error wrapped with additional info\"}]}")
require.Equal(t, output, "{\"error\":\"This is a standard error\",\"stack_trace\":[{\"func\":\"github.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_JSONFormatter_Format_Inverted\",\"caller\":\"e2h_test.go:203\",\"context\":\"Error wrapped with additional info\"}]}")
}

func TestEnhancedError_EnhErr_RawFormatter_Format_FullPathHidden(t *testing.T) {
Expand All @@ -224,15 +225,15 @@ func TestEnhancedError_EnhErr_RawFormatter_Format_FullPathHidden(t *testing.T) {
_, b, _, _ := runtime.Caller(0)
hideThisPath := filepath.Dir(b) + string(os.PathSeparator)
params := e2hformat.Params{
PathHidingMethod: e2hformat.HidingMethod_FullBaseline,
PathHidingMethod: formatter.HidingMethod_FullBaseline,
PathHidingValue: hideThisPath,
}

// Execute
output := rawFormatter.Format(enhancedErr, params)

// Check
require.Equal(t, output, "This is a standard error; github.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_RawFormatter_Format_FullPathHidden (e2h_test.go:222) [Error wrapped with additional info];")
require.Equal(t, output, "This is a standard error; github.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_RawFormatter_Format_FullPathHidden (e2h_test.go:223) [Error wrapped with additional info];")
}

func TestEnhancedError_EnhErr_JSONFormatter_Format_FullPathHidden(t *testing.T) {
Expand All @@ -243,15 +244,15 @@ func TestEnhancedError_EnhErr_JSONFormatter_Format_FullPathHidden(t *testing.T)
_, b, _, _ := runtime.Caller(0)
hideThisPath := filepath.Dir(b) + string(os.PathSeparator)
params := e2hformat.Params{
PathHidingMethod: e2hformat.HidingMethod_FullBaseline,
PathHidingMethod: formatter.HidingMethod_FullBaseline,
PathHidingValue: hideThisPath,
}

// Execute
output := jsonFormatter.Format(enhancedErr, params)

// Check
require.Equal(t, output, "{\"error\":\"This is a standard error\",\"stack_trace\":[{\"func\":\"github.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_JSONFormatter_Format_FullPathHidden\",\"caller\":\"e2h_test.go:241\",\"context\":\"Error wrapped with additional info\"}]}")
require.Equal(t, output, "{\"error\":\"This is a standard error\",\"stack_trace\":[{\"func\":\"github.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_JSONFormatter_Format_FullPathHidden\",\"caller\":\"e2h_test.go:242\",\"context\":\"Error wrapped with additional info\"}]}")
}

func TestEnhancedError_EnhErr_RawFormatter_Format_PartialPathHidden(t *testing.T) {
Expand All @@ -262,14 +263,14 @@ func TestEnhancedError_EnhErr_RawFormatter_Format_PartialPathHidden(t *testing.T
_, b, _, _ := runtime.Caller(0)
path := strings.Split(filepath.Dir(b), string(os.PathSeparator))
params := e2hformat.Params{
PathHidingMethod: e2hformat.HidingMethod_ToFolder,
PathHidingMethod: formatter.HidingMethod_ToFolder,
PathHidingValue: path[len(path)-1],
}

// Execute
output := rawFormatter.Format(enhancedErr, params)

want := strings.ReplaceAll("This is a standard error; github.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_RawFormatter_Format_PartialPathHidden (<LAST_DIR>/e2h_test.go:260) [Error wrapped with additional info];",
want := strings.ReplaceAll("This is a standard error; github.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_RawFormatter_Format_PartialPathHidden (<LAST_DIR>/e2h_test.go:261) [Error wrapped with additional info];",
"<LAST_DIR>", params.PathHidingValue)

// Check
Expand All @@ -284,14 +285,14 @@ func TestEnhancedError_EnhErr_JSONFormatter_Format_PartialPathHidden(t *testing.
_, b, _, _ := runtime.Caller(0)
path := strings.Split(filepath.Dir(b), string(os.PathSeparator))
params := e2hformat.Params{
PathHidingMethod: e2hformat.HidingMethod_ToFolder,
PathHidingMethod: formatter.HidingMethod_ToFolder,
PathHidingValue: path[len(path)-1],
}

// Execute
output := jsonFormatter.Format(enhancedErr, params)

want := strings.ReplaceAll("{\"error\":\"This is a standard error\",\"stack_trace\":[{\"func\":\"github.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_JSONFormatter_Format_PartialPathHidden\",\"caller\":\"<LAST_DIR>/e2h_test.go:282\",\"context\":\"Error wrapped with additional info\"}]}",
want := strings.ReplaceAll("{\"error\":\"This is a standard error\",\"stack_trace\":[{\"func\":\"github.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_JSONFormatter_Format_PartialPathHidden\",\"caller\":\"<LAST_DIR>/e2h_test.go:283\",\"context\":\"Error wrapped with additional info\"}]}",
"<LAST_DIR>", params.PathHidingValue)

// Check
Expand All @@ -309,13 +310,13 @@ func TestEnhancedError_EnhError_JSONFormatter_Format_MultipleTraces(t *testing.T
hideThisPath := filepath.Dir(b) + string(os.PathSeparator)
params := e2hformat.Params{
Beautify: true,
PathHidingMethod: e2hformat.HidingMethod_FullBaseline,
PathHidingMethod: formatter.HidingMethod_FullBaseline,
PathHidingValue: hideThisPath,
}

// Execute
output := jsonFormatter.Format(enhancedErr, params)

// Check
require.Equal(t, output, "{\n\t\"error\": \"This is a standard error\",\n\t\"stack_trace\": [\n\t\t{\n\t\t\t\"func\": \"github.com/cdleo/go-e2h_test.TestEnhancedError_EnhError_JSONFormatter_Format_MultipleTraces\",\n\t\t\t\"caller\": \"e2h_test.go:304\",\n\t\t\t\"context\": \"Error wrapped with additional info\"\n\t\t},\n\t\t{\n\t\t\t\"func\": \"github.com/cdleo/go-e2h_test.TestEnhancedError_EnhError_JSONFormatter_Format_MultipleTraces\",\n\t\t\t\"caller\": \"e2h_test.go:305\",\n\t\t\t\"context\": \"This is the 2nd. stack level\"\n\t\t},\n\t\t{\n\t\t\t\"func\": \"github.com/cdleo/go-e2h_test.TestEnhancedError_EnhError_JSONFormatter_Format_MultipleTraces\",\n\t\t\t\"caller\": \"e2h_test.go:306\"\n\t\t}\n\t]\n}")
require.Equal(t, output, "{\n\t\"error\": \"This is a standard error\",\n\t\"stack_trace\": [\n\t\t{\n\t\t\t\"func\": \"github.com/cdleo/go-e2h_test.TestEnhancedError_EnhError_JSONFormatter_Format_MultipleTraces\",\n\t\t\t\"caller\": \"e2h_test.go:305\",\n\t\t\t\"context\": \"Error wrapped with additional info\"\n\t\t},\n\t\t{\n\t\t\t\"func\": \"github.com/cdleo/go-e2h_test.TestEnhancedError_EnhError_JSONFormatter_Format_MultipleTraces\",\n\t\t\t\"caller\": \"e2h_test.go:306\",\n\t\t\t\"context\": \"This is the 2nd. stack level\"\n\t\t},\n\t\t{\n\t\t\t\"func\": \"github.com/cdleo/go-e2h_test.TestEnhancedError_EnhError_JSONFormatter_Format_MultipleTraces\",\n\t\t\t\"caller\": \"e2h_test.go:307\"\n\t\t}\n\t]\n}")
}
51 changes: 3 additions & 48 deletions formatter/formatterFactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ package e2hformat

import (
"fmt"
"path/filepath"
"strings"

"github.com/cdleo/go-commons/formatter"
)

type Format int8
Expand All @@ -17,24 +17,13 @@ const (
Format_JSON
)

type HidingMethod int8

//Allowed path treatments
const (
HidingMethod_None HidingMethod = iota

HidingMethod_FullBaseline

HidingMethod_ToFolder
)

type Params struct {
//Sets if the output will be beautified
Beautify bool
//Sets if at top of the stack shows the last trace (invert = true) or the origin error (invert = false)
InvertCallstack bool
//Sets the way in with the filepaths are managed.
PathHidingMethod HidingMethod
PathHidingMethod formatter.HidingMethod
//Value to use, according to the selected 'PathHidingMethod'
PathHidingValue string
}
Expand All @@ -56,37 +45,3 @@ func NewFormatter(format Format) (Formatter, error) {
}

}

// This function format the sourcefile according to the provided params
func formatSourceFile(file string, hidingMethod HidingMethod, hidingValue string) string {
switch hidingMethod {
case HidingMethod_FullBaseline:
return removePathSegment(file, hidingValue)
case HidingMethod_ToFolder:
return removeBeforeFolder(file, hidingValue)
default: //HidingMethod_None
return file
}
}

// Utility funtion that removes the first part of the filepath til the end of `baseline` path argument
func removePathSegment(file string, baseline string) string {

file = filepath.Clean(file)
prettyCaller := strings.ReplaceAll(file, baseline, "")
if len(prettyCaller) > 0 {
return prettyCaller
}

return file
}

// Utility funtion that removes the first part of the filepath til found the folder indicated in `newRootFolder` argument
func removeBeforeFolder(file string, newRootFolder string) string {

fileParts := strings.Split(file, newRootFolder)
if len(fileParts) < 2 {
return file
}
return newRootFolder + fileParts[1]
}
5 changes: 3 additions & 2 deletions formatter/jsonFormatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/json"
"fmt"

"github.com/cdleo/go-commons/formatter"
"github.com/cdleo/go-e2h"
)

Expand All @@ -26,9 +27,9 @@ type jsonSource struct {
Context string `json:"context,omitempty"`
}

func newJSONStack(item *e2h.StackDetails, hidingMethod HidingMethod, pathOrFolder string) jsonStack {
func newJSONStack(item *e2h.StackDetails, hidingMethod formatter.HidingMethod, pathOrFolder string) jsonStack {

filePath := formatSourceFile(item.File, hidingMethod, pathOrFolder)
filePath := formatter.FormatSourceFile(item.File, hidingMethod, pathOrFolder)

return jsonStack{
FuncName: item.FuncName,
Expand Down
3 changes: 2 additions & 1 deletion formatter/rawFormatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"strings"

"github.com/cdleo/go-commons/formatter"
"github.com/cdleo/go-e2h"
)

Expand Down Expand Up @@ -73,7 +74,7 @@ func (s *rawFormatter) Format(err error, params Params) string {

func (s *rawFormatter) formatItem(withInfoTrace string, withoutInfoTrace string, params Params, item e2h.StackDetails) string {

filePath := formatSourceFile(item.File, params.PathHidingMethod, params.PathHidingValue)
filePath := formatter.FormatSourceFile(item.File, params.PathHidingMethod, params.PathHidingValue)

if len(item.Message) > 0 {
return fmt.Sprintf(withInfoTrace, item.FuncName, filePath, item.Line, item.Message)
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ module github.com/cdleo/go-e2h

go 1.14

require github.com/stretchr/testify v1.7.0
require github.com/stretchr/testify v1.7.1

require (
github.com/cdleo/go-commons v0.0.0-20220328183115-77de79dd0070
github.com/davecgh/go-spew v1.1.1 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
Loading

0 comments on commit edd16e8

Please sign in to comment.