summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'main.go')
-rw-r--r--main.go19
1 files changed, 18 insertions, 1 deletions
diff --git a/main.go b/main.go
index 71f8d34..c4104e3 100644
--- a/main.go
+++ b/main.go
@@ -26,6 +26,7 @@ func (s *storehouse) putObject(c echo.Context) error {
bucketId := c.Param("bucketId")
objectId := c.Param("objectId")
+ // Get body as a string
bodyBytes, err := ioutil.ReadAll(c.Request().Body)
if err != nil {
log.Printf("Error reading body: %v", err)
@@ -34,7 +35,23 @@ func (s *storehouse) putObject(c echo.Context) error {
}
content := string(bodyBytes)
- s.objects.CreateObject(bucketId, objectId, content)
+ err = s.objects.CreateObject(bucketId, objectId, content)
+ // Check errors creating the object
+ if err != nil {
+ log.Printf("Error creating the object: %v", err)
+ switch err.(type) {
+ case *objectstore.DuplicateError:
+ // Return "409 Conflict" if there
+ // is a duplicate of the object
+ m := &errorJSON{Error: err.Error()}
+ return c.JSON(http.StatusConflict, m)
+ default:
+ m := &errorJSON{Error: "Internal error"}
+ return c.JSON(http.StatusInternalServerError, m)
+ }
+ }
+
+ // Return success
m := &objectIdJSON{Id: objectId}
return c.JSON(http.StatusCreated, m)
}