@@ -12,6 +12,7 @@ import (
1212 "github.com/zclconf/go-cty/cty"
1313
1414 plugin "github.com/hashicorp/go-plugin"
15+ "github.com/zclconf/go-cty/cty/function"
1516 ctyjson "github.com/zclconf/go-cty/cty/json"
1617 "github.com/zclconf/go-cty/cty/msgpack"
1718 "google.golang.org/grpc"
@@ -740,7 +741,7 @@ func (p *GRPCProvider) CallFunction(r providers.CallFunctionRequest) (resp provi
740741
741742 schema := p .GetProviderSchema ()
742743 if schema .Diagnostics .HasErrors () {
743- resp .Diagnostics = schema .Diagnostics
744+ resp .Err = schema .Diagnostics . Err ()
744745 return resp
745746 }
746747
@@ -754,15 +755,15 @@ func (p *GRPCProvider) CallFunction(r providers.CallFunctionRequest) (resp provi
754755 // Should only get here if the caller has a bug, because we should
755756 // have detected earlier any attempt to call a function that the
756757 // provider didn't declare.
757- resp .Diagnostics = resp . Diagnostics . Append ( fmt .Errorf ("provider has no function named %q" , r .FunctionName ) )
758+ resp .Err = fmt .Errorf ("provider has no function named %q" , r .FunctionName )
758759 return resp
759760 }
760761 if len (r .Arguments ) < len (funcDecl .Parameters ) {
761- resp .Diagnostics = resp . Diagnostics . Append ( fmt .Errorf ("not enough arguments for function %q" , r .FunctionName ) )
762+ resp .Err = fmt .Errorf ("not enough arguments for function %q" , r .FunctionName )
762763 return resp
763764 }
764765 if funcDecl .VariadicParameter == nil && len (r .Arguments ) > len (funcDecl .Parameters ) {
765- resp .Diagnostics = resp . Diagnostics . Append ( fmt .Errorf ("too many arguments for function %q" , r .FunctionName ) )
766+ resp .Err = fmt .Errorf ("too many arguments for function %q" , r .FunctionName )
766767 return resp
767768 }
768769 args := make ([]* proto6.DynamicValue , len (r .Arguments ))
@@ -776,7 +777,7 @@ func (p *GRPCProvider) CallFunction(r providers.CallFunctionRequest) (resp provi
776777
777778 argValRaw , err := msgpack .Marshal (argVal , paramDecl .Type )
778779 if err != nil {
779- resp .Diagnostics = resp . Diagnostics . Append ( err )
780+ resp .Err = err
780781 return resp
781782 }
782783 args [i ] = & proto6.DynamicValue {
@@ -789,17 +790,28 @@ func (p *GRPCProvider) CallFunction(r providers.CallFunctionRequest) (resp provi
789790 Arguments : args ,
790791 })
791792 if err != nil {
792- resp .Diagnostics = resp .Diagnostics .Append (grpcErr (err ))
793+ // functions can only support simple errors, but use our grpcError
794+ // diagnostic function to format common problems is a more
795+ // user-friendly manner.
796+ resp .Err = grpcErr (err ).Err ()
793797 return resp
794798 }
795- resp .Diagnostics = resp .Diagnostics .Append (convert .ProtoToDiagnostics (protoResp .Diagnostics ))
796- if resp .Diagnostics .HasErrors () {
799+
800+ if protoResp .Error != nil {
801+ resp .Err = errors .New (protoResp .Error .Text )
802+
803+ // If this is a problem with a specific argument, we can wrap the error
804+ // in a function.ArgError
805+ if protoResp .Error .FunctionArgument != nil {
806+ resp .Err = function .NewArgError (int (* protoResp .Error .FunctionArgument ), resp .Err )
807+ }
808+
797809 return resp
798810 }
799811
800812 resultVal , err := decodeDynamicValue (protoResp .Result , funcDecl .ReturnType )
801813 if err != nil {
802- resp .Diagnostics = resp . Diagnostics . Append ( err )
814+ resp .Err = err
803815 return resp
804816 }
805817
0 commit comments