From eb2f42d50a53613e59ea37b890e45651f420e4e0 Mon Sep 17 00:00:00 2001 From: Drew Bednar Date: Sat, 11 Jan 2025 14:39:36 -0500 Subject: [PATCH] Add helper function to errors --- error.go | 8 ++++++++ error_test.go | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/error.go b/error.go index e577a1c..4645d41 100644 --- a/error.go +++ b/error.go @@ -58,3 +58,11 @@ func ErrorMessage(err error) string { } return INTERNAL_ERROR_MESSAGE } + +// Errorf is a helper function to return an Error with a given code and format +func Errorf(code string, format string, args ...interface{}) *Error { + return &Error{ + Code: code, + Message: fmt.Sprintf(format, args...), + } +} diff --git a/error_test.go b/error_test.go index ec1deac..20917c0 100644 --- a/error_test.go +++ b/error_test.go @@ -49,7 +49,7 @@ func TestErrorMessage(t *testing.T) { }) t.Run("should return application error message", func(t *testing.T) { - e := &Error{Message: "my_message"} + e := Errorf(ENOTFOUND, "Entity %s not found", "dirp") got := ErrorMessage(e) AssertErrorString(t, got, e.Message)