From 3155b0884b773a83a273dfd7a4ec4f98f865783b Mon Sep 17 00:00:00 2001 From: MrBounty Date: Thu, 8 Aug 2024 11:36:01 +0200 Subject: [PATCH] Added better error code handeling and a file to save unknow error code --- Logger.go | 46 ++++++++++++++++++++++++++++++++++++++++ RequestAnthropic.go | 14 ++++++------ RequestCustomEndpoint.go | 1 + RequestDeepseek.go | 16 +++++++++++++- RequestFirework.go | 4 +--- RequestGoogle.go | 3 +-- RequestGroq.go | 16 +++++++++++++- RequestMistral.go | 1 + RequestNim.go | 1 + RequestOpenai.go | 6 ++++-- RequestPerplexity.go | 1 + RequestTogetherai.go | 20 ++++++++++++++++- main.go | 1 + 13 files changed, 114 insertions(+), 16 deletions(-) create mode 100644 Logger.go diff --git a/Logger.go b/Logger.go new file mode 100644 index 0000000..62ba25d --- /dev/null +++ b/Logger.go @@ -0,0 +1,46 @@ +package main + +import ( + "log" + "net/http" + "os" + + "github.com/gofiber/fiber/v2" +) + +var logMissingErrorCode *log.Logger + +func init() { + logFile, err := os.OpenFile("app.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + log.Fatal(err) + } + logMissingErrorCode = log.New(logFile, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile) +} + +func MissingErrorCodeLogsHandler(c *fiber.Ctx) error { + if !IsUserAdmin(c) { + return c.SendString("That's for admin, how did you manage to come here ?") + } + + // Read the log file + data, err := os.ReadFile("app.log") + if err != nil { + return c.SendStatus(http.StatusInternalServerError) + } + + // Send the log file contents as the response + return c.SendString(string(data)) +} + +func IsUserAdmin(c *fiber.Ctx) bool { + var user User + err := edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).QuerySingle(edgeCtx, ` + SELECT global currentUser { id } LIMIT 1 + `, &user) + if err != nil { + return false + } + + return user.ID.String() == "8185c6f0-1d8d-11ef-a7d1-cfc50efb05ff" +} diff --git a/RequestAnthropic.go b/RequestAnthropic.go index a2fda05..6433023 100644 --- a/RequestAnthropic.go +++ b/RequestAnthropic.go @@ -38,15 +38,16 @@ type AnthropicUsage struct { var AnthropicErrorCodes map[string]string -// TODO Update func init() { AnthropicErrorCodes = make(map[string]string) + AnthropicErrorCodes["400"] = "Invalid Request - Please contact the support." AnthropicErrorCodes["401"] = "Invalid Authentication - Ensure that the API key is still valid." - AnthropicErrorCodes["403"] = "Accessing the API from an unsupported country, region, or territory." - AnthropicErrorCodes["429"] = "Rate limit reached for requests - You are sending requests too quickly." - AnthropicErrorCodes["429"] = "You have run out of credits or hit your maximum monthly spend - Buy more credits or learn how to increase your limits." - AnthropicErrorCodes["500"] = "Issue on OpenAI servers - Retry your request after a brief wait and contact OpenAI if the issue persists. Check the status page https://status.openai.com/." - AnthropicErrorCodes["503"] = "OpenAI servers are experiencing high traffic - Please retry your requests after a brief wait." + AnthropicErrorCodes["403"] = "Current API key does not have permission to use the specified resource." + AnthropicErrorCodes["404"] = "The requested resource was not found." + AnthropicErrorCodes["413"] = "Request exceeds the maximum allowed number of bytes." + AnthropicErrorCodes["429"] = "Your account has hit a rate limit." + AnthropicErrorCodes["500"] = "An unexpected error has occurred internal to Anthropic’s systems." + AnthropicErrorCodes["529"] = "Anthropic’s server is temporarily overloaded." } func TestAnthropicKey(apiKey string) bool { @@ -195,6 +196,7 @@ func RequestAnthropic(c *fiber.Ctx, llm LLM, messages []Message) string { addUsage(c, inputCost, outputCost, chatCompletionResponse.Usage.InputTokens, chatCompletionResponse.Usage.OutputTokens, model) if len(chatCompletionResponse.Content) == 0 { + logMissingErrorCode.Println("Anthropic -", resp.Status, "-", string(body)) return "JADE internal error: 01-03-0007. Please contact the support." } diff --git a/RequestCustomEndpoint.go b/RequestCustomEndpoint.go index 3af982b..cfc3a5a 100644 --- a/RequestCustomEndpoint.go +++ b/RequestCustomEndpoint.go @@ -57,6 +57,7 @@ func RequestHuggingface(c *fiber.Ctx, llm LLM, messages []Message) string { var chatCompletionResponse OpenaiChatCompletionResponse err = json.Unmarshal(body, &chatCompletionResponse) if err != nil { + logMissingErrorCode.Println("Custom -", resp.Status, "-", string(body)) return "JADE internal error: 10-01-0005. Please contact the support." } diff --git a/RequestDeepseek.go b/RequestDeepseek.go index 254d36b..bf92413 100644 --- a/RequestDeepseek.go +++ b/RequestDeepseek.go @@ -10,6 +10,19 @@ import ( "github.com/gofiber/fiber/v2" ) +var DeepseekErrorCodes map[string]string + +func init() { + DeepseekErrorCodes = make(map[string]string) + DeepseekErrorCodes["400"] = "Invalid Request - Please contact the support." + DeepseekErrorCodes["401"] = "Invalid Authentication - Ensure that the API key is still valid." + DeepseekErrorCodes["402"] = "You have run out of balance. Please check your account's balance." + DeepseekErrorCodes["422"] = "Request exceeds the maximum allowed number of bytes." + DeepseekErrorCodes["429"] = "Your account has hit a rate limit. You are sending requests too quickly." + DeepseekErrorCodes["500"] = "An unexpected error has occurred internal to Deepseek’s systems." + DeepseekErrorCodes["503"] = "Deepseek’s server is temporarily overloaded." +} + func TestDeepseekKey(apiKey string) bool { url := "https://api.deepseek.com/chat/completions" @@ -120,7 +133,7 @@ func RequestDeepseek(c *fiber.Ctx, llm LLM, messages []Message) string { return "JADE internal error: 08-01-0004. Please contact the support." } - for key, value := range OpenaiErrorCodes { + for key, value := range DeepseekErrorCodes { if strings.Contains(resp.Status, key) { return value } @@ -150,6 +163,7 @@ func RequestDeepseek(c *fiber.Ctx, llm LLM, messages []Message) string { addUsage(c, inputCost, outputCost, chatCompletionResponse.Usage.PromptTokens, chatCompletionResponse.Usage.CompletionTokens, model) if len(chatCompletionResponse.Choices) == 0 { + logMissingErrorCode.Println("Deepseek -", resp.Status, "-", string(body)) return "JADE internal error: 08-03-0007. Please contact the support." } diff --git a/RequestFirework.go b/RequestFirework.go index 8196979..93fb89b 100644 --- a/RequestFirework.go +++ b/RequestFirework.go @@ -3,7 +3,6 @@ package main import ( "bytes" "encoding/json" - "fmt" "io" "net/http" "strings" @@ -149,8 +148,7 @@ func RequestFirework(c *fiber.Ctx, llm LLM, messages []Message) string { addUsage(c, inputCost, outputCost, chatCompletionResponse.Usage.PromptTokens, chatCompletionResponse.Usage.CompletionTokens, model) if len(chatCompletionResponse.Choices) == 0 { - fmt.Println(resp.Status) - fmt.Println("JADE internal error: 09-03-0007 - ", string(body)) + logMissingErrorCode.Println("Fireworks -", resp.Status, "-", string(body)) return "JADE internal error: 09-03-0007. Please contact the support." } diff --git a/RequestGoogle.go b/RequestGoogle.go index 7fb8cce..e4f7606 100644 --- a/RequestGoogle.go +++ b/RequestGoogle.go @@ -1,5 +1,3 @@ -// Yes Google do not work because I am in Europe and I can't get an API key... -// I can't dev, I will try using a VPN I guess at least so the features is available for people outside of Europe package main import ( @@ -216,6 +214,7 @@ func RequestGoogle(c *fiber.Ctx, llm LLM, messages []Message) string { addUsage(c, inputCost, outputCost, chatCompletionResponse.UsageMetadata.PromptTokenCount, chatCompletionResponse.UsageMetadata.CandidatesTokenCount, model) if len(chatCompletionResponse.Candidates) == 0 { + logMissingErrorCode.Println("Google -", resp.Status, "-", string(body)) return "JADE internal error: 03-03-0007. Please contact the support." } diff --git a/RequestGroq.go b/RequestGroq.go index 08ab727..1c7c909 100644 --- a/RequestGroq.go +++ b/RequestGroq.go @@ -10,6 +10,19 @@ import ( "github.com/gofiber/fiber/v2" ) +var GroqErrorCodes map[string]string + +func init() { + GroqErrorCodes = make(map[string]string) + GroqErrorCodes["400"] = "Invalid Request - Please contact the support." + GroqErrorCodes["404"] = "The requested resource could not be found. Most likely the model is not available anymore." + GroqErrorCodes["422"] = "The request was well-formed but could not be followed due to semantic errors. Verify the data provided for correctness and completeness." + GroqErrorCodes["429"] = "Too many requests were sent in a given timeframe. Implement request throttling and respect rate limits." + GroqErrorCodes["500"] = "A generic error occurred on Gorq's server. Try the request again later or contact support if the issue persists." + GroqErrorCodes["502"] = "The server received an invalid response from an upstream server. This may be a temporary issue; retrying the request might resolve it." + GroqErrorCodes["503"] = "The server is not ready to handle the request, often due to maintenance or overload. Wait before retrying the request." +} + func TestGroqKey(apiKey string) bool { url := "https://api.groq.com/openai/v1/chat/completions" @@ -118,7 +131,7 @@ func RequestGroq(c *fiber.Ctx, llm LLM, messages []Message) string { return "JADE internal error: 04-01-0004. Please contact the support." } - for key, value := range OpenaiErrorCodes { + for key, value := range GroqErrorCodes { if strings.Contains(resp.Status, key) { return value } @@ -148,6 +161,7 @@ func RequestGroq(c *fiber.Ctx, llm LLM, messages []Message) string { addUsage(c, inputCost, outputCost, chatCompletionResponse.Usage.PromptTokens, chatCompletionResponse.Usage.CompletionTokens, model) if len(chatCompletionResponse.Choices) == 0 { + logMissingErrorCode.Println("Groq -", resp.Status, "-", string(body)) return "JADE internal error: 04-03-0007. Please contact the support." } diff --git a/RequestMistral.go b/RequestMistral.go index 19cdc31..b76f285 100644 --- a/RequestMistral.go +++ b/RequestMistral.go @@ -148,6 +148,7 @@ func RequestMistral(c *fiber.Ctx, llm LLM, messages []Message) string { addUsage(c, inputCost, outputCost, chatCompletionResponse.Usage.PromptTokens, chatCompletionResponse.Usage.CompletionTokens, model) if len(chatCompletionResponse.Choices) == 0 { + logMissingErrorCode.Println("Mistral -", resp.Status, "-", string(body)) return "JADE internal error: 02-03-0007. Please contact the support." } diff --git a/RequestNim.go b/RequestNim.go index b4f3e65..f78e3f0 100644 --- a/RequestNim.go +++ b/RequestNim.go @@ -149,6 +149,7 @@ func RequestNim(c *fiber.Ctx, llm LLM, messages []Message) string { addUsage(c, inputCost, outputCost, chatCompletionResponse.Usage.PromptTokens, chatCompletionResponse.Usage.CompletionTokens, model) if len(chatCompletionResponse.Choices) == 0 { + logMissingErrorCode.Println("Nim -", resp.Status, "-", string(body)) return "JADE internal error: 05-03-0007. Please contact the support." } diff --git a/RequestOpenai.go b/RequestOpenai.go index f4dd758..cd03afd 100644 --- a/RequestOpenai.go +++ b/RequestOpenai.go @@ -42,10 +42,11 @@ var OpenaiErrorCodes map[string]string func init() { OpenaiErrorCodes = make(map[string]string) + OpenaiErrorCodes["400"] = "Invalid Request - Please contact the support." OpenaiErrorCodes["401"] = "Invalid Authentication - Ensure that the API key is still valid." OpenaiErrorCodes["403"] = "Accessing the API from an unsupported country, region, or territory." - OpenaiErrorCodes["429"] = "Rate limit reached for requests - You are sending requests too quickly." - OpenaiErrorCodes["429"] = "You have run out of credits or hit your maximum monthly spend - Buy more credits or learn how to increase your limits." + OpenaiErrorCodes["404"] = "Model not found." + OpenaiErrorCodes["429"] = "Rate limit reached for requests - You are sending requests too quickly OR You have run out of credits or hit your maximum monthly spend - Buy more credits or learn how to increase your limits." OpenaiErrorCodes["500"] = "Issue on Provider servers - Retry your request after a brief wait and contact the provider if the issue persists." OpenaiErrorCodes["503"] = "Servers are experiencing high traffic - Please retry your requests after a brief wait." } @@ -188,6 +189,7 @@ func RequestOpenai(c *fiber.Ctx, llm LLM, messages []Message) string { addUsage(c, inputCost, outputCost, chatCompletionResponse.Usage.PromptTokens, chatCompletionResponse.Usage.CompletionTokens, model) if len(chatCompletionResponse.Choices) == 0 { + logMissingErrorCode.Println("Openai -", resp.Status, "-", string(body)) return "JADE internal error: 00-03-0007. Please contact the support." } diff --git a/RequestPerplexity.go b/RequestPerplexity.go index 68c5e09..f6dfbd3 100644 --- a/RequestPerplexity.go +++ b/RequestPerplexity.go @@ -148,6 +148,7 @@ func RequestPerplexity(c *fiber.Ctx, llm LLM, messages []Message) string { addUsage(c, inputCost, outputCost, chatCompletionResponse.Usage.PromptTokens, chatCompletionResponse.Usage.CompletionTokens, model) if len(chatCompletionResponse.Choices) == 0 { + logMissingErrorCode.Println("Perplexity -", resp.Status, "-", string(body)) return "JADE internal error: 06-03-0007. Please contact the support." } diff --git a/RequestTogetherai.go b/RequestTogetherai.go index d534493..27987c1 100644 --- a/RequestTogetherai.go +++ b/RequestTogetherai.go @@ -10,6 +10,23 @@ import ( "github.com/gofiber/fiber/v2" ) +var TogetherErrorCodes map[string]string + +func init() { + TogetherErrorCodes = make(map[string]string) + TogetherErrorCodes["400"] = "Invalid Request - Please contact the support." + TogetherErrorCodes["401"] = "Invalid Authentication - Ensure that the API key is still valid." + TogetherErrorCodes["403"] = "Set max_tokens to a lower number. Or leave it empty for using max value." + TogetherErrorCodes["404"] = "Model not found." + TogetherErrorCodes["429"] = "Rate limit reached for requests - You are sending requests too quickly." + TogetherErrorCodes["500"] = "Issue on Together AI servers - Retry your request after a brief wait and contact Together AI if the issue persists." + TogetherErrorCodes["503"] = "Servers are experiencing high traffic - Please retry your requests after a brief wait." + TogetherErrorCodes["504"] = "Servers are experiencing high traffic - Please retry your requests after a brief wait." + TogetherErrorCodes["520"] = "An unexpected error has occurred internal to Together’s systems." + TogetherErrorCodes["524"] = "An unexpected error has occurred internal to Together’s systems." + TogetherErrorCodes["529"] = "An unexpected error has occurred internal to Together’s systems." +} + type TogetherChatCompletionResponse struct { ID string `json:"id"` Object string `json:"object"` @@ -133,7 +150,7 @@ func RequestTogether(c *fiber.Ctx, llm LLM, messages []Message) string { return "JADE internal error: 07-01-0004. Please contact the support." } - for key, value := range OpenaiErrorCodes { + for key, value := range TogetherErrorCodes { if strings.Contains(resp.Status, key) { return value } @@ -163,6 +180,7 @@ func RequestTogether(c *fiber.Ctx, llm LLM, messages []Message) string { addUsage(c, inputCost, outputCost, chatCompletionResponse.Usage.PromptTokens, chatCompletionResponse.Usage.CompletionTokens, model) if len(chatCompletionResponse.Choices) == 0 { + logMissingErrorCode.Println("Together -", resp.Status, "-", string(body)) return "JADE internal error: 07-03-0007. Please contact the support." } diff --git a/main.go b/main.go index 9dcee5d..5adfd1a 100644 --- a/main.go +++ b/main.go @@ -120,6 +120,7 @@ func main() { }) app.Get("/ws", websocket.New(handleWebSocket)) + app.Get("/missingErrorLogs", MissingErrorCodeLogsHandler) // Start server if err := app.Listen(":8080"); err != nil {