Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
CreatureChat
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Jobs
Commits
Open sidebar
Public
CreatureChat
Commits
b261c286
Commit
b261c286
authored
May 24, 2024
by
Jonathan Thomas
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve error messages for OpenAI and CreatureChat API
parent
ec7d966f
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
2 deletions
+52
-2
CHANGELOG.md
CHANGELOG.md
+4
-0
ChatGPTRequest.java
src/main/java/com/owlmaddie/chat/ChatGPTRequest.java
+48
-2
No files found.
CHANGELOG.md
View file @
b261c286
...
...
@@ -9,6 +9,10 @@ All notable changes to **CreatureChat** are documented in this file. The format
### Added
-
New automated deployments for Modrinth and CurseForge (GitLab CI Pipeline)
### Fixed
-
Parse OpenAI JSON error messages, to display a more readable error message
-
Remove quotes from CreatureChat API error messages
## [1.0.4] - 2024-05-15
### Added
...
...
src/main/java/com/owlmaddie/chat/ChatGPTRequest.java
View file @
b261c286
package
com
.
owlmaddie
.
chat
;
import
com.google.gson.Gson
;
import
com.google.gson.JsonSyntaxException
;
import
com.owlmaddie.commands.ConfigurationHandler
;
import
com.owlmaddie.json.ChatGPTResponse
;
import
com.owlmaddie.network.ServerPackets
;
...
...
@@ -63,6 +64,47 @@ public class ChatGPTRequest {
}
}
public
static
String
removeQuotes
(
String
str
)
{
if
(
str
!=
null
&&
str
.
length
()
>
1
&&
str
.
startsWith
(
"\""
)
&&
str
.
endsWith
(
"\""
))
{
return
str
.
substring
(
1
,
str
.
length
()
-
1
);
}
return
str
;
}
// Class to represent the error response structure
public
static
class
ErrorResponse
{
Error
error
;
static
class
Error
{
String
message
;
String
type
;
String
code
;
}
}
public
static
String
parseAndLogErrorResponse
(
String
errorResponse
)
{
try
{
Gson
gson
=
new
Gson
();
ErrorResponse
response
=
gson
.
fromJson
(
errorResponse
,
ErrorResponse
.
class
);
if
(
response
.
error
!=
null
)
{
LOGGER
.
error
(
"Error Message: "
+
response
.
error
.
message
);
LOGGER
.
error
(
"Error Type: "
+
response
.
error
.
type
);
LOGGER
.
error
(
"Error Code: "
+
response
.
error
.
code
);
return
response
.
error
.
message
;
}
else
{
LOGGER
.
error
(
"Unknown error response: "
+
errorResponse
);
return
"Unknown"
;
}
}
catch
(
JsonSyntaxException
e
)
{
LOGGER
.
warn
(
"Failed to parse error response as JSON, falling back to plain text"
);
LOGGER
.
error
(
"Error response: "
+
errorResponse
);
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"Failed to parse error response"
,
e
);
}
return
removeQuotes
(
errorResponse
);
}
// This method should be called in an appropriate context where ResourceManager is available
public
static
String
loadPromptFromResource
(
ResourceManager
resourceManager
,
String
filePath
)
{
Identifier
fileIdentifier
=
new
Identifier
(
"creaturechat"
,
filePath
);
...
...
@@ -173,8 +215,12 @@ public class ChatGPTRequest {
while
((
errorLine
=
errorReader
.
readLine
())
!=
null
)
{
errorResponse
.
append
(
errorLine
.
trim
());
}
LOGGER
.
error
(
"Error response from API: "
+
errorResponse
);
lastErrorMessage
=
errorResponse
.
toString
();
// Parse and log the error response using Gson
String
cleanError
=
parseAndLogErrorResponse
(
errorResponse
.
toString
());
lastErrorMessage
=
cleanError
;
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"Failed to read error response"
,
e
);
}
return
null
;
}
else
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment