-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: better flag create error handling (#85)
Handle 401 error consistent with other errors
- Loading branch information
Showing
5 changed files
with
129 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package errors_test | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"testing" | ||
|
||
ldapi "github.com/launchdarkly/api-client-go/v14" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
errs "ldcli/internal/errors" | ||
) | ||
|
||
func TestAPIError(t *testing.T) { | ||
t.Run("with a 400 error has a JSON response", func(t *testing.T) { | ||
underlying := errs.NewAPIError( | ||
[]byte(`{"code":"conflict","message":"an error"}`), | ||
errors.New("409 Conflict"), | ||
[]string{}, | ||
) | ||
|
||
err := errs.NewLDAPIError(underlying) | ||
|
||
require.Error(t, err) | ||
assert.JSONEq(t, `{"code": "conflict", "message": "an error"}`, err.Error()) | ||
}) | ||
|
||
t.Run("with a 401 error has a JSON response", func(t *testing.T) { | ||
rep := ldapi.UnauthorizedErrorRep{} | ||
repBytes, err := json.Marshal(rep) | ||
require.NoError(t, err) | ||
underlying := errs.NewAPIError( | ||
repBytes, | ||
errors.New("401 Unauthorized"), | ||
[]string{}, | ||
) | ||
|
||
err = errs.NewLDAPIError(underlying) | ||
|
||
require.Error(t, err) | ||
assert.JSONEq(t, `{"code": "unauthorized", "message": "You do not have access to perform this action"}`, err.Error()) | ||
}) | ||
|
||
t.Run("with a 403 error has a JSON response", func(t *testing.T) { | ||
underlying := errs.NewAPIError( | ||
[]byte(`{"code": "forbidden", "message": "an error"}`), | ||
errors.New("409 Conflict"), | ||
[]string{}, | ||
) | ||
|
||
err := errs.NewLDAPIError(underlying) | ||
|
||
require.Error(t, err) | ||
assert.JSONEq(t, `{"code": "forbidden", "message": "an error"}`, err.Error()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters