Added better error code handeling and a file to save unknow error code

This commit is contained in:
Adrien Bouvais 2024-08-08 11:36:01 +02:00
parent 3bc0ade499
commit 3155b0884b
13 changed files with 114 additions and 16 deletions

46
Logger.go Normal file
View File

@ -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"
}

View File

@ -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 Anthropics systems."
AnthropicErrorCodes["529"] = "Anthropics 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."
}

View File

@ -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."
}

View File

@ -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 Deepseeks systems."
DeepseekErrorCodes["503"] = "Deepseeks 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."
}

View File

@ -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."
}

View File

@ -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."
}

View File

@ -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."
}

View File

@ -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."
}

View File

@ -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."
}

View File

@ -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."
}

View File

@ -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."
}

View File

@ -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 Togethers systems."
TogetherErrorCodes["524"] = "An unexpected error has occurred internal to Togethers systems."
TogetherErrorCodes["529"] = "An unexpected error has occurred internal to Togethers 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."
}

View File

@ -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 {