From 1b10a712eb142b0d4ab82f94ef5d171b5db200fd Mon Sep 17 00:00:00 2001 From: Vojtech Vitek Date: Wed, 27 Sep 2023 15:34:26 +0200 Subject: [PATCH 1/2] Implement err.WithCause(), a shorter version of proto.ErrorWithCause() --- _examples/golang-basics/example.gen.go | 7 +++++++ _examples/golang-imports/api.gen.go | 7 +++++++ errors.go.tmpl | 10 ++++++++++ 3 files changed, 24 insertions(+) diff --git a/_examples/golang-basics/example.gen.go b/_examples/golang-basics/example.gen.go index f739865..75ab3e2 100644 --- a/_examples/golang-basics/example.gen.go +++ b/_examples/golang-basics/example.gen.go @@ -715,6 +715,13 @@ func (e WebRPCError) Unwrap() error { return e.cause } +func (e WebRPCError) WithCause(cause error) error { + err := e + err.cause = cause + err.Cause = cause.Error() + return err +} + func ErrorWithCause(rpcErr WebRPCError, cause error) WebRPCError { err := rpcErr err.cause = cause diff --git a/_examples/golang-imports/api.gen.go b/_examples/golang-imports/api.gen.go index 74258d6..3e0c749 100644 --- a/_examples/golang-imports/api.gen.go +++ b/_examples/golang-imports/api.gen.go @@ -482,6 +482,13 @@ func (e WebRPCError) Unwrap() error { return e.cause } +func (e WebRPCError) WithCause(cause error) error { + err := e + err.cause = cause + err.Cause = cause.Error() + return err +} + func ErrorWithCause(rpcErr WebRPCError, cause error) WebRPCError { err := rpcErr err.cause = cause diff --git a/errors.go.tmpl b/errors.go.tmpl index 26a47f5..2ec6464 100644 --- a/errors.go.tmpl +++ b/errors.go.tmpl @@ -45,6 +45,16 @@ func (e WebRPCError) Unwrap() error { return e.cause } +func (e WebRPCError) WithCause(cause error) error { + err := e + err.cause = cause + err.Cause = cause.Error() + {{- if $opts.errorStackTrace }} + runtime.Callers(1, err.frame.frames[:]) + {{- end}} + return err +} + func ErrorWithCause(rpcErr WebRPCError, cause error) WebRPCError { err := rpcErr err.cause = cause From bc0cc7ffdcc89fbc18c399444de84db49c67970f Mon Sep 17 00:00:00 2001 From: Vojtech Vitek Date: Wed, 27 Sep 2023 18:17:36 +0200 Subject: [PATCH 2/2] Deprecate proto.ErrorWithCause() --- _examples/golang-basics/example.gen.go | 6 ++---- _examples/golang-imports/api.gen.go | 6 ++---- errors.go.tmpl | 9 ++------- 3 files changed, 6 insertions(+), 15 deletions(-) diff --git a/_examples/golang-basics/example.gen.go b/_examples/golang-basics/example.gen.go index 75ab3e2..7263136 100644 --- a/_examples/golang-basics/example.gen.go +++ b/_examples/golang-basics/example.gen.go @@ -722,11 +722,9 @@ func (e WebRPCError) WithCause(cause error) error { return err } +// Deprecated: Use .WithCause() method on WebRPCError. func ErrorWithCause(rpcErr WebRPCError, cause error) WebRPCError { - err := rpcErr - err.cause = cause - err.Cause = cause.Error() - return err + return rpcErr.WithCause(cause) } // Webrpc errors diff --git a/_examples/golang-imports/api.gen.go b/_examples/golang-imports/api.gen.go index 3e0c749..be4b50b 100644 --- a/_examples/golang-imports/api.gen.go +++ b/_examples/golang-imports/api.gen.go @@ -489,11 +489,9 @@ func (e WebRPCError) WithCause(cause error) error { return err } +// Deprecated: Use .WithCause() method on WebRPCError. func ErrorWithCause(rpcErr WebRPCError, cause error) WebRPCError { - err := rpcErr - err.cause = cause - err.Cause = cause.Error() - return err + return rpcErr.WithCause(cause) } // Webrpc errors diff --git a/errors.go.tmpl b/errors.go.tmpl index 2ec6464..ea94bc5 100644 --- a/errors.go.tmpl +++ b/errors.go.tmpl @@ -55,14 +55,9 @@ func (e WebRPCError) WithCause(cause error) error { return err } +// Deprecated: Use .WithCause() method on WebRPCError. func ErrorWithCause(rpcErr WebRPCError, cause error) WebRPCError { - err := rpcErr - err.cause = cause - err.Cause = cause.Error() - {{- if $opts.errorStackTrace }} - runtime.Callers(1, err.frame.frames[:]) - {{- end}} - return err + return rpcErr.WithCause(cause) } {{- if $opts.errorStackTrace -}}