Skip to content

Commit 79fbf70

Browse files
committed
Add better error messages
Don't make a request if the API token is missing
1 parent 28b7b68 commit 79fbf70

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

internal/bundler/upload.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7+
"io"
78
"mime/multipart"
89
"net/http"
910
"os"
@@ -12,13 +13,15 @@ import (
1213
)
1314

1415
type Uploader struct {
16+
token string
1517
configPath string
1618
config *config.Config
1719
dir string
1820
}
1921

2022
func NewUploader(configPath, dir string, conf *config.Config) *Uploader {
2123
return &Uploader{
24+
token: os.Getenv("SQLC_AUTH_TOKEN"),
2225
configPath: configPath,
2326
config: conf,
2427
dir: dir,
@@ -27,7 +30,10 @@ func NewUploader(configPath, dir string, conf *config.Config) *Uploader {
2730

2831
func (up *Uploader) Validate() error {
2932
if up.config.Project.ID == "" {
30-
return fmt.Errorf("project ID is not set")
33+
return fmt.Errorf("project.id is not set")
34+
}
35+
if up.token == "" {
36+
return fmt.Errorf("SQLC_AUTH_TOKEN environment variable is not set")
3137
}
3238
return nil
3339
}
@@ -40,24 +46,22 @@ func (up *Uploader) Upload(ctx context.Context, result map[string]string) error
4046

4147
w := multipart.NewWriter(body)
4248
defer w.Close()
43-
4449
if err := writeInputs(w, up.configPath, up.config); err != nil {
4550
return err
4651
}
4752
if err := writeOutputs(w, up.dir, result); err != nil {
4853
return err
4954
}
50-
5155
w.Close()
5256

53-
req, err := http.NewRequest("POST", "http://localhost:8090/upload", body)
57+
req, err := http.NewRequest("POST", "https://api.sqlc.dev/upload", body)
5458
if err != nil {
5559
return err
5660
}
5761

5862
// Set sqlc-version header
5963
req.Header.Set("Content-Type", w.FormDataContentType())
60-
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", os.Getenv("SQLC_AUTH_TOKEN")))
64+
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", up.token))
6165
req = req.WithContext(ctx)
6266

6367
client := &http.Client{}
@@ -66,7 +70,12 @@ func (up *Uploader) Upload(ctx context.Context, result map[string]string) error
6670
return err
6771
}
6872
if resp.StatusCode >= 400 {
69-
return fmt.Errorf("upload endpiont returned non-200 status code: %d", resp.StatusCode)
73+
body, err := io.ReadAll(resp.Body)
74+
defer resp.Body.Close()
75+
if err != nil {
76+
return fmt.Errorf("upload error: endpoint returned non-200 status code: %d", resp.StatusCode)
77+
}
78+
return fmt.Errorf("upload error: %d: %s", resp.StatusCode, string(body))
7079
}
7180
return nil
7281
}

0 commit comments

Comments
 (0)