From f24f54cc5baadfbf859db2b0c3f7579e17d8b05f Mon Sep 17 00:00:00 2001 From: Edward Muller Date: Tue, 29 Apr 2014 20:44:41 -0700 Subject: [PATCH] Close #483. Return 409 if a database already exist --- src/api/http/api.go | 2 ++ src/cluster/cluster_configuration.go | 2 +- src/common/error.go | 10 ++++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/api/http/api.go b/src/api/http/api.go index 649df80fed6..ca204df7c0f 100644 --- a/src/api/http/api.go +++ b/src/api/http/api.go @@ -321,6 +321,8 @@ func errorToStatusCode(err error) int { return libhttp.StatusUnauthorized // HTTP 401 case AuthorizationError: return libhttp.StatusForbidden // HTTP 403 + case DatabaseExistsError: + return libhttp.StatusConflict // HTTP 409 default: return libhttp.StatusBadRequest // HTTP 400 } diff --git a/src/cluster/cluster_configuration.go b/src/cluster/cluster_configuration.go index 95ebb23e761..d7797d32ea0 100644 --- a/src/cluster/cluster_configuration.go +++ b/src/cluster/cluster_configuration.go @@ -271,7 +271,7 @@ func (self *ClusterConfiguration) CreateDatabase(name string, replicationFactor defer self.createDatabaseLock.Unlock() if _, ok := self.DatabaseReplicationFactors[name]; ok { - return fmt.Errorf("database %s exists", name) + return common.NewDatabaseExistsError(name) } self.DatabaseReplicationFactors[name] = replicationFactor return nil diff --git a/src/common/error.go b/src/common/error.go index 5d3f3eed4ad..16a2f07a25f 100644 --- a/src/common/error.go +++ b/src/common/error.go @@ -42,3 +42,13 @@ func (self AuthorizationError) Error() string { func NewAuthorizationError(formatStr string, args ...interface{}) AuthorizationError { return AuthorizationError(fmt.Sprintf(formatStr, args...)) } + +type DatabaseExistsError string + +func (self DatabaseExistsError) Error() string { + return string(self) +} + +func NewDatabaseExistsError(db string) DatabaseExistsError { + return DatabaseExistsError(fmt.Sprintf("database %s exists", db)) +}