Skip to content

Commit 986fb40

Browse files
authored
Provide a diagnostic error message when a filesystem scheme is not found. (#21816)
Example old message: panic: file system scheme "default" not registered Example new message: panic: file system scheme "default" not registered for "/tmp/fake.txt": Consider adding the following import to your program to register an implementation for "default": import _ "github.com/apache/beam/sdks/v2/go/pkg/beam/io/filesystem/local"
1 parent 4d04f50 commit 986fb40

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

sdks/go/pkg/beam/io/filesystem/filesystem.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ import (
3434

3535
var registry = make(map[string]func(context.Context) Interface)
3636

37+
// wellKnownSchemeImportPaths is used for deliverng useful error messages when a
38+
// scheme is not found.
39+
var wellKnownSchemeImportPaths = map[string]string{
40+
"memfs": "github.com/apache/beam/sdks/v2/go/pkg/beam/io/filesystem/memfs",
41+
"default": "github.com/apache/beam/sdks/v2/go/pkg/beam/io/filesystem/local",
42+
"gs": "github.com/apache/beam/sdks/v2/go/pkg/beam/io/filesystem/gcs",
43+
}
44+
3745
// Register registers a file system backend under the given scheme. For
3846
// example, "hdfs" would be registered a HFDS file system and HDFS paths used
3947
// transparently.
@@ -49,11 +57,19 @@ func New(ctx context.Context, path string) (Interface, error) {
4957
scheme := getScheme(path)
5058
mkfs, ok := registry[scheme]
5159
if !ok {
52-
return nil, errors.Errorf("file system scheme %v not registered for %v", scheme, path)
60+
return nil, errorForMissingScheme(scheme, path)
5361
}
5462
return mkfs(ctx), nil
5563
}
5664

65+
func errorForMissingScheme(scheme, path string) error {
66+
messageSuffix := ""
67+
if suggestedImportPath, ok := wellKnownSchemeImportPaths[scheme]; ok {
68+
messageSuffix = fmt.Sprintf(": Consider adding the following import to your program to register an implementation for %q:\n import _ %q", scheme, suggestedImportPath)
69+
}
70+
return errors.Errorf("file system scheme %q not registered for %q%s", scheme, path, messageSuffix)
71+
}
72+
5773
// Interface is a filesystem abstraction that allows beam io sources and sinks
5874
// to use various underlying storage systems transparently.
5975
type Interface interface {
@@ -105,6 +121,6 @@ func ValidateScheme(path string) {
105121
}
106122
scheme := getScheme(path)
107123
if _, ok := registry[scheme]; !ok {
108-
panic(fmt.Sprintf("filesystem scheme %v not registered", scheme))
124+
panic(errorForMissingScheme(scheme, path))
109125
}
110126
}

0 commit comments

Comments
 (0)