Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

standardize error reporting from persistent and non persistent config #6615

Merged
merged 1 commit into from
Dec 19, 2023
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
32 changes: 17 additions & 15 deletions rest/admin_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,7 @@ func (h *handler) handleCreateDB() error {

_, err = h.server._applyConfig(contextNoCancel, loadedConfig, true, false)
if err != nil {
var httpErr *base.HTTPError
if errors.As(err, &httpErr) {
return httpErr
}
if errors.Is(err, base.ErrAuthError) {
return base.HTTPErrorf(http.StatusForbidden, "Provided credentials do not have access to specified bucket/scope/collection")
}
if errors.Is(err, base.ErrAlreadyExists) {
return base.HTTPErrorf(http.StatusConflict, "couldn't load database: %s", err)
}
return base.HTTPErrorf(http.StatusInternalServerError, "couldn't load database: %v", err)
return databaseLoadErrorAsHTTPError(err)
}

// now we've started the db successfully, we can persist it to the cluster first checking if this db used to be a corrupt db
Expand Down Expand Up @@ -167,10 +157,7 @@ func (h *handler) handleCreateDB() error {

// load database in-memory for non-persistent nodes
if _, err := h.server.AddDatabaseFromConfigFailFast(contextNoCancel, DatabaseConfig{DbConfig: *config}); err != nil {
if errors.Is(err, base.ErrAuthError) {
return base.HTTPErrorf(http.StatusForbidden, "auth failure using provided bucket credentials for database %s", base.MD(config.Name))
}
return base.HTTPErrorf(http.StatusInternalServerError, "couldn't load database: %v", err)
return databaseLoadErrorAsHTTPError(err)
}
}

Expand Down Expand Up @@ -1768,3 +1755,18 @@ func (h *handler) handleGetClusterInfo() error {
h.writeJSON(clusterInfo)
return nil
}

// databaseLoadErrorAsHTTPError converts an error loading a database into an error with an http status code. Pulled into a function so we can duplicate persistent and non persistent config logic.
func databaseLoadErrorAsHTTPError(err error) error {
var httpErr *base.HTTPError
if errors.As(err, &httpErr) {
return httpErr
}
if errors.Is(err, base.ErrAuthError) {
return base.HTTPErrorf(http.StatusForbidden, "Provided credentials do not have access to specified bucket/scope/collection")
}
if errors.Is(err, base.ErrAlreadyExists) {
return base.HTTPErrorf(http.StatusConflict, "couldn't load database: %s", err)
}
return base.HTTPErrorf(http.StatusInternalServerError, "couldn't load database: %v", err)
}
11 changes: 11 additions & 0 deletions rest/adminapitest/admin_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4367,3 +4367,14 @@ func TestDeleteDatabaseCBGTTeardown(t *testing.T) {
time.Sleep(1 * time.Second) // some time for polling
}
}

func TestDatabaseCreationErrorCode(t *testing.T) {
for _, persistentConfig := range []bool{true, false} {
rt := rest.NewRestTester(t, &rest.RestTesterConfig{PersistentConfig: persistentConfig})
defer rt.Close()

rt.CreateDatabase("db", rt.NewDbConfig())
resp := rt.SendAdminRequest(http.MethodPut, "/db/", `{"bucket": "irrelevant"}`)
rest.RequireStatus(t, resp, http.StatusPreconditionFailed)
}
}