From 1eb0378952b576db290fa15fc87fc6904ace13e5 Mon Sep 17 00:00:00 2001 From: Alexey Zhuchkov Date: Sat, 30 Oct 2021 13:46:07 +0300 Subject: [PATCH] Unwrap errors in json log output --- v2/pkg/output/output.go | 5 +++-- v2/pkg/utils/utils.go | 12 ++++++++++++ v2/pkg/utils/utils_test.go | 21 +++++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 v2/pkg/utils/utils_test.go diff --git a/v2/pkg/output/output.go b/v2/pkg/output/output.go index 57c841a8..0b0cbf02 100644 --- a/v2/pkg/output/output.go +++ b/v2/pkg/output/output.go @@ -16,6 +16,7 @@ import ( "github.com/projectdiscovery/nuclei/v2/pkg/model" "github.com/projectdiscovery/nuclei/v2/pkg/model/types/severity" "github.com/projectdiscovery/nuclei/v2/pkg/operators" + "github.com/projectdiscovery/nuclei/v2/pkg/utils" ) // Writer is an interface which writes output to somewhere for nuclei events. @@ -192,8 +193,8 @@ func (w *StandardWriter) Request(templatePath, input, requestType string, reques Input: input, Type: requestType, } - if requestErr != nil { - request.Error = requestErr.Error() + if unwrappedErr := utils.UnwrapError(requestErr); unwrappedErr != nil { + request.Error = unwrappedErr.Error() } else { request.Error = "none" } diff --git a/v2/pkg/utils/utils.go b/v2/pkg/utils/utils.go index 8d6fc0cf..ee924761 100644 --- a/v2/pkg/utils/utils.go +++ b/v2/pkg/utils/utils.go @@ -1,6 +1,7 @@ package utils import ( + "errors" "strings" ) @@ -11,3 +12,14 @@ func IsBlank(value string) bool { func IsNotBlank(value string) bool { return !IsBlank(value) } + +func UnwrapError(err error) error { + for { // get the last wrapped error + unwrapped := errors.Unwrap(err) + if unwrapped == nil { + break + } + err = unwrapped + } + return err +} diff --git a/v2/pkg/utils/utils_test.go b/v2/pkg/utils/utils_test.go new file mode 100644 index 00000000..ede9d530 --- /dev/null +++ b/v2/pkg/utils/utils_test.go @@ -0,0 +1,21 @@ +package utils + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestUnwrapError(t *testing.T) { + require.Equal(t, nil, UnwrapError(nil)) + + errOne := fmt.Errorf("error one") + require.Equal(t, errOne, UnwrapError(errOne)) + + errTwo := fmt.Errorf("error with error: %w", errOne) + require.Equal(t, errOne, UnwrapError(errTwo)) + + errThree := fmt.Errorf("error with error: %w", errTwo) + require.Equal(t, errOne, UnwrapError(errThree)) +}