From 09b7aa91868e264156a22149c7f040b3da54da73 Mon Sep 17 00:00:00 2001 From: Fraser Waters Date: Wed, 24 Nov 2021 17:01:55 +0000 Subject: [PATCH] Add String and GoString to Result (#8490) I got fed up of assert errors in tests that looked like: ``` Expected nil, but got: &result.simpleResult{err:(*errors.fundamental)(0xc0002fa5d0)} ``` It was very hard to work out at a glance what had gone wrong and I kept having to hook a debugger just to look at what the error was. With GoString these now print something like: ``` Expected nil, but got: &simpleResult{err: Unexpected diag message: <{%reset%}>resource violates plan: properties changed: -zed, -baz, -foo<{%reset%}> } ``` Which is much more useful. --- sdk/go/common/util/result/result.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/sdk/go/common/util/result/result.go b/sdk/go/common/util/result/result.go index 599839912..447bc6828 100644 --- a/sdk/go/common/util/result/result.go +++ b/sdk/go/common/util/result/result.go @@ -15,6 +15,8 @@ package result import ( + "fmt" + "github.com/hashicorp/go-multierror" "github.com/pkg/errors" ) @@ -51,6 +53,18 @@ type simpleResult struct { func (r *simpleResult) Error() error { return r.err } func (r *simpleResult) IsBail() bool { return r.err == nil } +func (r *simpleResult) String() string { + if r.err == nil { + return "Bail" + } + return fmt.Sprintf("Error: %s", r.err) +} +func (r *simpleResult) GoString() string { + if r.err == nil { + return "&simpleResult{}" + } + return fmt.Sprintf("&simpleResult{err: %#v}", r.err) +} // Bail produces a Result that represents a computation that failed to complete // successfully but is not a bug in Pulumi.