Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions internal/cmdutil/json_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"io"
"regexp"
"strings"

"github.com/planetscale/cli/internal/planetscale"
)

// JSONErrorIssue mirrors the issue shape used by auth check, sql, and import
Expand Down Expand Up @@ -49,6 +51,7 @@ func GlobalJSONError(err error) JSONErrorResponse {
msg := err.Error()
status := "error"
code := "COMMAND_FAILED"
apiCode := ""

if cmdErr, ok := errors.AsType[*Error](err); ok {
if cmdErr.Msg != "" {
Expand All @@ -58,6 +61,9 @@ func GlobalJSONError(err error) JSONErrorResponse {
status = "action_required"
}
}
if perr, ok := err.(*planetscale.Error); ok {
apiCode = perr.APICode
}

msg = ansiEscape.ReplaceAllString(msg, "")

Expand Down Expand Up @@ -169,6 +175,15 @@ func GlobalJSONError(err error) JSONErrorResponse {
}
}

if apiCode != "" {
code = apiCode
if apiCode == "schema_mutation_blocked" {
nextSteps = []string{
"Wait for the active vtctld mutation or deploy to finish, then retry",
}
}
}

return JSONErrorResponse{
Status: status,
Error: msg,
Expand Down
28 changes: 28 additions & 0 deletions internal/cmdutil/json_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"encoding/json"
"errors"
"testing"

"github.com/planetscale/cli/internal/planetscale"
)

func TestGlobalJSONErrorAuthRequired(t *testing.T) {
Expand Down Expand Up @@ -140,6 +142,32 @@ func TestGlobalJSONErrorGenericFallback(t *testing.T) {
}
}

func TestGlobalJSONErrorSchemaMutationBlocked(t *testing.T) {
resp := GlobalJSONError(&planetscale.Error{
APICode: "schema_mutation_blocked",
})
if resp.Code() != "schema_mutation_blocked" {
t.Fatalf("code = %q", resp.Code())
}
if len(resp.NextSteps) != 1 || resp.NextSteps[0] != "Wait for the active vtctld mutation or deploy to finish, then retry" {
t.Fatalf("next_steps = %#v", resp.NextSteps)
}
for _, step := range resp.NextSteps {
if step == AgentAuthCheckCmd() || step == AgentAuthLoginCmd() {
t.Fatalf("schema_mutation_blocked should not suggest auth, got %#v", resp.NextSteps)
}
}
}

func TestGlobalJSONErrorPreservesOtherAPICodes(t *testing.T) {
resp := GlobalJSONError(&planetscale.Error{
APICode: "unprocessable",
})
if resp.Code() != "unprocessable" {
t.Fatalf("code = %q", resp.Code())
}
}

func TestGlobalJSONErrorIssuesMirrorError(t *testing.T) {
resp := GlobalJSONError(errors.New("something went wrong"))
if len(resp.Issues) != 1 {
Expand Down
9 changes: 7 additions & 2 deletions internal/planetscale/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,9 @@ func (c *Client) handleResponse(ctx context.Context, res *http.Response, v inter
}

return &Error{
msg: errorRes.Message,
Code: errCode,
msg: errorRes.Message,
Code: errCode,
APICode: errorRes.Code,
}
}

Expand Down Expand Up @@ -661,6 +662,10 @@ type Error struct {
// Code specifies the error code. i.e; NotFound, RateLimited, etc...
Code ErrorCode

// APICode is the raw code from the API error JSON body (e.g.
// "schema_mutation_blocked"). Empty when the response had no code field.
APICode string

// Meta contains additional information depending on the error code. As an
// example, if the Code is "ErrResponseMalformed", the map will be: ["body"]
// = "body of the response"
Expand Down
18 changes: 18 additions & 0 deletions internal/planetscale/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ func TestDo(t *testing.T) {
Code: ErrInvalid,
},
},
{
desc: "preserves raw API code for schema_mutation_blocked",
statusCode: http.StatusUnprocessableEntity,
method: http.MethodPost,
response: `{
"code": "schema_mutation_blocked",
"message": "MoveTables create in progress"
}`,
expectedError: &Error{
msg: "MoveTables create in progress",
APICode: "schema_mutation_blocked",
},
},
{
desc: "returns ErrorResponse for 5xx errors",
statusCode: http.StatusInternalServerError,
Expand Down Expand Up @@ -165,6 +178,11 @@ func TestDo(t *testing.T) {
if err != nil {
if tt.expectedError != nil {
c.Assert(tt.expectedError.Error(), qt.Equals, err.Error())
if want, ok := tt.expectedError.(*Error); ok && want.APICode != "" {
got, ok := err.(*Error)
c.Assert(ok, qt.IsTrue)
c.Assert(got.APICode, qt.Equals, want.APICode)
}
}
}

Expand Down