Skip to content

Commit

Permalink
handle 401 in OpenAI API
Browse files Browse the repository at this point in the history
  • Loading branch information
cgawron committed Mar 14, 2024
1 parent 9196169 commit 714a37e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/chat/context/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export default function action(state, dispatch) {
chat: newChat,
});
},
onStar() {},
onStar() { },
onEnd() {
setState({
is: { ...is, thinking: false },
Expand All @@ -64,6 +64,10 @@ export default function action(state, dispatch) {
console.log(res);
const { error } = res || {};
if (error) {
if (error === "Unauthorized") {
console.log("Unauthorized");
window.location.href = "https://login.ki.fh-swf.de/openai/api/login";
}
newChat.splice(currentChat, 1, {
...chat[currentChat],
error,
Expand Down Expand Up @@ -127,10 +131,10 @@ export default function action(state, dispatch) {
content === ""
? {}
: {
role: "user",
content,
id: Date.now(),
};
role: "user",
content,
id: Date.now(),
};
setState({ is: { ...state.is, typeing: true }, typeingMessage });
},

Expand Down
4 changes: 4 additions & 0 deletions src/chat/service/openai.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ export const fetchStream = async ({
);
if (!result) return;
if (!result.ok) {
if (result.status === 401) {
onError && onError("Unauthorized", controller);
return;
}
const error = await result.json();
onError && onError(error);
return;
Expand Down

0 comments on commit 714a37e

Please sign in to comment.