Skip to content

Commit

Permalink
feat: added logic to handle errors depending on progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Codetrauma committed Feb 18, 2025
1 parent c088d30 commit eb177d9
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 29 deletions.
2 changes: 2 additions & 0 deletions src/entries/Background/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export async function setNotaryRequestError(
export async function setNotaryRequestProgress(
id: string,
progress: RequestProgress,
errorMessage?: string,
): Promise<RequestHistory | null> {
const existing = await historyDb.get(id);

Expand All @@ -123,6 +124,7 @@ export async function setNotaryRequestProgress(
const newReq: RequestHistory = {
...existing,
progress,
errorMessage,
};

await historyDb.put(id, newReq);
Expand Down
14 changes: 10 additions & 4 deletions src/entries/Background/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,10 @@ export enum RequestProgress {
Error,
}

export function progressText(progress: RequestProgress): string {
export function progressText(
progress: RequestProgress,
errorMessage?: string,
): string {
switch (progress) {
case RequestProgress.CreatingProver:
return 'Creating prover...';
Expand All @@ -185,7 +188,9 @@ export function progressText(progress: RequestProgress): string {
case RequestProgress.FinalizingOutputs:
return 'Finalizing notarization outputs...';
case RequestProgress.Error:
return 'Notarization Failed';
return errorMessage

Check failure on line 191 in src/entries/Background/rpc.ts

View workflow job for this annotation

GitHub Actions / build

Replace `⏎········?·errorMessage⏎·······` with `·?·errorMessage`
? errorMessage
: 'Error: Notarization Failed';
}
}

Expand All @@ -211,6 +216,7 @@ export type RequestHistory = {
secretHeaders?: string[];
secretResps?: string[];
cid?: string;
errorMessage?: string;
metadata?: {
[k: string]: string;
};
Expand Down Expand Up @@ -431,9 +437,9 @@ async function handleUpdateRequestProgress(
request: BackgroundAction,
sendResponse: (data?: any) => void,
) {
const { id, progress } = request.data;
const { id, progress, errorMessage } = request.data;

const newReq = await setNotaryRequestProgress(id, progress);
const newReq = await setNotaryRequestProgress(id, progress, errorMessage);
if (!newReq) return;
await pushToRedux(addRequestHistory(await getNotaryRequest(id)));

Expand Down
87 changes: 64 additions & 23 deletions src/entries/Offscreen/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,35 +451,50 @@ async function createProver(options: {

const hostname = urlify(url)?.hostname || '';
const notary = NotaryServer.from(notaryUrl);
try {
updateRequestProgress(id, RequestProgress.CreatingProver);
const prover: TProver = await new Prover({

let prover: TProver;
prover = await handleProgress(

Check failure on line 456 in src/entries/Offscreen/rpc.ts

View workflow job for this annotation

GitHub Actions / build

'prover' is never reassigned. Use 'const' instead
id,
RequestProgress.CreatingProver,
() => new Prover({

Check failure on line 459 in src/entries/Offscreen/rpc.ts

View workflow job for this annotation

GitHub Actions / build

Insert `⏎·····`
id,

Check failure on line 460 in src/entries/Offscreen/rpc.ts

View workflow job for this annotation

GitHub Actions / build

Insert `··`
serverDns: hostname,

Check failure on line 461 in src/entries/Offscreen/rpc.ts

View workflow job for this annotation

GitHub Actions / build

Insert `··`
maxSentData,

Check failure on line 462 in src/entries/Offscreen/rpc.ts

View workflow job for this annotation

GitHub Actions / build

Insert `··`
maxRecvData,

Check failure on line 463 in src/entries/Offscreen/rpc.ts

View workflow job for this annotation

GitHub Actions / build

Insert `··`
});
}),

Check failure on line 464 in src/entries/Offscreen/rpc.ts

View workflow job for this annotation

GitHub Actions / build

Replace `····` with `······`
'Error creating prover'

Check failure on line 465 in src/entries/Offscreen/rpc.ts

View workflow job for this annotation

GitHub Actions / build

Insert `,`
);

updateRequestProgress(id, RequestProgress.GettingSession);
const sessionUrl = await notary.sessionUrl(maxSentData, maxRecvData);
const sessionUrl = await handleProgress(
id,
RequestProgress.GettingSession,
() => notary.sessionUrl(maxSentData, maxRecvData),
'Error getting session from Notary'

Check failure on line 472 in src/entries/Offscreen/rpc.ts

View workflow job for this annotation

GitHub Actions / build

Insert `,`
);

updateRequestProgress(id, RequestProgress.SettingUpProver);
await prover.setup(sessionUrl);
await handleProgress(
id,
RequestProgress.SettingUpProver,
() => prover.setup(sessionUrl),
'Error setting up prover'
);

updateRequestProgress(id, RequestProgress.SendingRequest);
await prover.sendRequest(websocketProxyUrl + `?token=${hostname}`, {
url,
method,
headers,
body,
});
await handleProgress(
id,
RequestProgress.SendingRequest,
() =>
prover.sendRequest(websocketProxyUrl + `?token=${hostname}`, {
url,
method,
headers,
body,
}),
'Error sending request'
);

return prover;
} catch (error: any) {
updateRequestProgress(id, RequestProgress.Error);
throw error;
}
return prover;
}

async function verifyProof(
proof: PresentationJSON,
): Promise<{ sent: string; recv: string }> {
Expand Down Expand Up @@ -508,13 +523,39 @@ async function verifyProof(
return result;
}

function updateRequestProgress(id: string, progress: RequestProgress) {
devlog(`Request ${id}: ${progressText(progress)}`);
function updateRequestProgress(
id: string,
progress: RequestProgress,
errorMessage?: string,
) {
const progressMessage =
progress === RequestProgress.Error
? `${errorMessage || 'Notarization Failed'}`
: progressText(progress);
console.log('ERROR MESSAGE', progressMessage);
devlog(`Request ${id}: ${progressMessage}`);

browser.runtime.sendMessage({
type: BackgroundActiontype.update_request_progress,
data: {
id,
progress: progress,
progress,
errorMessage,
},
});
}

async function handleProgress<T>(
id: string,
progress: RequestProgress,
action: () => Promise<T>,
errorMessage: string,
): Promise<T> {
try {
updateRequestProgress(id, progress);
return await action();
} catch (error: any) {
updateRequestProgress(id, RequestProgress.Error, errorMessage);
throw error;
}
}
2 changes: 1 addition & 1 deletion src/entries/SidePanel/SidePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ function StepContent(
<Icon className="animate-spin" fa="fa-solid fa-spinner" size={1} />
<span className="text-sm">
{notaryRequest?.progress === 6
? 'Error: Notarization Failed'
? `${progressText(notaryRequest.progress, notaryRequest?.errorMessage)}`
: notaryRequest?.progress
? `(${(
((notaryRequest.progress + 1) / 6.06) *
Expand Down
2 changes: 1 addition & 1 deletion src/pages/History/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export function OneRequestHistory(props: {
/>
<span className="">
{request?.progress === 6
? 'Error: Notarization Failed'
? `${progressText(request.progress, request.errorMessage)}`
: request?.progress
? `(${(
((request.progress + 1) / 6.06) *
Expand Down

0 comments on commit eb177d9

Please sign in to comment.