Skip to content

Commit

Permalink
start of better frontend error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
martypdx committed Oct 27, 2023
1 parent 1e7b18a commit 7cedc9b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 16 deletions.
85 changes: 71 additions & 14 deletions src/services/spirit-wave.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,85 @@
// Tests for this regex: https://regex101.com/r/5zXb2v/2
const ExtractPreContentRegex = /<pre>(.+?)<\/pre>/gims;

const getStream = (url) => async () => {
const res = await fetch(url, {
headers: {
/* spell-checker: disable */
// Authorization: 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0'
/* spell-checker: enable */
const res = await getResponse(url);
try {
if (!res.ok) {
let error = null;
error = await res.text();
if (error.startsWith('<!DOCTYPE html>')) {
const matches = error.matchAll(ExtractPreContentRegex);
let message = '';
for (const [, group] of matches) {
if (message) message += '\n';
message += group ?? '';
}

throw message;
}
else {
try {
error = JSON.parse(error);
}
finally {
// eslint-disable-next-line no-unsafe-finally
throw error;
}
}
}
});
if (!res.ok) {
const error = await res.json();
throw error;

return res.body.pipeThrough(new TextDecoderStream());
}
catch (err) {
//TODO: handle different failures:
// - res.json issues (might go in code block with .json()?)
// - no body
// - piping issues thru textdecoder?

return res.body.pipeThrough(new TextDecoderStream());
// eslint-disable-next-line no-console
console.log (err);
throw new FetchError(err);
}
};

class FetchError extends Error {
constructor(err) {
super(`Unable to retrieve AI response: \n${err}`);
}
}

// connection or other pre-async failures
class ConnectivityError extends Error {
constructor(err) {
super(`Unable to connect with server to get AI response: \n${err}`);
}
}

function getResponse(url) {
try {
return fetch(url, {
headers: {
/* spell-checker: disable */
// TODO: use logged in supabase user
// Authorization: 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0'
/* spell-checker: enable */
}
});
}
catch (err) {
// eslint-disable-next-line no-console
console.log (err);
throw new ConnectivityError(err);
}
}

const SUPABASE_PROJECT_URL = window.SUPABASE_PROJECT_URL || '';
const API = `${SUPABASE_PROJECT_URL}/functions/v1`;
const API_URL = `${SUPABASE_PROJECT_URL}/functions/v1`;

const API_KEY = window.SUPABASE_API_KEY;

const API_KEY_QUERY = API_KEY ? `?apikey=${encodeURIComponent(API_KEY)}` : '';


const getUrl = path => `${API}${path}${API_KEY_QUERY}`;
const getUrl = path => `${API_URL}${path}${API_KEY_QUERY}`;

export const streamGreeting = getStream(getUrl('/greeting'));
export const streamInvocation = getStream(getUrl('/invocation'));
4 changes: 2 additions & 2 deletions src/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ async function tryStream(getStream) {
await stream.pipeTo(domStream);
}
catch (err) {
// TODO: figure how how to deal with failures
// TODO: better handling of failures. maybe a service at some point
// eslint-disable-next-line no-console
console.error('oh noes!', err);
alert(err?.constructor?.name + ' - ' + err.message);
}
}

0 comments on commit 7cedc9b

Please sign in to comment.