Unwrap errors in json log output

dev
Alexey Zhuchkov 2021-10-30 13:46:07 +03:00
parent 463c1c0142
commit 1eb0378952
3 changed files with 36 additions and 2 deletions

View File

@ -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"
}

View File

@ -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
}

View File

@ -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))
}