Skip to content

Commit

Permalink
chore: add stacktrace to internal errors
Browse files Browse the repository at this point in the history
  • Loading branch information
sweatybridge committed Dec 17, 2023
1 parent a5308b4 commit 79b74c0
Show file tree
Hide file tree
Showing 99 changed files with 441 additions and 392 deletions.
6 changes: 1 addition & 5 deletions internal/bans/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ func Run(ctx context.Context, projectRef string, fsys afero.Fs) error {
return errors.Errorf("failed to retrieve network bans: %w", err)
}
if resp.JSON201 == nil {
return errors.New("failed to retrieve network bans; received: " + string(resp.Body))
}

if err != nil {
return err
return errors.New("Unexpected error retrieving network bans: " + string(resp.Body))
}
fmt.Printf("DB banned IPs: %+v\n", resp.JSON201.BannedIpv4Addresses)
return nil
Expand Down
4 changes: 2 additions & 2 deletions internal/bans/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ func Run(ctx context.Context, projectRef string, dbIpsToUnban []string, fsys afe
Ipv4Addresses: dbIpsToUnban,
})
if err != nil {
return err
return errors.Errorf("failed to remove network bans: %w", err)
}
if resp.StatusCode() != 200 {
return errors.New("failed to remove network bans: " + string(resp.Body))
return errors.New("Unexpected error removing network bans: " + string(resp.Body))
}
fmt.Printf("Successfully removed bans for %+v.\n", dbIpsToUnban)
return nil
Expand Down
4 changes: 2 additions & 2 deletions internal/branches/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package create

import (
"context"
"errors"
"fmt"

"github.com/go-errors/errors"
"github.com/spf13/afero"
"github.com/supabase/cli/internal/gen/keys"
"github.com/supabase/cli/internal/utils"
Expand All @@ -27,7 +27,7 @@ func Run(ctx context.Context, name, region string, fsys afero.Fs) error {
Region: &region,
})
if err != nil {
return err
return errors.Errorf("failed to create preview branch: %w", err)
}

if resp.JSON201 == nil {
Expand Down
80 changes: 80 additions & 0 deletions internal/branches/create/create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package create

import (
"context"
"net"
"net/http"
"os"
"testing"

"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/supabase/cli/internal/testing/apitest"
"github.com/supabase/cli/internal/testing/fstest"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/pkg/api"
"gopkg.in/h2non/gock.v1"
)

func TestCreateCommand(t *testing.T) {
t.Run("creates preview branch", func(t *testing.T) {
ref := apitest.RandomProjectRef()
// Setup in-memory fs
fsys := afero.NewMemMapFs()
require.NoError(t, afero.WriteFile(fsys, utils.ProjectRefPath, []byte(ref), 0644))
// Setup mock api
defer gock.OffAll()
gock.New(utils.DefaultApiHost).
Post("/v1/projects/" + ref + "/branches").
Reply(http.StatusCreated).
JSON(api.BranchResponse{
Id: "test-uuid",
})
// Run test
err := Run(context.Background(), "", "sin", fsys)
// Check error
assert.NoError(t, err)
})

t.Run("throws error on permission denied", func(t *testing.T) {
// Setup in-memory fs
fsys := &fstest.OpenErrorFs{DenyPath: utils.ProjectRefPath}
// Run test
err := Run(context.Background(), "branch", "region", fsys)
// Check error
assert.ErrorIs(t, err, os.ErrPermission)
})

t.Run("throws error on network disconnected", func(t *testing.T) {
ref := apitest.RandomProjectRef()
// Setup in-memory fs
fsys := afero.NewMemMapFs()
require.NoError(t, afero.WriteFile(fsys, utils.ProjectRefPath, []byte(ref), 0644))
// Setup mock api
defer gock.OffAll()
gock.New(utils.DefaultApiHost).
Post("/v1/projects/" + ref + "/branches").
ReplyError(net.ErrClosed)
// Run test
err := Run(context.Background(), "", "sin", fsys)
// Check error
assert.ErrorIs(t, err, net.ErrClosed)
})

t.Run("throws error on service unavailable", func(t *testing.T) {
ref := apitest.RandomProjectRef()
// Setup in-memory fs
fsys := afero.NewMemMapFs()
require.NoError(t, afero.WriteFile(fsys, utils.ProjectRefPath, []byte(ref), 0644))
// Setup mock api
defer gock.OffAll()
gock.New(utils.DefaultApiHost).
Post("/v1/projects/" + ref + "/branches").
Reply(http.StatusServiceUnavailable)
// Run test
err := Run(context.Background(), "", "sin", fsys)
// Check error
assert.ErrorContains(t, err, "Unexpected error creating preview branch:")
})
}
4 changes: 2 additions & 2 deletions internal/branches/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ package delete

import (
"context"
"errors"
"fmt"
"net/http"

"github.com/go-errors/errors"
"github.com/supabase/cli/internal/utils"
)

func Run(ctx context.Context, branchId string) error {
resp, err := utils.GetSupabase().DeleteBranchWithResponse(ctx, branchId)
if err != nil {
return err
return errors.Errorf("failed to delete preview branch: %w", err)
}
if resp.StatusCode() != http.StatusOK {
return errors.New("Unexpected error deleting preview branch: " + string(resp.Body))
Expand Down
4 changes: 2 additions & 2 deletions internal/branches/disable/disable.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package disable

import (
"context"
"errors"
"fmt"
"net/http"

"github.com/go-errors/errors"
"github.com/spf13/afero"
"github.com/supabase/cli/internal/utils"
)
Expand All @@ -17,7 +17,7 @@ func Run(ctx context.Context, fsys afero.Fs) error {
}
resp, err := utils.GetSupabase().DisableBranchWithResponse(ctx, ref)
if err != nil {
return err
return errors.Errorf("failed to disable preview branching: %w", err)
}
if resp.StatusCode() != http.StatusOK {
return errors.New("Unexpected error disabling preview branching: " + string(resp.Body))
Expand Down
4 changes: 2 additions & 2 deletions internal/branches/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ package get

import (
"context"
"errors"
"fmt"

"github.com/go-errors/errors"
"github.com/supabase/cli/internal/migration/list"
"github.com/supabase/cli/internal/utils"
)

func Run(ctx context.Context, branchId string) error {
resp, err := utils.GetSupabase().GetBranchDetailsWithResponse(ctx, branchId)
if err != nil {
return err
return errors.Errorf("failed to retrieve preview branch: %w", err)
}
if resp.JSON200 == nil {
return errors.New("Unexpected error retrieving preview branch: " + string(resp.Body))
Expand Down
4 changes: 2 additions & 2 deletions internal/branches/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package list

import (
"context"
"errors"
"fmt"
"strings"

"github.com/go-errors/errors"
"github.com/spf13/afero"
"github.com/supabase/cli/internal/migration/list"
"github.com/supabase/cli/internal/utils"
Expand All @@ -18,7 +18,7 @@ func Run(ctx context.Context, fsys afero.Fs) error {
}
resp, err := utils.GetSupabase().GetBranchesWithResponse(ctx, ref)
if err != nil {
return err
return errors.Errorf("failed to list preview branches: %w", err)
}

if resp.JSON200 == nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/branches/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package update

import (
"context"
"errors"
"fmt"

"github.com/go-errors/errors"
"github.com/spf13/afero"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/pkg/api"
Expand All @@ -13,7 +13,7 @@ import (
func Run(ctx context.Context, branchId string, body api.UpdateBranchBody, fsys afero.Fs) error {
resp, err := utils.GetSupabase().UpdateBranchWithResponse(ctx, branchId, body)
if err != nil {
return err
return errors.Errorf("failed to update preview branch: %w", err)
}
if resp.JSON200 == nil {
return errors.New("Unexpected error updating preview branch: " + string(resp.Body))
Expand Down
2 changes: 1 addition & 1 deletion internal/db/branch/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"bytes"
"context"
_ "embed"
"errors"
"fmt"
"io"
"os"
"path/filepath"

"github.com/docker/docker/api/types"
"github.com/docker/docker/pkg/stdcopy"
"github.com/go-errors/errors"
"github.com/spf13/afero"
"github.com/supabase/cli/internal/utils"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/db/branch/list/list.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package list

import (
"errors"
"fmt"
"io"
"os"
"path/filepath"

"github.com/go-errors/errors"
"github.com/spf13/afero"
"github.com/supabase/cli/internal/utils"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/db/branch/switch_/switch_.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package switch_

import (
"context"
"errors"
"fmt"
"os"
"path/filepath"

"github.com/go-errors/errors"
"github.com/jackc/pgconn"
"github.com/jackc/pgx/v4"
"github.com/spf13/afero"
Expand Down
5 changes: 3 additions & 2 deletions internal/db/diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"

"github.com/go-errors/errors"
"github.com/jackc/pgconn"
"github.com/spf13/afero"
"github.com/supabase/cli/internal/db/start"
Expand All @@ -22,7 +23,7 @@ func SaveDiff(out, file string, fsys afero.Fs) error {
} else if len(file) > 0 {
path := new.GetMigrationPath(utils.GetCurrentTimestamp(), file)
if err := afero.WriteFile(fsys, path, []byte(out), 0644); err != nil {
return err
return errors.Errorf("failed to save diff: %w", err)
}
fmt.Fprintln(os.Stderr, warnDiff)
} else {
Expand Down Expand Up @@ -63,7 +64,7 @@ func run(p utils.Program, ctx context.Context, schema []string, config pgconn.Co
}
defer utils.DockerRemove(shadow)
if !start.WaitForHealthyService(ctx, shadow, start.HealthTimeout) {
return start.ErrDatabase
return errors.New(start.ErrDatabase)
}
if err := MigrateShadowDatabase(ctx, shadow, fsys); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion internal/db/diff/migra.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func DiffDatabase(ctx context.Context, schema []string, config pgconn.Config, w
}
defer utils.DockerRemove(shadow)
if !start.WaitForHealthyService(ctx, shadow, start.HealthTimeout) {
return "", start.ErrDatabase
return "", errors.New(start.ErrDatabase)
}
if err := MigrateShadowDatabase(ctx, shadow, fsys, options...); err != nil {
return "", err
Expand Down
3 changes: 2 additions & 1 deletion internal/db/dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/go-errors/errors"
"github.com/jackc/pgconn"
"github.com/spf13/afero"
"github.com/supabase/cli/internal/utils"
Expand All @@ -30,7 +31,7 @@ func Run(ctx context.Context, path string, config pgconn.Config, schema []string
if len(path) > 0 {
f, err := fsys.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
return errors.Errorf("failed to open dump file: %w", err)
}
defer f.Close()
outStream = f
Expand Down
18 changes: 11 additions & 7 deletions internal/db/lint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"strings"

"github.com/go-errors/errors"
"github.com/jackc/pgconn"
"github.com/jackc/pgx/v4"
"github.com/spf13/afero"
Expand Down Expand Up @@ -80,13 +81,16 @@ func printResultJSON(result []Result, minLevel LintLevel, stdout io.Writer) erro
// Pretty print output
enc := json.NewEncoder(stdout)
enc.SetIndent("", " ")
return enc.Encode(filtered)
if err := enc.Encode(filtered); err != nil {
return errors.Errorf("failed to print result json: %w", err)
}
return nil
}

func LintDatabase(ctx context.Context, conn *pgx.Conn, schema []string) ([]Result, error) {
tx, err := conn.Begin(ctx)
if err != nil {
return nil, err
return nil, errors.Errorf("failed to begin transaction: %w", err)
}
if len(schema) == 0 {
schema, err = diff.LoadUserSchemas(ctx, conn, utils.InternalSchemas...)
Expand All @@ -101,7 +105,7 @@ func LintDatabase(ctx context.Context, conn *pgx.Conn, schema []string) ([]Resul
}
}()
if _, err := conn.Exec(ctx, ENABLE_PGSQL_CHECK); err != nil {
return nil, err
return nil, errors.Errorf("failed to enable pgsql_check: %w", err)
}
// Batch prepares statements
batch := pgx.Batch{}
Expand All @@ -115,26 +119,26 @@ func LintDatabase(ctx context.Context, conn *pgx.Conn, schema []string) ([]Resul
fmt.Fprintln(os.Stderr, "Linting schema:", s)
rows, err := br.Query()
if err != nil {
return nil, err
return nil, errors.Errorf("failed to query rows: %w", err)
}
// Parse result row
for rows.Next() {
var name string
var data []byte
if err := rows.Scan(&name, &data); err != nil {
return nil, err
return nil, errors.Errorf("failed to scan rows: %w", err)
}
var r Result
if err := json.Unmarshal(data, &r); err != nil {
return nil, err
return nil, errors.Errorf("failed to marshal json: %w", err)
}
// Update function name
r.Function = s + "." + name
result = append(result, r)
}
err = rows.Err()
if err != nil {
return nil, err
return nil, errors.Errorf("failed to parse rows: %w", err)
}
}
return result, nil
Expand Down
Loading

0 comments on commit 79b74c0

Please sign in to comment.