Repository: goadesign/goa
Pull request #3153 (2022-09-30 19:07:51)

Fix goa error namer


Patch:
diff --git a/grpc/codegen/server.go b/grpc/codegen/server.go
index 7866fa23..50ebda98 100644
--- a/grpc/codegen/server.go
+++ b/grpc/codegen/server.go
@@ -220,11 +220,17 @@ type GoaErrorNamer interface {
 	GoaErrorName() string
 }
 
-type adaptErrorNamer ErrorName
+type adaptErrorNamer struct {
+	ErrorNamer
+}
 
 func (err adaptErrorNamer) GoaErrorName() string {
 	return err.ErrorName()
 }
+
+func (err adaptErrorNamer) Error() string {
+	return err.ErrorNamer.(error).Error()
+}
 `
 
 // input: ServiceData
@@ -281,8 +287,8 @@ func (s *{{ .ServerStruct }}) {{ .Method.VarName }}(
 	if err != nil {
 	{{- if .Errors }}
 		var deprecatedErrorNamer ErrorNamer
-		if errors.As(v, &deprecatedErrorNamer) {
-			v = adaptErrorNamer(deprecatedErrorNamer)
+		if errors.As(err, &deprecatedErrorNamer) {
+			err = adaptErrorNamer{deprecatedErrorNamer}
 		}
 		var en GoaErrorNamer
 		if errors.As(err, &en) {
diff --git a/http/codegen/server.go b/http/codegen/server.go
index be294eb6..d8040264 100644
--- a/http/codegen/server.go
+++ b/http/codegen/server.go
@@ -291,11 +291,17 @@ type GoaErrorNamer interface {
 	GoaErrorName() string
 }
 
-type adaptErrorNamer ErrorName
+type adaptErrorNamer struct {
+	ErrorNamer
+}
 
 func (err adaptErrorNamer) GoaErrorName() string {
 	return err.ErrorName()
 }
+
+func (err adaptErrorNamer) Error() string {
+	return err.ErrorNamer.(error).Error()
+}
 `
 
 // input: ServiceData
@@ -1219,7 +1225,7 @@ func {{ .ErrorEncoder }}(encoder func(context.Context, http.ResponseWriter) goah
 	return func(ctx context.Context, w http.ResponseWriter, v error) error {
 		var deprecatedErrorNamer ErrorNamer
 		if errors.As(v, &deprecatedErrorNamer) {
-			v = adaptErrorNamer(deprecatedErrorNamer)
+			v = adaptErrorNamer{deprecatedErrorNamer}
 		}
 		var en GoaErrorNamer
 		if !errors.As(v, &en) {


Test patch:
diff --git a/grpc/codegen/testdata/server_interface_code.go b/grpc/codegen/testdata/server_interface_code.go
index e5cb7974..1c5e18f1 100644
--- a/grpc/codegen/testdata/server_interface_code.go
+++ b/grpc/codegen/testdata/server_interface_code.go
@@ -59,8 +59,8 @@ func (s *Server) MethodUnaryRPCWithErrors(ctx context.Context, message *service_
 	resp, err := s.MethodUnaryRPCWithErrorsH.Handle(ctx, message)
 	if err != nil {
 		var deprecatedErrorNamer ErrorNamer
-		if errors.As(v, &deprecatedErrorNamer) {
-			v = adaptErrorNamer(deprecatedErrorNamer)
+		if errors.As(err, &deprecatedErrorNamer) {
+			err = adaptErrorNamer{deprecatedErrorNamer}
 		}
 		var en GoaErrorNamer
 		if errors.As(err, &en) {
@@ -97,8 +97,8 @@ func (s *Server) MethodUnaryRPCWithOverridingErrors(ctx context.Context, message
 	resp, err := s.MethodUnaryRPCWithOverridingErrorsH.Handle(ctx, message)
 	if err != nil {
 		var deprecatedErrorNamer ErrorNamer
-		if errors.As(v, &deprecatedErrorNamer) {
-			v = adaptErrorNamer(deprecatedErrorNamer)
+		if errors.As(err, &deprecatedErrorNamer) {
+			err = adaptErrorNamer{deprecatedErrorNamer}
 		}
 		var en GoaErrorNamer
 		if errors.As(err, &en) {
@@ -240,8 +240,8 @@ func (s *Server) MethodBidirectionalStreamingRPCWithErrors(stream service_bidire
 	_, err := s.MethodBidirectionalStreamingRPCWithErrorsH.Decode(ctx, nil)
 	if err != nil {
 		var deprecatedErrorNamer ErrorNamer
-		if errors.As(v, &deprecatedErrorNamer) {
-			v = adaptErrorNamer(deprecatedErrorNamer)
+		if errors.As(err, &deprecatedErrorNamer) {
+			err = adaptErrorNamer{deprecatedErrorNamer}
 		}
 		var en GoaErrorNamer
 		if errors.As(err, &en) {
@@ -262,8 +262,8 @@ func (s *Server) MethodBidirectionalStreamingRPCWithErrors(stream service_bidire
 	err = s.MethodBidirectionalStreamingRPCWithErrorsH.Handle(ctx, ep)
 	if err != nil {
 		var deprecatedErrorNamer ErrorNamer
-		if errors.As(v, &deprecatedErrorNamer) {
-			v = adaptErrorNamer(deprecatedErrorNamer)
+		if errors.As(err, &deprecatedErrorNamer) {
+			err = adaptErrorNamer{deprecatedErrorNamer}
 		}
 		var en GoaErrorNamer
 		if errors.As(err, &en) {
diff --git a/http/codegen/testdata/error_encoder_code.go b/http/codegen/testdata/error_encoder_code.go
index 861e5d0e..d9189555 100644
--- a/http/codegen/testdata/error_encoder_code.go
+++ b/http/codegen/testdata/error_encoder_code.go
@@ -8,7 +8,7 @@ func EncodeMethodPrimitiveErrorResponseError(encoder func(context.Context, http.
 	return func(ctx context.Context, w http.ResponseWriter, v error) error {
 		var deprecatedErrorNamer ErrorNamer
 		if errors.As(v, &deprecatedErrorNamer) {
-			v = adaptErrorNamer(deprecatedErrorNamer)
+			v = adaptErrorNamer{deprecatedErrorNamer}
 		}
 		var en GoaErrorNamer
 		if !errors.As(v, &en) {
@@ -46,7 +46,7 @@ func EncodeMethodPrimitiveErrorInResponseHeaderError(encoder func(context.Contex
 	return func(ctx context.Context, w http.ResponseWriter, v error) error {
 		var deprecatedErrorNamer ErrorNamer
 		if errors.As(v, &deprecatedErrorNamer) {
-			v = adaptErrorNamer(deprecatedErrorNamer)
+			v = adaptErrorNamer{deprecatedErrorNamer}
 		}
 		var en GoaErrorNamer
 		if !errors.As(v, &en) {
@@ -90,7 +90,7 @@ func EncodeMethodAPIPrimitiveErrorResponseError(encoder func(context.Context, ht
 	return func(ctx context.Context, w http.ResponseWriter, v error) error {
 		var deprecatedErrorNamer ErrorNamer
 		if errors.As(v, &deprecatedErrorNamer) {
-			v = adaptErrorNamer(deprecatedErrorNamer)
+			v = adaptErrorNamer{deprecatedErrorNamer}
 		}
 		var en GoaErrorNamer
 		if !errors.As(v, &en) {
@@ -132,7 +132,7 @@ func EncodeMethodDefaultErrorResponseError(encoder func(context.Context, http.Re
 	return func(ctx context.Context, w http.ResponseWriter, v error) error {
 		var deprecatedErrorNamer ErrorNamer
 		if errors.As(v, &deprecatedErrorNamer) {
-			v = adaptErrorNamer(deprecatedErrorNamer)
+			v = adaptErrorNamer{deprecatedErrorNamer}
 		}
 		var en GoaErrorNamer
 		if !errors.As(v, &en) {
@@ -166,7 +166,7 @@ func EncodeMethodDefaultErrorResponseError(encoder func(context.Context, http.Re
 	return func(ctx context.Context, w http.ResponseWriter, v error) error {
 		var deprecatedErrorNamer ErrorNamer
 		if errors.As(v, &deprecatedErrorNamer) {
-			v = adaptErrorNamer(deprecatedErrorNamer)
+			v = adaptErrorNamer{deprecatedErrorNamer}
 		}
 		var en GoaErrorNamer
 		if !errors.As(v, &en) {
@@ -201,7 +201,7 @@ func EncodeMethodServiceErrorResponseError(encoder func(context.Context, http.Re
 	return func(ctx context.Context, w http.ResponseWriter, v error) error {
 		var deprecatedErrorNamer ErrorNamer
 		if errors.As(v, &deprecatedErrorNamer) {
-			v = adaptErrorNamer(deprecatedErrorNamer)
+			v = adaptErrorNamer{deprecatedErrorNamer}
 		}
 		var en GoaErrorNamer
 		if !errors.As(v, &en) {
@@ -248,7 +248,7 @@ func EncodeMethodServiceErrorResponseError(encoder func(context.Context, http.Re
 	return func(ctx context.Context, w http.ResponseWriter, v error) error {
 		var deprecatedErrorNamer ErrorNamer
 		if errors.As(v, &deprecatedErrorNamer) {
-			v = adaptErrorNamer(deprecatedErrorNamer)
+			v = adaptErrorNamer{deprecatedErrorNamer}
 		}
 		var en GoaErrorNamer
 		if !errors.As(v, &en) {
@@ -296,7 +296,7 @@ func EncodeMethodServiceErrorResponseError(encoder func(context.Context, http.Re
 	return func(ctx context.Context, w http.ResponseWriter, v error) error {
 		var deprecatedErrorNamer ErrorNamer
 		if errors.As(v, &deprecatedErrorNamer) {
-			v = adaptErrorNamer(deprecatedErrorNamer)
+			v = adaptErrorNamer{deprecatedErrorNamer}
 		}
 		var en GoaErrorNamer
 		if !errors.As(v, &en) {
@@ -326,7 +326,7 @@ func EncodeMethodServiceErrorResponseError(encoder func(context.Context, http.Re
 	return func(ctx context.Context, w http.ResponseWriter, v error) error {
 		var deprecatedErrorNamer ErrorNamer
 		if errors.As(v, &deprecatedErrorNamer) {
-			v = adaptErrorNamer(deprecatedErrorNamer)
+			v = adaptErrorNamer{deprecatedErrorNamer}
 		}
 		var en GoaErrorNamer
 		if !errors.As(v, &en) {
@@ -358,7 +358,7 @@ func EncodeMethodEmptyErrorResponseBodyError(encoder func(context.Context, http.
 	return func(ctx context.Context, w http.ResponseWriter, v error) error {
 		var deprecatedErrorNamer ErrorNamer
 		if errors.As(v, &deprecatedErrorNamer) {
-			v = adaptErrorNamer(deprecatedErrorNamer)
+			v = adaptErrorNamer{deprecatedErrorNamer}
 		}
 		var en GoaErrorNamer
 		if !errors.As(v, &en) {
@@ -415,7 +415,7 @@ func EncodeMethodEmptyCustomErrorResponseBodyError(encoder func(context.Context,
 	return func(ctx context.Context, w http.ResponseWriter, v error) error {
 		var deprecatedErrorNamer ErrorNamer
 		if errors.As(v, &deprecatedErrorNamer) {
-			v = adaptErrorNamer(deprecatedErrorNamer)
+			v = adaptErrorNamer{deprecatedErrorNamer}
 		}
 		var en GoaErrorNamer
 		if !errors.As(v, &en) {