@@ -17,6 +17,7 @@ package java
1717
1818import (
1919 "context"
20+ "errors"
2021 "fmt"
2122 "os"
2223 "os/exec"
@@ -36,6 +37,13 @@ const (
3637 commonProtos = "google/cloud/common_resources.proto"
3738)
3839
40+ var (
41+ // errExtractVersion is returned when an API version cannot be extracted from its path.
42+ errExtractVersion = errors .New ("failed to extract version" )
43+ // errNoProtos is returned when no proto files are found in an API directory.
44+ errNoProtos = errors .New ("no protos found" )
45+ )
46+
3947// Generate generates a Java client library.
4048func Generate (ctx context.Context , cfg * config.Config , library * config.Library , srcs * sources.Sources ) error {
4149 outdir , err := filepath .Abs (library .Output )
@@ -49,7 +57,7 @@ func Generate(ctx context.Context, cfg *config.Config, library *config.Library,
4957 return fmt .Errorf ("failed to resolve googleapis directory path: %w" , err )
5058 }
5159 if err := os .MkdirAll (outdir , 0755 ); err != nil {
52- return fmt .Errorf ("failed to create output directory: %w" , err )
60+ return fmt .Errorf ("failed to create output directory %q : %w" , outdir , err )
5361 }
5462 for _ , api := range library .APIs {
5563 if err := generateAPI (ctx , cfg , api , library , googleapisDir , outdir ); err != nil {
@@ -62,7 +70,7 @@ func Generate(ctx context.Context, cfg *config.Config, library *config.Library,
6270func generateAPI (ctx context.Context , cfg * config.Config , api * config.API , library * config.Library , googleapisDir , outdir string ) error {
6371 version := serviceconfig .ExtractVersion (api .Path )
6472 if version == "" {
65- return fmt .Errorf ("failed to generate api: failed to extract version from api path %q " , api .Path )
73+ return fmt .Errorf ("%s: %w " , api .Path , errExtractVersion )
6674 }
6775 javaAPI := resolveJavaAPI (library , api )
6876 p := postProcessParams {
@@ -77,7 +85,7 @@ func generateAPI(ctx context.Context, cfg *config.Config, api *config.API, libra
7785 }
7886 for _ , dir := range []string {p .gapicDir , p .grpcDir , p .protoDir } {
7987 if err := os .MkdirAll (dir , 0755 ); err != nil {
80- return fmt .Errorf ("failed to create directory %s : %w" , dir , err )
88+ return fmt .Errorf ("failed to create directory %q : %w" , dir , err )
8189 }
8290 }
8391
@@ -91,7 +99,7 @@ func generateAPI(ctx context.Context, cfg *config.Config, api *config.API, libra
9199 return fmt .Errorf ("failed to find protos: %w" , err )
92100 }
93101 if len (apiProtos ) == 0 {
94- return fmt .Errorf ("failed to generate api: no protos found in api %q " , api .Path )
102+ return fmt .Errorf ("%s: %w " , api .Path , errNoProtos )
95103 }
96104 p .apiProtos = apiProtos
97105
@@ -246,7 +254,7 @@ func Format(ctx context.Context, library *config.Library) error {
246254
247255 args := append ([]string {"--replace" }, files ... )
248256 if err := command .Run (ctx , "google-java-format" , args ... ); err != nil {
249- return fmt .Errorf ("formatting failed: %w" , err )
257+ return fmt .Errorf ("failed to format files : %w" , err )
250258 }
251259 return nil
252260}
0 commit comments