Skip to content

Commit 45a7688

Browse files
authored
Merge pull request #646 from projectdiscovery/bugfix-panics-in-unwrapping
Guarding vs external calls
2 parents 8dce051 + e8c3909 commit 45a7688

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

errkit/errors.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,23 @@ func (e *ErrorX) SetAttr(s ...slog.Attr) *ErrorX {
313313

314314
// parseError recursively parses all known types of errors
315315
func parseError(to *ErrorX, err error) {
316+
// guard against panics in external libraries calls
317+
defer func() {
318+
if r := recover(); r != nil {
319+
// Convert panic to error and append it as the last error
320+
var panicErr error
321+
switch v := r.(type) {
322+
case error:
323+
panicErr = fmt.Errorf("error while unwrapping: %w", v)
324+
case string:
325+
panicErr = fmt.Errorf("error while unwrapping: %s", v)
326+
default:
327+
panicErr = fmt.Errorf("error while unwrapping: panic: %v", r)
328+
}
329+
to.append(panicErr)
330+
}
331+
}()
332+
316333
if err == nil {
317334
return
318335
}
@@ -393,7 +410,7 @@ func parseError(to *ErrorX, err error) {
393410
parseError(to, errors.New(part))
394411
}
395412
} else {
396-
// this cannot be furthur unwrapped
413+
// this cannot be further unwrapped
397414
to.append(err)
398415
}
399416
}

0 commit comments

Comments
 (0)