Skip to content

Commit 58d0588

Browse files
committed
better error handling
1 parent 9096e5e commit 58d0588

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

examples/server/public/completion.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ const paramDefaults = {
55

66
let generation_settings = null;
77

8+
export class CompletionError extends Error {
9+
constructor(message, name, data) {
10+
super(message);
11+
this.name = name;
12+
}
13+
};
814

915
// Completes the prompt as a generator. Recommended for most use cases.
1016
//
@@ -39,6 +45,18 @@ export async function* llama(prompt, params = {}, config = {}) {
3945
signal: controller.signal,
4046
});
4147

48+
const status = response.status;
49+
if (status !== 200) {
50+
try {
51+
const body = await response.json();
52+
if (body && body.error && body.error.message) {
53+
throw new CompletionError(body.error.message, 'ServerError');
54+
}
55+
} catch (err) {
56+
throw new CompletionError(err.message, 'ServerError');
57+
}
58+
}
59+
4260
const reader = response.body.getReader();
4361
const decoder = new TextDecoder();
4462

examples/server/public/index.html

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,14 @@ <h3 class="text-lg font-bold mb-6">Settings</h3>
285285
conv.lastModified = Date.now();
286286
localStorage.setItem(convId, JSON.stringify(conv));
287287
},
288+
popMsg(convId) {
289+
const conv = StorageUtils.getOneConversation(convId);
290+
if (!conv) return;
291+
const msg = conv.messages.pop();
292+
conv.lastModified = Date.now();
293+
localStorage.setItem(convId, JSON.stringify(conv));
294+
return msg;
295+
},
288296

289297
// manage config
290298
getConfig() {
@@ -425,6 +433,7 @@ <h3 class="text-lg font-bold mb-6">Settings</h3>
425433
top_p: this.config.top_p,
426434
max_tokens: this.config.max_tokens,
427435
...(this.config.custom.length ? JSON.parse(this.config.custom) : {}),
436+
...(this.config.apiKey ? { api_key: this.config.apiKey } : {}),
428437
};
429438
const config = {
430439
controller: abortController,
@@ -457,13 +466,16 @@ <h3 class="text-lg font-bold mb-6">Settings</h3>
457466
} else {
458467
console.error(error);
459468
alert(error);
460-
this.inputMsg = this.pendingMsg.content || '';
469+
// pop last user message
470+
const lastUserMsg = StorageUtils.popMsg(currConvId);
471+
this.inputMsg = lastUserMsg ? lastUserMsg.content : '';
461472
}
462473
}
463474

464475
this.pendingMsg = null;
465476
this.isGenerating = false;
466477
this.stopGeneration = () => {};
478+
this.fetchMessages();
467479
},
468480

469481
// message actions

examples/server/server.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2356,6 +2356,11 @@ int main(int argc, char ** argv) {
23562356
"/v1/models",
23572357
};
23582358

2359+
// If this is OPTIONS request, skip validation because browsers don't include Authorization header
2360+
if (req.method == "OPTIONS") {
2361+
return true;
2362+
}
2363+
23592364
// If API key is not set, skip validation
23602365
if (params.api_keys.empty()) {
23612366
return true;

0 commit comments

Comments
 (0)