You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by cc...@apache.org on 2019/06/21 21:25:38 UTC

[mynewt-artifact] 14/23: Add "errors" package

This is an automated email from the ASF dual-hosted git repository.

ccollins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-artifact.git

commit 697ec59beded3decd59cd08f3f3a46b7573f418c
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Thu Jun 20 14:05:35 2019 -0700

    Add "errors" package
    
    This is a thin wrapper over the `pkg/errors` package.  It decorates the
    base package with the following functionality:
    
    1. Wrap and Wrapf produce an error with exactly one stack trace.  If
    the wrapped error already contains a stack trace, these functions just
    append text to the message.
    
    2. WithStack produces an error with exactly one stack trace.  If the
    wrapped error already contains a stack trace, it is returned unmodified.
    
    All of artifact's public APIs return errors produced with this package.
    This allows us to remove the dependency on newt (`NewtError`) but still
    retain error stack traces.
---
 errors/errors.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)

diff --git a/errors/errors.go b/errors/errors.go
new file mode 100644
index 0000000..8154fc5
--- /dev/null
+++ b/errors/errors.go
@@ -0,0 +1,63 @@
+// This is a thin wrapper over the `pkg/errors` package.  It decorates the
+// base package with the following functionality:
+//
+// 1. Wrap and Wrapf produce an error with exactly one stack trace.  If the
+// wrapped error already contains a stack trace, these functions just append
+// text to the message.
+//
+// 2. WithStack produces an error with exactly one stack trace.  If the
+// wrapped error already contains a stack trace, this function returns it
+// unmodified.
+
+package errors
+
+import (
+	"fmt"
+
+	pkgerrors "github.com/pkg/errors"
+)
+
+type stackTracer interface {
+	StackTrace() pkgerrors.StackTrace
+}
+
+// Cause retrieves the underlying cause of an error
+func Cause(err error) error {
+	return pkgerrors.Cause(err)
+}
+
+// Errorf formats an error
+func Errorf(format string, args ...interface{}) error {
+	return pkgerrors.Errorf(format, args...)
+}
+
+// New creates a new error
+func New(message string) error {
+	return pkgerrors.New(message)
+}
+
+func Wrap(err error, message string) error {
+	if _, ok := err.(stackTracer); !ok {
+		return pkgerrors.Wrap(err, message)
+	} else {
+		msg := err.Error() + ": " + message
+		return pkgerrors.WithMessage(err, msg)
+	}
+}
+
+func Wrapf(err error, format string, args ...interface{}) error {
+	return Wrap(err, fmt.Sprintf(format, args...))
+}
+
+func WithStack(err error) error {
+	if _, ok := err.(stackTracer); !ok {
+		return pkgerrors.WithStack(err)
+	} else {
+		return err
+	}
+}
+
+func HasStackTrace(err error) bool {
+	_, ok := err.(stackTracer)
+	return ok
+}