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
28 changes: 14 additions & 14 deletions pkg/simplecontent/api/content_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (h *ContentHandler) CreateContent(w http.ResponseWriter, r *http.Request) {
content, err := h.service.CreateContent(r.Context(), createReq)
if err != nil {
slog.Error("Failed to create content", "error", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
http.Error(w, simplecontent.ToErrorMessage(err), http.StatusInternalServerError)
return
}

Expand All @@ -153,7 +153,7 @@ func (h *ContentHandler) CreateContent(w http.ResponseWriter, r *http.Request) {
}
if err := h.service.UpdateContentStatus(r.Context(), content.ID, statusEnum); err != nil {
slog.Error("Failed to update content status", "error", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
http.Error(w, simplecontent.ToErrorMessage(err), http.StatusInternalServerError)
return
}
// Update the content object with the new status
Expand All @@ -169,7 +169,7 @@ func (h *ContentHandler) CreateContent(w http.ResponseWriter, r *http.Request) {
CreatedBy: ownerID.String(),
}); err != nil {
slog.Error("Failed to set content metadata", "error", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
http.Error(w, simplecontent.ToErrorMessage(err), http.StatusInternalServerError)
return
}

Expand Down Expand Up @@ -215,7 +215,7 @@ func (h *ContentHandler) GetContent(w http.ResponseWriter, r *http.Request) {
content, err := h.service.GetContent(r.Context(), id)
if err != nil {
slog.Error("Failed to get content", "content_id", idStr, "error", err)
http.Error(w, err.Error(), http.StatusNotFound)
http.Error(w, simplecontent.ToErrorMessage(err), http.StatusNotFound)
return
}

Expand Down Expand Up @@ -315,7 +315,7 @@ func (h *ContentHandler) DeleteContent(w http.ResponseWriter, r *http.Request) {

if err := h.service.DeleteContent(r.Context(), id); err != nil {
slog.Error("Failed to delete content", "content_id", idStr, "error", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
http.Error(w, simplecontent.ToErrorMessage(err), http.StatusInternalServerError)
return
}

Expand All @@ -337,7 +337,7 @@ func (h *ContentHandler) GetContentDetails(w http.ResponseWriter, r *http.Reques

if err != nil {
slog.Error("Failed to get content details", "content_id", idStr, "error", err)
http.Error(w, err.Error(), http.StatusNotFound)
http.Error(w, simplecontent.ToErrorMessage(err), http.StatusNotFound)
return
}

Expand Down Expand Up @@ -376,7 +376,7 @@ func (h *ContentHandler) CreateObject(w http.ResponseWriter, r *http.Request) {
})
if err != nil {
slog.Error("Fail to create object", "error", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
http.Error(w, simplecontent.ToErrorMessage(err), http.StatusInternalServerError)
return
}

Expand All @@ -394,7 +394,7 @@ func (h *ContentHandler) CreateObject(w http.ResponseWriter, r *http.Request) {
uploadURL, err := h.storage.GetUploadURL(r.Context(), object.ID)
if err != nil {
slog.Error("Failed to get upload URL", "err", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
http.Error(w, simplecontent.ToErrorMessage(err), http.StatusInternalServerError)
return
}

Expand Down Expand Up @@ -430,7 +430,7 @@ func (h *ContentHandler) ListObjects(w http.ResponseWriter, r *http.Request) {
objects, err := h.service.GetObjectsByContentID(r.Context(), contentID)
if err != nil {
slog.Error("Fail to get objects by content ID", "content_id", contentIDStr, "error", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
http.Error(w, simplecontent.ToErrorMessage(err), http.StatusInternalServerError)
return
}
if len(objects) == 0 {
Expand Down Expand Up @@ -568,7 +568,7 @@ func (h *ContentHandler) CreateDerivedContent(w http.ResponseWriter, r *http.Req
})
if err != nil {
slog.Error("Failed to create derived content", "error", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
http.Error(w, simplecontent.ToErrorMessage(err), http.StatusInternalServerError)
return
}

Expand Down Expand Up @@ -675,7 +675,7 @@ func (h *ContentHandler) SetContentMetadata(w http.ResponseWriter, r *http.Reque
// Set content metadata
if err := h.service.SetContentMetadata(r.Context(), req); err != nil {
slog.Error("Failed to set content metadata", "error", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
http.Error(w, simplecontent.ToErrorMessage(err), http.StatusInternalServerError)
return
}

Expand All @@ -696,7 +696,7 @@ func (h *ContentHandler) GetContentMetadataHandler(w http.ResponseWriter, r *htt
metadata, err := h.service.GetContentMetadata(r.Context(), id)
if err != nil {
slog.Error("Failed to get content metadata", "content_id", idStr, "error", err)
http.Error(w, err.Error(), http.StatusNotFound)
http.Error(w, simplecontent.ToErrorMessage(err), http.StatusNotFound)
return
}

Expand Down Expand Up @@ -748,7 +748,7 @@ func (h *ContentHandler) GetDerivedContent(w http.ResponseWriter, r *http.Reques
derivedList, err := h.service.ListDerivedContent(r.Context(), simplecontent.WithParentID(parentID))
if err != nil {
slog.Error("Failed to get derived content", "parent_id", parentIDStr, "error", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
http.Error(w, simplecontent.ToErrorMessage(err), http.StatusInternalServerError)
return
}

Expand Down Expand Up @@ -799,7 +799,7 @@ func (h *ContentHandler) GetDerivedContentTree(w http.ResponseWriter, r *http.Re
rootContent, err := h.service.GetContent(r.Context(), rootID)
if err != nil {
slog.Error("Failed to get root content", "root_id", rootIDStr, "error", err)
http.Error(w, err.Error(), http.StatusNotFound)
http.Error(w, simplecontent.ToErrorMessage(err), http.StatusNotFound)
return
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/simplecontent/api/files_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (h *FilesHandler) CreateFile(w http.ResponseWriter, r *http.Request) {
})
if err != nil {
slog.Error("Failed to create content", "error", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
http.Error(w, simplecontent.ToErrorMessage(err), http.StatusInternalServerError)
return
}

Expand All @@ -142,7 +142,7 @@ func (h *FilesHandler) CreateFile(w http.ResponseWriter, r *http.Request) {
}
if err := h.service.SetContentMetadata(r.Context(), metadataParams); err != nil {
slog.Error("Failed to set content metadata", "error", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
http.Error(w, simplecontent.ToErrorMessage(err), http.StatusInternalServerError)
return
}

Expand All @@ -158,7 +158,7 @@ func (h *FilesHandler) CreateFile(w http.ResponseWriter, r *http.Request) {
})
if err != nil {
slog.Error("Failed to create object", "error", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
http.Error(w, simplecontent.ToErrorMessage(err), http.StatusInternalServerError)
return
}

Expand All @@ -169,15 +169,15 @@ func (h *FilesHandler) CreateFile(w http.ResponseWriter, r *http.Request) {
"file_name": req.FileName,
}); err != nil {
slog.Error("Failed to set object metadata", "error", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
http.Error(w, simplecontent.ToErrorMessage(err), http.StatusInternalServerError)
return
}

// Generate upload URL
uploadURL, err := h.storageService.GetUploadURL(r.Context(), object.ID)
if err != nil {
slog.Error("Failed to generate upload URL", "error", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
http.Error(w, simplecontent.ToErrorMessage(err), http.StatusInternalServerError)
return
}

Expand Down Expand Up @@ -206,7 +206,7 @@ func (h *FilesHandler) CompleteUpload(w http.ResponseWriter, r *http.Request) {
// Complete the upload using the unified API
if err := h.service.UpdateContentStatus(r.Context(), contentID, simplecontent.ContentStatusUploaded); err != nil {
slog.Error("Failed to complete upload", "content_id", contentID.String(), "error", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
http.Error(w, simplecontent.ToErrorMessage(err), http.StatusInternalServerError)
return
}

Expand Down
28 changes: 28 additions & 0 deletions pkg/simplecontent/api/files_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func TestFilesHandler_CreateFile_Success(t *testing.T) {
// Memory backend doesn't support upload URLs, so we expect an error
// But we can verify the request was processed
assert.Equal(t, http.StatusInternalServerError, w.Code)
t.Logf("CreateFile_Success body: %s", w.Body.String())
}

func TestFilesHandler_CreateFile_InvalidOwnerID(t *testing.T) {
Expand All @@ -97,6 +98,7 @@ func TestFilesHandler_CreateFile_InvalidOwnerID(t *testing.T) {
router.ServeHTTP(w, req)

assert.Equal(t, http.StatusBadRequest, w.Code)
t.Logf("CreateFile_InvalidOwnerID body: %s", w.Body.String())
assert.Contains(t, w.Body.String(), "Invalid owner ID")
}

Expand All @@ -123,6 +125,7 @@ func TestFilesHandler_CreateFile_MissingOwnerType(t *testing.T) {
router.ServeHTTP(w, req)

assert.Equal(t, http.StatusBadRequest, w.Code)
t.Logf("CreateFile_MissingOwnerType body: %s", w.Body.String())
assert.Contains(t, w.Body.String(), "Owner type is required")
}

Expand All @@ -149,6 +152,7 @@ func TestFilesHandler_CreateFile_MissingDocumentType(t *testing.T) {
router.ServeHTTP(w, req)

assert.Equal(t, http.StatusBadRequest, w.Code)
t.Logf("CreateFile_MissingDocumentType body: %s", w.Body.String())
assert.Contains(t, w.Body.String(), "Document type is required")
}

Expand Down Expand Up @@ -197,6 +201,7 @@ func TestFilesHandler_CompleteUpload_InvalidContentID(t *testing.T) {
router.ServeHTTP(w, req)

assert.Equal(t, http.StatusBadRequest, w.Code)
t.Logf("CompleteUpload_InvalidContentID body: %s", w.Body.String())
assert.Contains(t, w.Body.String(), "Invalid content ID")
}

Expand Down Expand Up @@ -257,6 +262,7 @@ func TestFilesHandler_GetFileInfo_NotFound(t *testing.T) {

w := httptest.NewRecorder()
router.ServeHTTP(w, req)
t.Logf("GetFileInfo_NotFound body: %s", w.Body.String())

assert.Equal(t, http.StatusNotFound, w.Code)
}
Expand Down Expand Up @@ -328,6 +334,7 @@ func TestFilesHandler_GetFilesByContentIDs_MissingIDParameter(t *testing.T) {
router.ServeHTTP(w, req)

assert.Equal(t, http.StatusBadRequest, w.Code)
t.Logf("GetFilesByContentIDs_MissingIDParameter body: %s", w.Body.String())
assert.Contains(t, w.Body.String(), "Missing required 'id' parameter")
}

Expand All @@ -351,5 +358,26 @@ func TestFilesHandler_GetFilesByContentIDs_TooManyIDs(t *testing.T) {
router.ServeHTTP(w, req)

assert.Equal(t, http.StatusBadRequest, w.Code)
t.Logf("GetFilesByContentIDs_TooManyIDs body: %s", w.Body.String())
assert.Contains(t, w.Body.String(), "Too many IDs requested")
}

// New test to trigger JSON decode error and print the error message
func TestFilesHandler_CreateFile_InvalidJSON(t *testing.T) {
handler, _, _ := setupFilesHandlerTest(t)
router := chi.NewRouter()
router.Post("/", handler.CreateFile)

// Invalid JSON body
body := []byte("{ invalid json")

req := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()

router.ServeHTTP(w, req)

// Expect 400 with decode error message
assert.Equal(t, http.StatusBadRequest, w.Code)
t.Logf("CreateFile_InvalidJSON body: %s", w.Body.String())
}
Loading
Loading