Skip to content

Commit 26e1ddd

Browse files
committed
接口状态异常时抛出错误
1 parent cecb1a4 commit 26e1ddd

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,18 @@ export default class Api2d
4141
return new Promise( async (resolve, reject) => {
4242
try {
4343
let chars = "";
44-
console.log( "in stream" );
44+
// console.log( "in stream" );
4545
const response = await fetchEventSource( url, {
4646
method: "POST",
4747
headers: {...headers, "Accept": "text/event-stream"},
4848
body: JSON.stringify( {...restOptions, model:model||'gpt-3.5-turbo'}),
49+
async onopen( response )
50+
{
51+
if( response.status != 200 )
52+
{
53+
throw new Error( response.statusText );
54+
}
55+
},
4956
onmessage: e => {
5057
if( e.data == '[DONE]' )
5158
{
@@ -61,9 +68,12 @@ export default class Api2d
6168
}
6269

6370
},
71+
onerror: error => {
72+
throw new Error( error );
73+
}
6474
});
6575
} catch (error) {
66-
console.log( error );
76+
// console.log( error );
6777
reject( error );
6878
}
6979
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "api2d",
3-
"version": "0.1.5",
3+
"version": "0.1.6",
44
"description": "pure browser sdk for api2d and openai",
55
"main": "index.js",
66
"repository": "https://github.com/easychen/api2d-js",

0 commit comments

Comments
 (0)