Skip to content

Commit 250e52f

Browse files
authored
refactor(internal/librarian): improve error logging for generation, formatting, and cleanup (#4609)
The generation, formatting, and cleanup functions are refactored to wrap errors with the operation name, library name, and language. This provides better diagnostic information when generation steps fail.
1 parent 7d7fef4 commit 250e52f

1 file changed

Lines changed: 45 additions & 37 deletions

File tree

internal/librarian/generate.go

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -111,40 +111,32 @@ func runGenerate(ctx context.Context, cfg *config.Config, all bool, libraryName
111111
// cleanLibraries iterates over all the given libraries sequentially,
112112
// delegating to language-specific code to clean each library.
113113
func cleanLibraries(language string, libraries []*config.Library) error {
114+
var err error
114115
for _, library := range libraries {
115116
switch language {
116117
case config.LanguageDart:
117-
if err := checkAndClean(library.Output, library.Keep); err != nil {
118-
return err
119-
}
118+
err = checkAndClean(library.Output, library.Keep)
120119
case config.LanguageFake:
121-
if err := fakeClean(library); err != nil {
122-
return err
123-
}
120+
err = fakeClean(library)
124121
case config.LanguageGo:
125-
if err := golang.Clean(library); err != nil {
126-
return err
127-
}
122+
err = golang.Clean(library)
128123
case config.LanguageJava:
129-
if err := java.Clean(library); err != nil {
130-
return err
131-
}
124+
err = java.Clean(library)
132125
case config.LanguageNodejs:
133-
if err := checkAndClean(library.Output, library.Keep); err != nil {
134-
return err
135-
}
126+
err = checkAndClean(library.Output, library.Keep)
136127
case config.LanguagePython:
137-
if err := python.Clean(library); err != nil {
138-
return err
139-
}
128+
err = python.Clean(library)
140129
case config.LanguageRust:
141-
keep, err := rust.Keep(library)
142-
if err != nil {
143-
return fmt.Errorf("library %q: %w", library.Name, err)
144-
}
145-
if err := checkAndClean(library.Output, keep); err != nil {
146-
return err
130+
keep, keepErr := rust.Keep(library)
131+
if keepErr != nil {
132+
return fmt.Errorf("generating keep list: %w", keepErr)
147133
}
134+
err = checkAndClean(library.Output, keep)
135+
default:
136+
err = fmt.Errorf("language %q does not support cleaning", language)
137+
}
138+
if err != nil {
139+
return fmt.Errorf("clean library %q (%s): %w", library.Name, language, err)
148140
}
149141
}
150142
return nil
@@ -160,19 +152,22 @@ func generateLibraries(ctx context.Context, cfg *config.Config, libraries []*con
160152
for _, library := range libraries {
161153
g.Go(func() error {
162154
if err := dart.Generate(gctx, library, src); err != nil {
163-
return err
155+
return fmt.Errorf("generate library %q (%s): %w", library.Name, cfg.Language, err)
156+
}
157+
if err := dart.Format(gctx, library); err != nil {
158+
return fmt.Errorf("format library %q (%s): %w", library.Name, cfg.Language, err)
164159
}
165-
return dart.Format(gctx, library)
160+
return nil
166161
})
167162
}
168163
return g.Wait()
169164
case config.LanguageFake:
170165
for _, library := range libraries {
171166
if err := fakeGenerate(library); err != nil {
172-
return err
167+
return fmt.Errorf("generate library %q (%s): %w", library.Name, cfg.Language, err)
173168
}
174169
if err := fakeFormat(library); err != nil {
175-
return err
170+
return fmt.Errorf("format library %q (%s): %w", library.Name, cfg.Language, err)
176171
}
177172
}
178173
case config.LanguageGo:
@@ -181,31 +176,39 @@ func generateLibraries(ctx context.Context, cfg *config.Config, libraries []*con
181176
// shared cloud.google.com/go directory tree under each library's
182177
// output, and concurrent MoveAndMerge calls would race.
183178
if err := golang.Generate(ctx, library, src.Googleapis); err != nil {
184-
return err
179+
return fmt.Errorf("generate library %q (%s): %w", library.Name, cfg.Language, err)
185180
}
186181
}
187182
g, gctx := errgroup.WithContext(ctx)
188183
for _, library := range libraries {
189-
g.Go(func() error { return golang.Format(gctx, library) })
184+
g.Go(func() error {
185+
if err := golang.Format(gctx, library); err != nil {
186+
return fmt.Errorf("format library %q (%s): %w", library.Name, cfg.Language, err)
187+
}
188+
return nil
189+
})
190190
}
191191
return g.Wait()
192192
case config.LanguageJava:
193193
for _, library := range libraries {
194194
if err := java.Generate(ctx, cfg, library, src.Googleapis); err != nil {
195-
return err
195+
return fmt.Errorf("generate library %q (%s): %w", library.Name, cfg.Language, err)
196196
}
197197
if err := java.Format(ctx, library); err != nil {
198-
return err
198+
return fmt.Errorf("format library %q (%s): %w", library.Name, cfg.Language, err)
199199
}
200200
}
201201
case config.LanguageNodejs:
202202
g, gctx := errgroup.WithContext(ctx)
203203
for _, library := range libraries {
204204
g.Go(func() error {
205205
if err := nodejs.Generate(gctx, library, src.Googleapis); err != nil {
206-
return err
206+
return fmt.Errorf("generate library %q (%s): %w", library.Name, cfg.Language, err)
207207
}
208-
return nodejs.Format(gctx, library)
208+
if err := nodejs.Format(gctx, library); err != nil {
209+
return fmt.Errorf("format library %q (%s): %w", library.Name, cfg.Language, err)
210+
}
211+
return nil
209212
})
210213
}
211214
return g.Wait()
@@ -214,22 +217,27 @@ func generateLibraries(ctx context.Context, cfg *config.Config, libraries []*con
214217
// TODO(https://github.com/googleapis/librarian/issues/3730): separate
215218
// generation and formatting for Python.
216219
if err := python.Generate(ctx, cfg, library, src.Googleapis); err != nil {
217-
return err
220+
return fmt.Errorf("generate library %q (%s): %w", library.Name, cfg.Language, err)
218221
}
219222
}
220223
case config.LanguageRust:
221224
// Generation can be parallelized but formatting cannot because
222225
// cargo fmt shares the Cargo.toml workspace file across libraries.
223226
g, gctx := errgroup.WithContext(ctx)
224227
for _, library := range libraries {
225-
g.Go(func() error { return rust.Generate(gctx, cfg, library, src) })
228+
g.Go(func() error {
229+
if err := rust.Generate(gctx, cfg, library, src); err != nil {
230+
return fmt.Errorf("generate library %q (%s): %w", library.Name, cfg.Language, err)
231+
}
232+
return nil
233+
})
226234
}
227235
if err := g.Wait(); err != nil {
228236
return err
229237
}
230238
for _, library := range libraries {
231239
if err := rust.Format(ctx, library); err != nil {
232-
return err
240+
return fmt.Errorf("format library %q (%s): %w", library.Name, cfg.Language, err)
233241
}
234242
}
235243
default:

0 commit comments

Comments
 (0)