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
6 changes: 5 additions & 1 deletion api/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ vet: fmt
.PHONY: vet

build: vet
go build
go build -v ./...
.PHONY: build

test:
go test -v ./...
.PHONY: test

install:
go install github.com/air-verse/air@latest
go mod tidy
Expand Down
147 changes: 0 additions & 147 deletions api/artifact_instruction.txt

This file was deleted.

83 changes: 46 additions & 37 deletions api/dto/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"encoding/json"
"errors"
"fmt"
"log"
"log/slog"
"net/http"
"sort"
"strings"
Expand Down Expand Up @@ -225,7 +225,7 @@ var (

// Deprecated: use ErrClaudeResponseFailed instead.
ErrClaudeResponseFaild = ErrClaudeResponseFailed
ErrOpenAIStreamFailed = APIError{
ErrOpenAIStreamFailed = APIError{
HTTPCode: http.StatusInternalServerError,
Code: ErrModel + "_006",
Message: "Failed to stream OpenAI response",
Expand Down Expand Up @@ -297,11 +297,11 @@ func RespondWithAPIError(w http.ResponseWriter, err APIError) {
}

if err.DebugInfo != "" {
log.Printf("Error [%s]: %s - %s", err.Code, err.Message, err.DebugInfo)
slog.Error("api error", "code", err.Code, "message", err.Message, "debug", err.DebugInfo)
}

if err := json.NewEncoder(w).Encode(response); err != nil {
log.Printf("Failed to write error response: %v", err)
slog.Info("Failed to write error response", "error", err)
}
}

Expand Down Expand Up @@ -359,7 +359,7 @@ func MapDatabaseError(err error) error {
}
}

log.Printf("Unhandled database error: %v", err)
slog.Info("Unhandled database error", "error", err)

dbErr := ErrDatabaseQuery
dbErr.DebugInfo = err.Error()
Expand Down Expand Up @@ -401,6 +401,15 @@ func WrapError(err error, detail string) APIError {
apiErr.Detail = detail
}
}
case *APIError:
apiErr = *e
if detail != "" {
if apiErr.Detail != "" {
apiErr.Detail = fmt.Sprintf("%s: %s", detail, apiErr.Detail)
} else {
apiErr.Detail = detail
}
}
default:
apiErr = ErrInternalUnexpected
apiErr.Detail = detail
Expand All @@ -421,39 +430,39 @@ func IsErrorCode(err error, code string) bool {
// --- Error catalog (for documentation) ---

var ErrorCatalog = map[string]APIError{
ErrAuthInvalidCredentials.Code: ErrAuthInvalidCredentials,
ErrAuthExpiredToken.Code: ErrAuthExpiredToken,
ErrAuthAdminRequired.Code: ErrAuthAdminRequired,
ErrAuthInvalidEmailOrPassword.Code: ErrAuthInvalidEmailOrPassword,
ErrAuthAccessDenied.Code: ErrAuthAccessDenied,
ErrResourceNotFoundGeneric.Code: ErrResourceNotFoundGeneric,
ErrResourceAlreadyExistsGeneric.Code: ErrResourceAlreadyExistsGeneric,
ErrTooManyRequests.Code: ErrTooManyRequests,
ErrChatSessionNotFound.Code: ErrChatSessionNotFound,
ErrChatFileNotFound.Code: ErrChatFileNotFound,
ErrChatModelNotFound.Code: ErrChatModelNotFound,
ErrChatMessageNotFound.Code: ErrChatMessageNotFound,
ErrAuthInvalidCredentials.Code: ErrAuthInvalidCredentials,
ErrAuthExpiredToken.Code: ErrAuthExpiredToken,
ErrAuthAdminRequired.Code: ErrAuthAdminRequired,
ErrAuthInvalidEmailOrPassword.Code: ErrAuthInvalidEmailOrPassword,
ErrAuthAccessDenied.Code: ErrAuthAccessDenied,
ErrResourceNotFoundGeneric.Code: ErrResourceNotFoundGeneric,
ErrResourceAlreadyExistsGeneric.Code: ErrResourceAlreadyExistsGeneric,
ErrTooManyRequests.Code: ErrTooManyRequests,
ErrChatSessionNotFound.Code: ErrChatSessionNotFound,
ErrChatFileNotFound.Code: ErrChatFileNotFound,
ErrChatModelNotFound.Code: ErrChatModelNotFound,
ErrChatMessageNotFound.Code: ErrChatMessageNotFound,
ErrValidationInvalidInputGeneric.Code: ErrValidationInvalidInputGeneric,
ErrChatFileTooLarge.Code: ErrChatFileTooLarge,
ErrChatFileInvalidType.Code: ErrChatFileInvalidType,
ErrChatSessionInvalid.Code: ErrChatSessionInvalid,
ErrDatabaseQuery.Code: ErrDatabaseQuery,
ErrDatabaseConnection.Code: ErrDatabaseConnection,
ErrDatabaseForeignKey.Code: ErrDatabaseForeignKey,
ErrExternalTimeout.Code: ErrExternalTimeout,
ErrExternalUnavailable.Code: ErrExternalUnavailable,
ErrInternalUnexpected.Code: ErrInternalUnexpected,
ErrChatStreamFailed.Code: ErrChatStreamFailed,
ErrChatRequestFailed.Code: ErrChatRequestFailed,
ErrSystemMessageError.Code: ErrSystemMessageError,
ErrClaudeStreamFailed.Code: ErrClaudeStreamFailed,
ErrClaudeRequestFailed.Code: ErrClaudeRequestFailed,
ErrClaudeInvalidResponse.Code: ErrClaudeInvalidResponse,
ErrClaudeResponseFailed.Code: ErrClaudeResponseFailed,
ErrOpenAIStreamFailed.Code: ErrOpenAIStreamFailed,
ErrOpenAIRequestFailed.Code: ErrOpenAIRequestFailed,
ErrOpenAIInvalidResponse.Code: ErrOpenAIInvalidResponse,
ErrOpenAIConfigFailed.Code: ErrOpenAIConfigFailed,
ErrChatFileTooLarge.Code: ErrChatFileTooLarge,
ErrChatFileInvalidType.Code: ErrChatFileInvalidType,
ErrChatSessionInvalid.Code: ErrChatSessionInvalid,
ErrDatabaseQuery.Code: ErrDatabaseQuery,
ErrDatabaseConnection.Code: ErrDatabaseConnection,
ErrDatabaseForeignKey.Code: ErrDatabaseForeignKey,
ErrExternalTimeout.Code: ErrExternalTimeout,
ErrExternalUnavailable.Code: ErrExternalUnavailable,
ErrInternalUnexpected.Code: ErrInternalUnexpected,
ErrChatStreamFailed.Code: ErrChatStreamFailed,
ErrChatRequestFailed.Code: ErrChatRequestFailed,
ErrSystemMessageError.Code: ErrSystemMessageError,
ErrClaudeStreamFailed.Code: ErrClaudeStreamFailed,
ErrClaudeRequestFailed.Code: ErrClaudeRequestFailed,
ErrClaudeInvalidResponse.Code: ErrClaudeInvalidResponse,
ErrClaudeResponseFailed.Code: ErrClaudeResponseFailed,
ErrOpenAIStreamFailed.Code: ErrOpenAIStreamFailed,
ErrOpenAIRequestFailed.Code: ErrOpenAIRequestFailed,
ErrOpenAIInvalidResponse.Code: ErrOpenAIInvalidResponse,
ErrOpenAIConfigFailed.Code: ErrOpenAIConfigFailed,
}

// ErrorCatalogHandler serves the error catalog as JSON.
Expand Down
4 changes: 2 additions & 2 deletions api/handler/auth_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"encoding/json"
"net/http"

log "github.com/sirupsen/logrus"
"github.com/swuecho/chat_backend/auth"
"github.com/swuecho/chat_backend/dto"
"github.com/swuecho/chat_backend/sqlc_queries"
"log/slog"
)

// --- Request types ---
Expand Down Expand Up @@ -39,7 +39,7 @@ func (h *AuthUserHandler) ResetPasswordHandler(w http.ResponseWriter, r *http.Re

tempPassword, err := auth.GenerateRandomPassword()
if err != nil {
log.WithError(err).Error("Failed to generate temporary password")
slog.Error("Failed to generate temporary password", "error", err)
dto.RespondWithAPIError(w, dto.ErrInternalUnexpected.WithMessage("Failed to generate temporary password").WithDebugInfo(err.Error()))
return
}
Expand Down
Loading
Loading