Standardize API response structure and fix inconsistent error handling #39
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The API had inconsistent response formats—success responses returned raw data while errors returned
{"error": "message"}, the reset endpoint returnednil, and errors lacked machine-readable codes. The frontend displayed hardcoded generic messages instead of server-provided details.Backend Changes
Added standardized response types:
Updated all endpoints to return consistent responses:
createCollection: Returns{success, message, data: {name, created_at}}reset: Fixedreturn nil→ returns{success, message, data: {collection, reset_at}}search: Removed debugfmt.Println(r), wraps results with query metadataNOT_FOUND,INVALID_REQUEST,INTERNAL_ERROR,UNAUTHORIZED,CONFLICTFrontend Changes
Added global response handler:
Updated all fetch handlers to extract and display server-provided messages from
data.messageanddata.error.message.Example Responses
Success:
{ "success": true, "message": "Collection created successfully", "data": { "name": "test-collection", "created_at": "2026-01-12T22:13:20Z" } }Error:
{ "success": false, "error": { "code": "NOT_FOUND", "message": "Collection not found", "details": "Collection 'nonexistent' does not exist" } }Original prompt
Problem
The current API implementation has inconsistent error handling and JSON response structures:
Backend Issues (routes.go)
Inconsistent JSON Response Structure:
collection,results, arrays){"error": "message"}Missing Success Indicators: When creating a collection (
createCollection), the API returns the raw collection object with nosuccess,message, or operation status fields.No Response on Reset: The
reset()function returnsnil(no response body at all) on line 154:Debug Code in Production: There's a
fmt.Println(r)in thesearch()function (line 176) that should be removed.No Error Codes: Errors only contain messages, no error codes for programmatic handling by clients.
Frontend Issues (static/js/collectionManager.js)
Generic Error Messages: The frontend shows hardcoded generic messages like "Failed to create collection" instead of server-provided error details.
Error Details Not Extracted: While the search function attempts to parse error JSON, other functions like
createCollection,uploadFile,resetCollectiondon't properly extract and display server error messages.No Operation Result Feedback: For successful operations, there's no confirmation of what was actually done.
Required Changes
1. Create Standardized API Response Structure
Add new types to
routes.go:2. Add Helper Functions and Error Codes
3. Update All Route Handlers
Update all handlers in
routes.goto use the new response format:createCollection: Return success response with collection name and created_at timestampreset: Fix thereturn nilissue - return proper JSON success responseuploadFile: Return success response with filename and collection infodeleteEntryFromCollection: Return success response with remaining entriesregisterExternalSource: Already returns a message, but should use new formatremoveExternalSource: Already returns a message, but should use new formatsearch: Removefmt.Println(r)debug statement, wrap results in response formaterrorResponse()with appropriate error codes4. Update Frontend Error Handling
Update
static/js/collectionManager.js:createCollection()to parse and display server-provided messagesuploadFile()to show detailed error messagesresetCollection()to show server response messagesfetchCollections(),listEntries(), and other functions similarlyExample updated fetch handler pattern:
Files to Modify
routes.go- Add response types, helper functions, error codes, and update all handlersstatic/js/collectionManager.js- Update all fetch handlers to use new response formatExpected Outcome
After these changes:
success,message,data, anderrorfieldsresetendpoint returns a proper re...This pull request was created from Copilot chat.
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.