Visitar URL original
Give a better error message if snippet conversion isn't supported (#2… · pulumi/pulumi@bd45130 · GitHub
Skip to content

Commit bd45130

Browse files
authored
Give a better error message if snippet conversion isn't supported (#23320)
As it says on the tin. Detected `NotImplemented` and tell the user to try updating the converter or using pcl.
1 parent 25a9e89 commit bd45130

3 files changed

Lines changed: 71 additions & 3 deletions

File tree

‎pkg/cmd/pulumi/do/do_function_test.go‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2222,6 +2222,65 @@ func TestDoCmdFunctionInvokeWithUnknownInputFormat(t *testing.T) {
22222222
assert.ErrorContains(t, err, "converter not found")
22232223
}
22242224

2225+
// TestDoCmdFunctionInvokeWithConverterMissingConvertSnippet verifies that a converter plugin that exists but does not
2226+
// implement ConvertSnippet produces a targeted error instead of leaking the raw RPC error.
2227+
func TestDoCmdFunctionInvokeWithConverterMissingConvertSnippet(t *testing.T) {
2228+
t.Parallel()
2229+
2230+
mlm := &cmdBackend.MockLoginManager{}
2231+
mws := &pkgWorkspace.MockContext{}
2232+
host := func() (plugin.Host, error) {
2233+
return &plugin.MockHost{LoaderAddrF: func() string { return "loader-address" }}, nil
2234+
}
2235+
loadConverter := func(
2236+
_ *plugin.Context, name string, _ func(sev diag.Severity, msg string),
2237+
) (plugin.Converter, error) {
2238+
assert.Equal(t, "yaml", name)
2239+
return &plugin.MockConverter{}, nil
2240+
}
2241+
loader := func(ctx context.Context, pctx *plugin.Context, wd, source string) (plugin.Provider, error) {
2242+
spec := schema.PackageSpec{
2243+
Name: "azure",
2244+
Functions: map[string]schema.FunctionSpec{
2245+
"azure:index:myFunction": {
2246+
Inputs: &schema.ObjectTypeSpec{
2247+
Properties: map[string]schema.PropertySpec{
2248+
"x": {TypeSpec: schema.TypeSpec{Type: "string"}},
2249+
},
2250+
},
2251+
Outputs: &schema.ObjectTypeSpec{
2252+
Properties: map[string]schema.PropertySpec{
2253+
"y": {TypeSpec: schema.TypeSpec{Type: "string"}},
2254+
},
2255+
},
2256+
},
2257+
},
2258+
}
2259+
return &testProvider{
2260+
spec: spec,
2261+
MockProvider: plugin.MockProvider{
2262+
InvokeF: func(ctx context.Context, req plugin.InvokeRequest) (plugin.InvokeResponse, error) {
2263+
require.Fail(t, "Invoke should not be called when the converter cannot convert snippets")
2264+
return plugin.InvokeResponse{}, nil
2265+
},
2266+
},
2267+
}, nil
2268+
}
2269+
2270+
inputFile := writeHCLFile(t, "inputs.yaml", "x: hello")
2271+
2272+
var stdout bytes.Buffer
2273+
cmd := NewDoCmd(mlm, mws, loader, host, loadConverter)
2274+
cmd.SetOut(&stdout)
2275+
cmd.SetErr(&stdout)
2276+
cmd.SetArgs([]string{"azure:myFunction", "--input", "yaml", "--input-file", inputFile})
2277+
err := cmd.Execute()
2278+
require.Error(t, err)
2279+
assert.ErrorContains(t, err, "yaml input converter does not support snippet conversion; "+
2280+
"use pcl format or try installing a newer yaml converter")
2281+
assert.NotContains(t, err.Error(), "ConvertSnippet not implemented")
2282+
}
2283+
22252284
// TestDoCmdFunctionInvokeWithConverterDiagnostics asserts that diagnostic-level errors from ConvertSnippet are
22262285
// surfaced and that Invoke is never called.
22272286
func TestDoCmdFunctionInvokeWithConverterDiagnostics(t *testing.T) {

‎pkg/cmd/pulumi/do/do_shared.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525
"github.com/gofrs/uuid"
2626
"github.com/hashicorp/hcl/v2"
2727
"github.com/spf13/cobra"
28+
"google.golang.org/grpc/codes"
29+
"google.golang.org/grpc/status"
2830

2931
"github.com/pulumi/pulumi/pkg/v3/backend/backenderr"
3032
"github.com/pulumi/pulumi/pkg/v3/backend/display"
@@ -325,6 +327,11 @@ func evaluateFile(
325327
Token: token,
326328
})
327329
if err != nil {
330+
if status.Code(err) == codes.Unimplemented {
331+
return nil, fmt.Errorf(
332+
"%s %s converter does not support snippet conversion; use pcl format or try installing a newer %s converter",
333+
inputFormat, fileType, inputFormat)
334+
}
328335
return nil, fmt.Errorf("generate PCL from %s file: %w", fileType, err)
329336
}
330337
if resp.Diagnostics.HasErrors() {

‎sdk/go/common/resource/plugin/mock.go‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
"context"
1919

2020
"github.com/pulumi/pulumi/sdk/v3/go/common/env"
21+
"google.golang.org/grpc/codes"
22+
"google.golang.org/grpc/status"
2123

2224
"github.com/pkg/errors"
2325
"github.com/pulumi/pulumi/sdk/v3/go/common/diag"
@@ -356,7 +358,7 @@ func (m *MockConverter) ConvertState(ctx context.Context, req *ConvertStateReque
356358
if m.ConvertStateF != nil {
357359
return m.ConvertStateF(ctx, req)
358360
}
359-
return nil, errors.New("ConvertState not implemented")
361+
return nil, status.Error(codes.Unimplemented, "ConvertState not implemented")
360362
}
361363

362364
func (m *MockConverter) ConvertProgram(
@@ -365,7 +367,7 @@ func (m *MockConverter) ConvertProgram(
365367
if m.ConvertProgramF != nil {
366368
return m.ConvertProgramF(ctx, req)
367369
}
368-
return nil, errors.New("ConvertProgram not implemented")
370+
return nil, status.Error(codes.Unimplemented, "ConvertProgram not implemented")
369371
}
370372

371373
func (m *MockConverter) ConvertSnippet(
@@ -374,5 +376,5 @@ func (m *MockConverter) ConvertSnippet(
374376
if m.ConvertSnippetF != nil {
375377
return m.ConvertSnippetF(ctx, req)
376378
}
377-
return nil, errors.New("ConvertSnippet not implemented")
379+
return nil, status.Error(codes.Unimplemented, "ConvertSnippet not implemented")
378380
}

0 commit comments

Comments
 (0)