From 66c95b9960b126598268f46826300d731b7eec84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20-=20=E3=82=A2=E3=83=AC=E3=83=83=E3=82=AF=E3=82=B9?= Date: Tue, 23 Apr 2024 15:42:46 +0200 Subject: [PATCH] Error shorthand `WithCausef` (e.WithCause + fmt.Errof) (#65) * Update errors.go.tmpl * Update errors.go.tmpl * gen files * generate with 15.2 * use webrpc built from scratch as CI * use interface * errorStackTrace support --- _examples/golang-basics/example.gen.go | 10 +++++++++- _examples/golang-imports/api.gen.go | 10 +++++++++- errors.go.tmpl | 11 +++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/_examples/golang-basics/example.gen.go b/_examples/golang-basics/example.gen.go index 37e333c..bc4a298 100644 --- a/_examples/golang-basics/example.gen.go +++ b/_examples/golang-basics/example.gen.go @@ -1,6 +1,6 @@ // example v0.0.1 8713979f949e7797d4f7506750b6e18f3b9c7c1b // -- -// Code generated by webrpc-gen@v0.18.1 with ../../../gen-golang generator. DO NOT EDIT. +// Code generated by webrpc-gen@v0.18.2 with ../../../gen-golang generator. DO NOT EDIT. // // webrpc-gen -schema=example.ridl -target=../../../gen-golang -pkg=main -server -client -legacyErrors -fixEmptyArrays -out=./example.gen.go package main @@ -864,6 +864,14 @@ func (e WebRPCError) WithCause(cause error) WebRPCError { return err } +func (e WebRPCError) WithCausef(fmt string, args ...interface{}) WebRPCError { + cause := fmt.Errorf(fmt, args...) + err := e + err.cause = cause + err.Cause = cause.Error() + return err +} + // Deprecated: Use .WithCause() method on WebRPCError. func ErrorWithCause(rpcErr WebRPCError, cause error) WebRPCError { return rpcErr.WithCause(cause) diff --git a/_examples/golang-imports/api.gen.go b/_examples/golang-imports/api.gen.go index 400f599..f16d9bc 100644 --- a/_examples/golang-imports/api.gen.go +++ b/_examples/golang-imports/api.gen.go @@ -1,6 +1,6 @@ // example-api-service v1.0.0 eb9a5d4082a36a8cb84eaa4b3e1091fc4f1f6f3d // -- -// Code generated by webrpc-gen@v0.18.1 with ../../../gen-golang generator. DO NOT EDIT. +// Code generated by webrpc-gen@v0.18.2 with ../../../gen-golang generator. DO NOT EDIT. // // webrpc-gen -schema=./proto/api.ridl -target=../../../gen-golang -out=./api.gen.go -pkg=main -server -client -legacyErrors=true -fmt=false package main @@ -574,6 +574,14 @@ func (e WebRPCError) WithCause(cause error) WebRPCError { return err } +func (e WebRPCError) WithCausef(fmt string, args ...interface{}) WebRPCError { + cause := fmt.Errorf(fmt, args...) + err := e + err.cause = cause + err.Cause = cause.Error() + return err +} + // Deprecated: Use .WithCause() method on WebRPCError. func ErrorWithCause(rpcErr WebRPCError, cause error) WebRPCError { return rpcErr.WithCause(cause) diff --git a/errors.go.tmpl b/errors.go.tmpl index f31fd6b..4a1478c 100644 --- a/errors.go.tmpl +++ b/errors.go.tmpl @@ -58,6 +58,17 @@ func (e WebRPCError) WithCause(cause error) WebRPCError { return err } +func (e WebRPCError) WithCausef(fmt string, args ...interface{}) WebRPCError { + cause := fmt.Errorf(fmt, args...) + err := e + err.cause = cause + err.Cause = cause.Error() + {{- if $opts.errorStackTrace }} + runtime.Callers(1, err.frame.frames[:]) + {{- end}} + return err +} + // Deprecated: Use .WithCause() method on WebRPCError. func ErrorWithCause(rpcErr WebRPCError, cause error) WebRPCError { return rpcErr.WithCause(cause)