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

try using aider to refactor #613

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
5728df9
refactor: consolidate error handling using APIError struct
swuecho Feb 27, 2025
f005ce4
chore: Update .gitignore to include .aider* files
swuecho Feb 27, 2025
f98ca34
fix: refactor error handling functions for consistency
swuecho Feb 27, 2025
2d7ec39
fix: correct RespondWithErrorMessage usage with proper error wrapping
swuecho Feb 27, 2025
249460e
fix: wrap error strings with eris.New() in middleware_authenticate.go
swuecho Feb 27, 2025
f2bd303
more
swuecho Feb 27, 2025
5303479
refactor: Improve error catalog key definition for consistency
swuecho Feb 27, 2025
be981d9
refactor: Centralize and standardize API error handling in ErrorCatalog
swuecho Feb 27, 2025
a4255b5
refactor: Replace direct error responses with API error handling in c…
swuecho Feb 27, 2025
2799f6c
feat: Add TooManyRequests APIError to ErrorCatalog
swuecho Feb 27, 2025
8403541
refactor: Standardize error handling in chat session handler
swuecho Feb 27, 2025
5221611
refactor: Standardize error handling in authentication middleware usi…
swuecho Feb 27, 2025
37f056d
refactor: Improve rate limit middleware error handling with API error…
swuecho Feb 27, 2025
a985c38
refactor: Standardize error handling in chat snapshot handler using A…
swuecho Feb 27, 2025
452c40c
refactor: Standardize error handling in chat model handler using API …
swuecho Feb 27, 2025
647aa7a
refactor: Remove unused imports in chat_session_handler.go
swuecho Feb 27, 2025
81d4bd6
refactor: Improve error handling and add more detailed error types
swuecho Feb 27, 2025
b93ed30
refactor: Improve error handling for external services and context er…
swuecho Feb 27, 2025
0d8f629
Remove unused `eris` dependency from middleware files.
swuecho Feb 27, 2025
ec0b563
refactor: Enhance error handling with structured validation, rate lim…
swuecho Feb 27, 2025
f9cdba7
refactor: Update method signatures to pass HTTP request context for c…
swuecho Feb 27, 2025
1e945e6
refactor: Add HTTP request parameter to chat stream methods
swuecho Feb 27, 2025
0509bd9
Remove unused imports and temperature validation in chat handlers
swuecho Feb 27, 2025
c8caf43
refactor: Improve error handling and logging in Gemini API integration
swuecho Feb 27, 2025
b758381
refactor: Update ChatModel interface and function signatures to inclu…
swuecho Feb 27, 2025
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
env.sh
env.ps
data
.python-version
.python-version
.aider*
303 changes: 223 additions & 80 deletions api/chat_main_handler.go

Large diffs are not rendered by default.

81 changes: 54 additions & 27 deletions api/chat_model_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package main

import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"time"

"github.com/gorilla/mux"
"github.com/rotisserie/eris"
"github.com/samber/lo"
"github.com/swuecho/chat_backend/sqlc_queries"
)
Expand Down Expand Up @@ -43,15 +41,19 @@ func (h *ChatModelHandler) ListSystemChatModels(w http.ResponseWriter, r *http.R
ctx := r.Context()
ChatModels, err := h.db.ListSystemChatModels(ctx)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("Error listing chat APIs: %s", err.Error())))
apiErr := ErrInternalUnexpected
apiErr.Detail = "Failed to list chat models"
apiErr.DebugInfo = err.Error()
RespondWithAPIError(w, apiErr)
return
}

latestUsageTimeOfModels, err := h.db.GetLatestUsageTimeOfModel(ctx, "30 days")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("Error listing chat APIs: %s", err.Error())))
apiErr := ErrInternalUnexpected
apiErr.Detail = "Failed to get model usage data"
apiErr.DebugInfo = err.Error()
RespondWithAPIError(w, apiErr)
return
}
// create a map of model id to usage time
Expand Down Expand Up @@ -86,15 +88,17 @@ func (h *ChatModelHandler) ChatModelByID(w http.ResponseWriter, r *http.Request)
ctx := r.Context()
id, err := strconv.Atoi(vars["id"])
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Invalid chat API ID"))
apiErr := ErrValidationInvalidInput("Invalid chat model ID")
apiErr.DebugInfo = err.Error()
RespondWithAPIError(w, apiErr)
return
}

ChatModel, err := h.db.ChatModelByID(ctx, int32(id))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("Error retrieving chat API: %s", err.Error())))
apiErr := ErrResourceNotFound("Chat model")
apiErr.DebugInfo = err.Error()
RespondWithAPIError(w, apiErr)
return
}

Expand All @@ -105,7 +109,10 @@ func (h *ChatModelHandler) ChatModelByID(w http.ResponseWriter, r *http.Request)
func (h *ChatModelHandler) CreateChatModel(w http.ResponseWriter, r *http.Request) {
userID, err := getUserID(r.Context())
if err != nil {
RespondWithErrorMessage(w, http.StatusUnauthorized, "Unauthorized", err)
apiErr := ErrAuthInvalidCredentials
apiErr.DebugInfo = err.Error()
RespondWithAPIError(w, apiErr)
return
}

var input struct {
Expand All @@ -120,8 +127,9 @@ func (h *ChatModelHandler) CreateChatModel(w http.ResponseWriter, r *http.Reques

err = json.NewDecoder(r.Body).Decode(&input)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Failed to parse request body"))
apiErr := ErrValidationInvalidInput("Failed to parse request body")
apiErr.DebugInfo = err.Error()
RespondWithAPIError(w, apiErr)
return
}

Expand All @@ -137,8 +145,10 @@ func (h *ChatModelHandler) CreateChatModel(w http.ResponseWriter, r *http.Reques
})

if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("Error creating chat API: %s", err.Error())))
apiErr := ErrInternalUnexpected
apiErr.Detail = "Failed to create chat model"
apiErr.DebugInfo = err.Error()
RespondWithAPIError(w, apiErr)
return
}

Expand All @@ -150,14 +160,18 @@ func (h *ChatModelHandler) UpdateChatModel(w http.ResponseWriter, r *http.Reques
vars := mux.Vars(r)
id, err := strconv.Atoi(vars["id"])
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Invalid chat API ID"))
apiErr := ErrValidationInvalidInput("Invalid chat model ID")
apiErr.DebugInfo = err.Error()
RespondWithAPIError(w, apiErr)
return
}

userID, err := getUserID(r.Context())
if err != nil {
RespondWithErrorMessage(w, http.StatusUnauthorized, "Unauthorized", err)
apiErr := ErrAuthInvalidCredentials
apiErr.DebugInfo = err.Error()
RespondWithAPIError(w, apiErr)
return
}

var input struct {
Expand All @@ -176,7 +190,9 @@ func (h *ChatModelHandler) UpdateChatModel(w http.ResponseWriter, r *http.Reques
}
err = json.NewDecoder(r.Body).Decode(&input)
if err != nil {
RespondWithErrorMessage(w, http.StatusInternalServerError, eris.Wrap(err, "Failed to parse request body").Error(), err)
apiErr := ErrValidationInvalidInput("Failed to parse request body")
apiErr.DebugInfo = err.Error()
RespondWithAPIError(w, apiErr)
return
}

Expand All @@ -198,7 +214,10 @@ func (h *ChatModelHandler) UpdateChatModel(w http.ResponseWriter, r *http.Reques
})

if err != nil {
RespondWithErrorMessage(w, http.StatusInternalServerError, eris.Wrap(err, "Error updating chat API").Error(), err)
apiErr := ErrInternalUnexpected
apiErr.Detail = "Failed to update chat model"
apiErr.DebugInfo = err.Error()
RespondWithAPIError(w, apiErr)
return
}

Expand All @@ -210,14 +229,18 @@ func (h *ChatModelHandler) DeleteChatModel(w http.ResponseWriter, r *http.Reques
vars := mux.Vars(r)
id, err := strconv.Atoi(vars["id"])
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Invalid chat API ID"))
apiErr := ErrValidationInvalidInput("Invalid chat model ID")
apiErr.DebugInfo = err.Error()
RespondWithAPIError(w, apiErr)
return
}

userID, err := getUserID(r.Context())
if err != nil {
RespondWithErrorMessage(w, http.StatusUnauthorized, "Unauthorized", err)
apiErr := ErrAuthInvalidCredentials
apiErr.DebugInfo = err.Error()
RespondWithAPIError(w, apiErr)
return
}

err = h.db.DeleteChatModel(r.Context(),
Expand All @@ -226,8 +249,10 @@ func (h *ChatModelHandler) DeleteChatModel(w http.ResponseWriter, r *http.Reques
UserID: userID,
})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("Error deleting chat API: %s", err.Error())))
apiErr := ErrInternalUnexpected
apiErr.Detail = "Failed to delete chat model"
apiErr.DebugInfo = err.Error()
RespondWithAPIError(w, apiErr)
return
}

Expand All @@ -237,8 +262,10 @@ func (h *ChatModelHandler) DeleteChatModel(w http.ResponseWriter, r *http.Reques
func (h *ChatModelHandler) GetDefaultChatModel(w http.ResponseWriter, r *http.Request) {
ChatModel, err := h.db.GetDefaultChatModel(r.Context())
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("Error retrieving default chat API: %s", err.Error())))
apiErr := ErrInternalUnexpected
apiErr.Detail = "Failed to retrieve default chat model"
apiErr.DebugInfo = err.Error()
RespondWithAPIError(w, apiErr)
return
}
w.WriteHeader(http.StatusOK)
Expand Down
Loading
Loading