Skip to content

Commit

Permalink
better error msgs
Browse files Browse the repository at this point in the history
reload page support
  • Loading branch information
chaitanyapotti committed Jul 11, 2024
1 parent d8f45b5 commit 771a0a3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 55 deletions.
69 changes: 36 additions & 33 deletions src/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
TorusVerifierResponse,
} from "./handlers/interfaces";
import { registerServiceWorker } from "./registerServiceWorker";
import SentryHandler from "./sentry";
import { Sentry } from "./sentry";
import { AGGREGATE_VERIFIER, SENTRY_TXNS, TORUS_METHOD, UX_MODE, UX_MODE_TYPE } from "./utils/enums";
import { serializeError } from "./utils/error";
import { handleRedirectParameters, isFirefox, padUrlString } from "./utils/helpers";
Expand All @@ -47,7 +47,7 @@ class CustomAuth {

storageHelper: StorageHelper;

sentryHandler: SentryHandler;
sentryHandler: Sentry;

constructor({
baseUrl,
Expand Down Expand Up @@ -93,7 +93,7 @@ class CustomAuth {
if (enableLogging) log.enableAll();
else log.disableAll();
this.storageHelper = new StorageHelper(storageServerUrl);
this.sentryHandler = new SentryHandler(sentry);
this.sentryHandler = sentry;
}

async init({ skipSw = false, skipInit = false, skipPrefetch = false }: InitParams = {}): Promise<void> {
Expand Down Expand Up @@ -188,7 +188,7 @@ class CustomAuth {
throw new Error("Not initialized yet");
}
if (!aggregateVerifierType || !verifierIdentifier || !Array.isArray(subVerifierDetailsArray)) {
throw new Error("Invalid params");
throw new Error("Invalid params. Missing aggregateVerifierType, verifierIdentifier or subVerifierDetailsArray");
}
if (aggregateVerifierType === AGGREGATE_VERIFIER.SINGLE_VERIFIER_ID && subVerifierDetailsArray.length !== 1) {
throw new Error("Single id verifier can only have one sub verifier");
Expand Down Expand Up @@ -268,7 +268,7 @@ class CustomAuth {
!aggregateLoginParams.verifierIdentifier ||
!Array.isArray(aggregateLoginParams.subVerifierDetailsArray)
) {
throw new Error("Invalid params");
throw new Error("Invalid params. Missing aggregateVerifierType, verifierIdentifier or subVerifierDetailsArray");
}
if (
aggregateLoginParams.aggregateVerifierType === AGGREGATE_VERIFIER.SINGLE_VERIFIER_ID &&
Expand Down Expand Up @@ -354,28 +354,28 @@ class CustomAuth {
idToken: string,
additionalParams?: ExtraParams
): Promise<TorusKey> {
const nodeTx = this.sentryHandler.startTransaction({
name: SENTRY_TXNS.FETCH_NODE_DETAILS,
});
const nodeDetails = await this.nodeDetailManager.getNodeDetails({ verifier, verifierId });
this.sentryHandler.finishTransaction(nodeTx);
const nodeDetails = await this.sentryHandler.startSpan(
{
name: SENTRY_TXNS.FETCH_NODE_DETAILS,
},
async () => {
return this.nodeDetailManager.getNodeDetails({ verifier, verifierId });
}
);

log.debug("torus-direct/getTorusKey", { torusNodeEndpoints: nodeDetails.torusNodeEndpoints });

const sharesTx = this.sentryHandler.startTransaction({
name: SENTRY_TXNS.FETCH_SHARES,
});
const sharesResponse = await this.torus.retrieveShares(
nodeDetails.torusNodeEndpoints,
nodeDetails.torusIndexes,
verifier,
verifierParams,
idToken,
const sharesResponse = await this.sentryHandler.startSpan(
{
...additionalParams,
name: SENTRY_TXNS.FETCH_SHARES,
},
async () => {
return this.torus.retrieveShares(nodeDetails.torusNodeEndpoints, nodeDetails.torusIndexes, verifier, verifierParams, idToken, {
...additionalParams,
});
}
);
this.sentryHandler.finishTransaction(sharesTx);

log.debug("torus-direct/getTorusKey", { retrieveShares: sharesResponse });
return sharesResponse;
}
Expand Down Expand Up @@ -410,13 +410,8 @@ class CustomAuth {
queryParams[key] = value;
});

if (replaceUrl) {
const cleanUrl = window.location.origin + window.location.pathname;
window.history.replaceState({ ...window.history.state, as: cleanUrl, url: cleanUrl }, "", cleanUrl);
}

if (!hash && Object.keys(queryParams).length === 0) {
throw new Error("Unable to fetch result from OAuth login");
throw new Error("Found Empty hash and query parameters. This can happen if user reloads the page");
}

const { error, instanceParameters, hashParameters } = handleRedirectParameters(hash, queryParams);
Expand All @@ -428,10 +423,6 @@ class CustomAuth {
const { args, method, ...rest } = await this.storageHelper.retrieveLoginDetails(instanceId);
log.info(args, method);

if (clearLoginDetails) {
this.storageHelper.clearLoginDetailsStorage(instanceId);
}

if (error) {
return { error, state: instanceParameters || {}, method, result: {}, hashParameters, args };
}
Expand Down Expand Up @@ -460,8 +451,11 @@ class CustomAuth {
} catch (err: unknown) {
const serializedError = await serializeError(err);
log.error(serializedError);
if (clearLoginDetails) {
this.storageHelper.clearLoginDetailsStorage(instanceId);
}
return {
error: `Could not get result from torus nodes. \n ${serializedError.message || ""}`,
error: `${serializedError.message || ""}`,
state: instanceParameters || {},
method,
result: {},
Expand All @@ -473,7 +467,7 @@ class CustomAuth {

if (!result)
return {
error: "Unsupported method type",
error: "Init parameters not found. It might be because storage is not available. Please retry the login in a different browser",
state: instanceParameters || {},
method,
result: {},
Expand All @@ -482,6 +476,15 @@ class CustomAuth {
...rest,
};

if (replaceUrl) {
const cleanUrl = window.location.origin + window.location.pathname;
window.history.replaceState({ ...window.history.state, as: cleanUrl, url: cleanUrl }, "", cleanUrl);
}

if (clearLoginDetails) {
this.storageHelper.clearLoginDetailsStorage(instanceId);
}

return { method, result, state: instanceParameters || {}, hashParameters, args, ...rest };
}

Expand Down
23 changes: 2 additions & 21 deletions src/sentry.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,5 @@
import type { Transaction, TransactionContext } from "@sentry/types";
import type { Span, StartSpanOptions } from "@sentry/types";

export interface Sentry {
startTransaction(_: TransactionContext): Transaction;
}
export default class SentryHandler {
sentry: Sentry | null = null;

constructor(sentry?: Sentry) {
this.sentry = sentry;
}

startTransaction(context: TransactionContext): Transaction | void {
if (this.sentry) {
return this.sentry.startTransaction(context);
}
}

finishTransaction(tx: void | Transaction): void {
if (tx) {
tx.finish();
}
}
startSpan<T>(context: StartSpanOptions, callback: (span: Span) => T): T;
}
2 changes: 1 addition & 1 deletion src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const getVerifierId = (
case LOGIN.JWT:
return caseSensitiveField(sub, isVerifierIdCaseSensitive);
default:
throw new Error("Invalid login type");
throw new Error("Invalid login type to get verifier id");
}
};

Expand Down

0 comments on commit 771a0a3

Please sign in to comment.