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
68 changes: 67 additions & 1 deletion CodenameOne/src/com/codename1/ai/OpenAiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,23 @@ protected void handleErrorResponseCode(int code, String message) {

@Override
protected void postResponse() {
int code;
try {
code = getResponseCode();
} catch (Throwable t) {
code = 0;
}
final byte[] data = getResponseData();
final LlmException httpErr = httpErrorOrNull(code, data);
if (httpErr != null) {
Display.getInstance().callSerially(new Runnable() {
@Override
public void run() {
result.error(httpErr);
}
});
return;
}
try {
Map root = JSONParser.parseJSON(data);
final ChatResponse cr2 = OpenAiSseDecoder.parseNonStreaming(root);
Expand Down Expand Up @@ -222,8 +238,25 @@ protected void handleErrorResponseCode(int code, String message) {

@Override
protected void postResponse() {
int code;
try {
Map root = JSONParser.parseJSON(getResponseData());
code = getResponseCode();
} catch (Throwable t) {
code = 0;
}
final byte[] data = getResponseData();
final LlmException httpErr = httpErrorOrNull(code, data);
if (httpErr != null) {
Display.getInstance().callSerially(new Runnable() {
@Override
public void run() {
result.error(httpErr);
}
});
return;
}
try {
Map root = JSONParser.parseJSON(data);
List<Object> dataArr = JSONParser.asList(root.get("data"));
List<Embedding> out = new ArrayList<Embedding>(dataArr == null ? 0 : dataArr.size());
if (dataArr != null) {
Expand Down Expand Up @@ -276,6 +309,39 @@ public void run() {
return result;
}

/// Detects an error response in `postResponse()`. With
/// `setReadResponseForErrors(true)` the framework routes HTTP 4xx/5xx
/// through `postResponse()` (not `handleException`), so without this
/// check an OpenAI `{"error":...}` envelope would be parsed as an empty
/// success. Returns the typed [LlmException] to surface, or `null` when
/// the response is a normal success that should be parsed as usual.
private static LlmException httpErrorOrNull(int code, byte[] data) {
String bodyText;
try {
bodyText = data == null ? "" : new String(data, "UTF-8");
} catch (UnsupportedEncodingException uee) {
// UTF-8 is universally available; this branch is theoretical.
bodyText = "";
}
boolean envelopeError = false;
if (code < 400 && bodyText.length() > 0) {
// Some OpenAI-compatible servers answer 200 with an
// {"error":...} body; honour that shape too.
try {
Map root = JSONParser.parseJSON(bodyText);
envelopeError = root != null && root.get("error") != null;
} catch (IOException ignored) {
// Not JSON -> treat as a normal (non-error) body.
} catch (RuntimeException ignored) {
// Malformed structure -> treat as a non-error body.
}
}
if (code >= 400 || envelopeError) {
return OpenAiSseDecoder.mapErrorStatic(code, bodyText);
}
return null;
}

private void configureRequest(ConnectionRequest cr, String pathOrNull) {
if (pathOrNull != null) {
cr.setUrl(resolveUrl(pathOrNull));
Expand Down
10 changes: 7 additions & 3 deletions CodenameOne/src/com/codename1/nfc/NdefRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,13 @@ public String getTextPayload() {
/// URI (re-expanding the leading prefix code). Returns `null` if the
/// record is not a recognised URI record.
public String getUriPayload() {
// Absolute-URI records carry the URI in the type field and may have
// an empty payload, so this branch must run before the payload-length
// guard below (which only the well-known URI form needs -- its prefix
// code lives in payload[0]).
if (tnf == TNF_ABSOLUTE_URI) {
return fromUtf8(type, 0, type.length);
}
if (payload.length < 1) {
return null;
}
Expand All @@ -265,9 +272,6 @@ public String getUriPayload() {
: "";
return p + fromUtf8(payload, 1, payload.length - 1);
}
if (tnf == TNF_ABSOLUTE_URI) {
return fromUtf8(type, 0, type.length);
}
return null;
}

Expand Down
Loading
Loading