Skip to content

Commit

Permalink
add status code to the exception
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeloffner committed Dec 2, 2024
1 parent 3bfc81d commit 1ce49b8
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 27 deletions.
25 changes: 23 additions & 2 deletions core/src/main/java/lucee/runtime/ai/AIUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
import java.util.Collections;
import java.util.List;

import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;

import lucee.commons.io.res.ContentType;
import lucee.commons.lang.StringUtil;
import lucee.commons.net.http.HTTPResponse;
import lucee.runtime.PageContext;
import lucee.runtime.engine.ThreadLocalPageContext;
import lucee.runtime.exp.ApplicationException;
Expand All @@ -23,7 +27,7 @@ public class AIUtil {
private static final Key CREATED_AT = KeyImpl.init("createdAt");
private static final Key STATUS_DETAILS = KeyImpl.init("statusDetails");

public static PageException toException(AIEngine engine, String msg, String type, String code) {
public static PageException toException(AIEngine engine, String msg, String type, String code, int statusCode) {
String appendix = "";
if ("model_not_found".equals(code) || msg.indexOf("models") != -1) {
try {
Expand All @@ -33,7 +37,7 @@ public static PageException toException(AIEngine engine, String msg, String type
}
}

PageException ae = new ApplicationException(msg + appendix, "type:" + type + ";code:" + code);
PageException ae = new ApplicationException(msg + appendix + "; type:" + type + "; code:" + code + "; status-code:" + statusCode);
ae.setErrorCode(code);
return ae;
}
Expand Down Expand Up @@ -146,4 +150,21 @@ public static String createJsonContentType(String charset) {
if (StringUtil.isEmpty(charset, true)) return "application/json";
return "application/json; charset=" + charset.trim();
}

public static int getStatusCode(HTTPResponse response) {
if (response != null) {
return response.getStatusCode();
}
return -1;
}

public static int getStatusCode(CloseableHttpResponse response) {
if (response != null) {
StatusLine sl = response.getStatusLine();
if (sl != null) {
return sl.getStatusCode();
}
}
return -1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import lucee.runtime.ai.AIEngineSupport;
import lucee.runtime.ai.AIModel;
import lucee.runtime.ai.AISession;
import lucee.runtime.ai.AIUtil;
import lucee.runtime.exp.ApplicationException;
import lucee.runtime.exp.PageException;
import lucee.runtime.net.proxy.ProxyData;
Expand Down Expand Up @@ -125,14 +124,6 @@ public List<AIModel> getModels() {

}

private void throwIfError(Struct raw) throws PageException {
Struct err = Caster.toStruct(raw.get(KeyConstants._error, null), null);
if (err != null) {
throw AIUtil.toException(this, Caster.toString(err.get(KeyConstants._message)), Caster.toString(err.get(KeyConstants._type, null), null),
Caster.toString(err.get(KeyConstants._code, null), null));
}
}

@Override
public int getConversationSizeLimit() {
// TODO Auto-generated method stub
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public Response inquiry(String message, AIResponseListener listener) throws Page
Struct err = Caster.toStruct(raw.get(KeyConstants._error, null), null);
if (err != null) {
throw AIUtil.toException(this.getEngine(), Caster.toString(err.get(KeyConstants._message)), Caster.toString(err.get(KeyConstants._type, null), null),
Caster.toString(err.get(KeyConstants._code, null), null));
Caster.toString(err.get(KeyConstants._code, null), null), AIUtil.getStatusCode(response));
}

// Create response object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public List<AIModel> getModels() throws PageException {
Struct err = Caster.toStruct(raw.get(KeyConstants._error, null), null);
if (err != null) {
throw AIUtil.toException(this, Caster.toString(err.get(KeyConstants._message)), Caster.toString(err.get(KeyConstants._type, null), null),
Caster.toString(err.get(KeyConstants._code, null), null));
Caster.toString(err.get(KeyConstants._code, null), null), AIUtil.getStatusCode(rsp));
}
Array data = Caster.toArray(raw.get("models"));
Iterator<Object> it = data.valueIterator();
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/lucee/runtime/ai/google/GeminiSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ else if ("application/json".equals(ct.getMimeType())) {
String msg = Caster.toString(err.get(KeyConstants._message, null), null);
String code = Caster.toString(err.get(KeyConstants._code, null), null);
String status = Caster.toString(err.get(KeyConstants._status, null), null);
if (!StringUtil.isEmpty(msg, true)) throw AIUtil.toException(getEngine(), msg, status, code);
if (!StringUtil.isEmpty(msg, true)) throw AIUtil.toException(getEngine(), msg, status, code, AIUtil.getStatusCode(rsp));
throw AIUtil.toException(getEngine(), getEngine().getLabel() + " did reposne with status code [" + rsp.getStatusCode() + "]", null,
Caster.toString(rsp.getStatusCode(), null));
Caster.toString(rsp.getStatusCode(), null), AIUtil.getStatusCode(rsp));
}

GeminiResponse response = new GeminiResponse(raw, cs);
Expand Down
18 changes: 9 additions & 9 deletions core/src/main/java/lucee/runtime/ai/openai/OpenAIEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public List<AIModel> getModels() throws PageException {
if (Util.isEmpty(cs, true)) cs = charset;
String str = rsp.getContentAsString(cs);
Struct raw = Caster.toStruct(new JSONExpressionInterpreter().interpret(null, str));
throwIfError(raw);
throwIfError(raw, AIUtil.getStatusCode(rsp));

List<AIModel> list = new ArrayList<>();
Object o = raw.get(KeyConstants._data, null);
Expand All @@ -258,11 +258,11 @@ else if (!CollectionUtil.hasKey(raw, KeyConstants._data)) {
}
}

private void throwIfError(Struct raw) throws PageException {
private void throwIfError(Struct raw, int statusCode) throws PageException {
Struct err = Caster.toStruct(raw.get(KeyConstants._error, null), null);
if (err != null) {
throw AIUtil.toException(this, Caster.toString(err.get(KeyConstants._message)), Caster.toString(err.get(KeyConstants._type, null), null),
Caster.toString(err.get(KeyConstants._code, null), null));
Caster.toString(err.get(KeyConstants._code, null), null), statusCode);
}
}

Expand Down Expand Up @@ -291,7 +291,7 @@ public Struct createFineTuningJob(String trainingFileId) throws PageException {
String responseString = EntityUtils.toString(responseEntity, charset);

Struct raw = Caster.toStruct(new JSONExpressionInterpreter().interpret(null, responseString));
throwIfError(raw);
throwIfError(raw, AIUtil.getStatusCode(response));
return raw;
}
}
Expand Down Expand Up @@ -342,7 +342,7 @@ public String uploadFile(Resource jsonl, String purpose) throws PageException {
*/

Struct raw = Caster.toStruct(new JSONExpressionInterpreter().interpret(null, responseString));
throwIfError(raw);
throwIfError(raw, AIUtil.getStatusCode(response));
return Caster.toString(raw.get(KeyConstants._id));

}
Expand Down Expand Up @@ -379,7 +379,7 @@ public List<AIFile> listFiles() throws PageException {
if ("application/json".equals(responseEntity.getContentType().getValue())) {
String responseString = EntityUtils.toString(responseEntity, charset);
Struct raw = Caster.toStruct(new JSONExpressionInterpreter().interpret(null, responseString));
throwIfError(raw);
throwIfError(raw, AIUtil.getStatusCode(response));
Array data = Caster.toArray(raw.get(KeyConstants._data));
Iterator<?> it = data.getIterator();
Struct sct;
Expand Down Expand Up @@ -436,7 +436,7 @@ public AIFile getFile(String id) throws PageException {
if ("application/json".equals(responseEntity.getContentType().getValue())) {
String responseString = EntityUtils.toString(responseEntity, charset);
Struct sct = Caster.toStruct(new JSONExpressionInterpreter().interpret(null, responseString));
throwIfError(sct);
throwIfError(sct, AIUtil.getStatusCode(response));
return new AIFileSupport(

Caster.toString(sct.get(KeyConstants._object)),
Expand Down Expand Up @@ -490,7 +490,7 @@ public InputStream getFileContent(String id) throws PageException {
if ("application/json".equals(responseEntity.getContentType().getValue())) {
String responseString = EntityUtils.toString(responseEntity, charset);
Struct sct = Caster.toStruct(new JSONExpressionInterpreter().interpret(null, responseString));
throwIfError(sct);
throwIfError(sct, AIUtil.getStatusCode(response));
}

// Return the InputStream, caller is responsible for closing it
Expand Down Expand Up @@ -520,7 +520,7 @@ public boolean deleteFile(String id) throws PageException {
if ("application/json".equals(responseEntity.getContentType().getValue())) {
String responseString = EntityUtils.toString(responseEntity, charset);
Struct sct = Caster.toStruct(new JSONExpressionInterpreter().interpret(null, responseString));
throwIfError(sct);
throwIfError(sct, AIUtil.getStatusCode(response));
return Caster.toBooleanValue(sct.get(KeyConstants._deleted));

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,9 @@ public Response inquiry(String message, AIResponseListener listener) throws Page

Struct err = Caster.toStruct(raw.get(KeyConstants._error, null), null);
if (err != null) {

throw AIUtil.toException(this.getEngine(), Caster.toString(err.get(KeyConstants._message)), Caster.toString(err.get(KeyConstants._type, null), null),
Caster.toString(err.get(KeyConstants._code, null), null));
Caster.toString(err.get(KeyConstants._code, null), null), response.getStatusLine().getStatusCode());
}

OpenAIResponse r = new OpenAIResponse(raw, cs);
Expand Down
2 changes: 1 addition & 1 deletion loader/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<project default="core" basedir="." name="Lucee"
xmlns:resolver="antlib:org.apache.maven.resolver.ant">

<property name="version" value="6.2.0.169-SNAPSHOT"/>
<property name="version" value="6.2.0.170-SNAPSHOT"/>

<taskdef uri="antlib:org.apache.maven.resolver.ant" resource="org/apache/maven/resolver/ant/antlib.xml">
<classpath>
Expand Down
2 changes: 1 addition & 1 deletion loader/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>org.lucee</groupId>
<artifactId>lucee</artifactId>
<version>6.2.0.169-SNAPSHOT</version>
<version>6.2.0.170-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Lucee Loader Build</name>
Expand Down

0 comments on commit 1ce49b8

Please sign in to comment.