Skip to content

Commit c9bc568

Browse files
committed
Adjust "ociImportBuild" error messages to be more debugging-friendly
1 parent 8660e60 commit c9bc568

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

cmd/bashbrew/oci-builder.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,17 @@ func ociImportBuild(tags []string, commit, dir, file string) error {
9696
return err
9797
}
9898

99+
// added to any string errors we generate to add more helpful debugging context for things like the wrong filename, directory, commit ID, etc.
100+
errFileStr := func(file string) string { return fmt.Sprintf("%q (from directory %q in commit %q)", file, dir, commit) }
101+
99102
// https://github.com/opencontainers/image-spec/blob/v1.0.2/image-layout.md#oci-layout-file
100103
var layout imagespec.ImageLayout
101104
if err := readJSONFile(fs, "oci-layout", &layout); err != nil {
102-
return fmt.Errorf("failed reading oci-layout: %w", err)
105+
return fmt.Errorf("failed reading %s: %w", errFileStr("oci-layout"), err)
103106
}
104107
if layout.Version != "1.0.0" {
105108
// "The imageLayoutVersion value will align with the OCI Image Specification version at the time changes to the layout are made, and will pin a given version until changes to the image layout are required."
106-
return fmt.Errorf("unsupported OCI image layout version %q", layout.Version)
109+
return fmt.Errorf("unsupported OCI image layout version %q in %s", layout.Version, errFileStr("oci-layout"))
107110
}
108111

109112
var manifestDescriptor imagespec.Descriptor
@@ -112,29 +115,29 @@ func ociImportBuild(tags []string, commit, dir, file string) error {
112115
// https://github.com/opencontainers/image-spec/blob/v1.0.2/image-layout.md#indexjson-file
113116
var index imagespec.Index
114117
if err := readJSONFile(fs, file, &index); err != nil {
115-
return fmt.Errorf("failed reading %q: %w", file, err)
118+
return fmt.Errorf("failed reading %s: %w", errFileStr(file), err)
116119
}
117120
if index.SchemaVersion != 2 {
118-
return fmt.Errorf("unsupported schemaVersion %d in %q", index.SchemaVersion, file)
121+
return fmt.Errorf("unsupported schemaVersion %d in %s", index.SchemaVersion, errFileStr(file))
119122
}
120123
if len(index.Manifests) != 1 {
121-
return fmt.Errorf("expected only one manifests entry (not %d) in %q", len(index.Manifests), file)
124+
return fmt.Errorf("expected only one manifests entry (not %d) in %s", len(index.Manifests), errFileStr(file))
122125
}
123126
manifestDescriptor = index.Manifests[0]
124127
} else {
125128
if err := readJSONFile(fs, file, &manifestDescriptor); err != nil {
126-
return fmt.Errorf("failed reading %q: %w", file, err)
129+
return fmt.Errorf("failed reading %s: %w", errFileStr(file), err)
127130
}
128131
}
129132

130133
if manifestDescriptor.MediaType != imagespec.MediaTypeImageManifest {
131-
return fmt.Errorf("unsupported mediaType %q in descriptor", manifestDescriptor.MediaType)
134+
return fmt.Errorf("unsupported mediaType %q in descriptor in %s", manifestDescriptor.MediaType, errFileStr(file))
132135
}
133136
if err := manifestDescriptor.Digest.Validate(); err != nil {
134-
return fmt.Errorf("invalid digest %q: %w", manifestDescriptor.Digest, err)
137+
return fmt.Errorf("invalid digest %q in %s: %w", manifestDescriptor.Digest, errFileStr(file), err)
135138
}
136139
if manifestDescriptor.Size < 0 {
137-
return fmt.Errorf("invalid size %d in descriptor", manifestDescriptor.Size)
140+
return fmt.Errorf("invalid size %d in descriptor in %s", manifestDescriptor.Size, errFileStr(file))
138141
}
139142
// TODO validate Platform is either unset or matches expected value
140143

@@ -152,17 +155,17 @@ func ociImportBuild(tags []string, commit, dir, file string) error {
152155
cs := client.ContentStore()
153156

154157
if err := importOCIBlob(ctx, cs, fs, manifestDescriptor); err != nil {
155-
return fmt.Errorf("failed to import manifest blob (%q): %w", manifestDescriptor.Digest, err)
158+
return fmt.Errorf("failed to import manifest blob %s: %w", errFileStr(string(manifestDescriptor.Digest)), err)
156159
}
157160
var manifest imagespec.Manifest
158161
if err := readContentJSON(ctx, cs, manifestDescriptor, &manifest); err != nil {
159-
return fmt.Errorf("failed to parse manifest (%q): %w", manifestDescriptor.Digest, err)
162+
return fmt.Errorf("failed to parse manifest %s: %w", errFileStr(string(manifestDescriptor.Digest)), err)
160163
}
161164

162165
otherBlobs := append([]imagespec.Descriptor{manifest.Config}, manifest.Layers...)
163166
for _, blob := range otherBlobs {
164167
if err := importOCIBlob(ctx, cs, fs, blob); err != nil {
165-
return fmt.Errorf("failed to import blob (%q): %w", blob.Digest, err)
168+
return fmt.Errorf("failed to import blob %s: %w", errFileStr(string(blob.Digest)), err)
166169
}
167170
}
168171

@@ -176,11 +179,11 @@ func ociImportBuild(tags []string, commit, dir, file string) error {
176179
img2, err := is.Update(ctx, img, "target") // "target" here is to specify that we want to update the descriptor that "Name" points to (if this image name already exists)
177180
if err != nil {
178181
if !errdefs.IsNotFound(err) {
179-
return fmt.Errorf("failed to update image %q: %w", img.Name, err)
182+
return fmt.Errorf("failed to update image %q in containerd: %w", img.Name, err)
180183
}
181184
img2, err = is.Create(ctx, img)
182185
if err != nil {
183-
return fmt.Errorf("failed to create image %q: %w", img.Name, err)
186+
return fmt.Errorf("failed to create image %q in containerd: %w", img.Name, err)
184187
}
185188
}
186189
img = img2 // unnecessary? :)

0 commit comments

Comments
 (0)