From 81e5aa3fd7eae942115405c7cd4d3da5b7694237 Mon Sep 17 00:00:00 2001 From: Rheisen Date: Sat, 23 Mar 2024 12:59:33 -0400 Subject: [PATCH] Add errors test case, check for sensitive attachments --- error_struct.go | 2 ++ error_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/error_struct.go b/error_struct.go index b42d3d8..dcd8efc 100644 --- a/error_struct.go +++ b/error_struct.go @@ -18,6 +18,8 @@ func newBerr(errorType ErrorType, errorMessage string, attachments ...Attachment next = fmt.Errorf("%s: %w", next, d.Value().(error)) case d.Type() == AttachmentMetadataType: errorMetadata[d.Key()] = d.Value() + case d.Sensitive() == true: + errorMetadata[d.Key()] = d.Value() default: errorDetail[d.Key()] = d.Value() } diff --git a/error_test.go b/error_test.go index 7ff7f73..78a218a 100644 --- a/error_test.go +++ b/error_test.go @@ -195,6 +195,35 @@ func TestTimeoutError(t *testing.T) { testErrorFunc(t, berr.TimeoutErrorType, berr.Timeout, "request cancelled") } +func TestErrorsFormatting(t *testing.T) { + const unimplementedErrorMessage = "store.GetData method not implemented" + + const applicationErrorMessage = "unexpected problem querying database" + + errs := berr.Errors{ + berr.Unimplemented(unimplementedErrorMessage), + berr.Application(applicationErrorMessage), + } + + errorString := errs.Error() + + if !strings.Contains(errorString, unimplementedErrorMessage) { + t.Errorf("expected error string to contain '%s', found: %s\n", unimplementedErrorMessage, errorString) + } + + if !strings.Contains(errorString, applicationErrorMessage) { + t.Errorf("expected error string to contain '%s', found: %s\n", applicationErrorMessage, errorString) + } + + if !strings.HasPrefix(errorString, "[") { + t.Errorf("expected error string to start with '['\n") + } + + if !strings.HasSuffix(errorString, "]") { + t.Errorf("expected error string to start with ']'\n") + } +} + func testErrorFunc( t *testing.T, errorType berr.ErrorType,