From 0548e8c67dc6c635b98f8f643d0af516d34c0860 Mon Sep 17 00:00:00 2001 From: Greg Weber Date: Mon, 10 Sep 2018 19:46:30 -0700 Subject: [PATCH] add an Errors utility helper (#8) --- group.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/group.go b/group.go index 003932c..e5a969a 100644 --- a/group.go +++ b/group.go @@ -6,6 +6,15 @@ type ErrorGroup interface { Errors() []error } +// Errors uses the ErrorGroup interface to return a slice of errors. +// If the ErrorGroup interface is not implemented it returns an array containing just the given error. +func Errors(err error) []error { + if eg, ok := err.(ErrorGroup); ok { + return eg.Errors() + } + return []error{err} +} + // WalkDeep does a depth-first traversal of all errors. // Any ErrorGroup is traversed (after going deep). // The visitor function can return true to end the traversal early