Remove custom errors indirection

This commit is contained in:
Manav Rathi 2024-04-03 15:45:20 +05:30
parent f10f751a2f
commit 8a00f1b85f
No known key found for this signature in database
3 changed files with 18 additions and 30 deletions

View file

@ -2,27 +2,17 @@ import { Container } from "components/Container";
import { Spinner } from "components/Spinner";
import * as React from "react";
import { parseAndHandleRequest } from "services/billingService";
import { CUSTOM_ERROR } from "utils/error";
import constants from "utils/strings";
export default function Home() {
const [errorMessageView, setErrorMessageView] = React.useState(false);
const [loading, setLoading] = React.useState(false);
const [failed, setFailed] = React.useState(false);
React.useEffect(() => {
async function main() {
try {
setLoading(true);
await parseAndHandleRequest();
} catch (e: unknown) {
if (
e instanceof Error &&
e.message === CUSTOM_ERROR.DIRECT_OPEN_WITH_NO_QUERY_PARAMS
) {
window.location.href = "https://ente.io";
} else {
setErrorMessageView(true);
}
} catch {
setFailed(true);
}
}
// TODO: audit
@ -32,11 +22,7 @@ export default function Home() {
return (
<Container>
{errorMessageView ? (
<div>{constants.SOMETHING_WENT_WRONG}</div>
) : (
loading && <Spinner />
)}
{failed ? constants.SOMETHING_WENT_WRONG : <Spinner />}
</Container>
);
}

View file

@ -7,7 +7,6 @@
/* eslint-disable @typescript-eslint/no-unnecessary-condition */
import { loadStripe } from "@stripe/stripe-js";
import { CUSTOM_ERROR } from "utils/error";
import { logError } from "utils/log";
import HTTPService from "./HTTPService";
@ -88,9 +87,15 @@ export async function parseAndHandleRequest() {
const action = urlParams.get("action");
const redirectURL = urlParams.get("redirectURL");
if (!action && !paymentToken && !productID && !redirectURL) {
throw Error(CUSTOM_ERROR.DIRECT_OPEN_WITH_NO_QUERY_PARAMS);
// Maybe someone attempted to directly open this page in their
// browser. Not much we can do, just redirect them to the main site.
console.log(
"None of the required query parameters were supplied, redirecting to the ente.io",
);
redirectHome();
return;
} else if (!action || !paymentToken || !productID || !redirectURL) {
throw Error(CUSTOM_ERROR.MISSING_REQUIRED_QUERY_PARAM);
throw Error("Required query parameter was not provided");
}
switch (action) {
case PaymentActionType.Buy:
@ -100,13 +105,11 @@ export async function parseAndHandleRequest() {
await updateSubscription(productID, paymentToken, redirectURL);
break;
default:
throw Error(CUSTOM_ERROR.INVALID_ACTION);
throw Error(`Unsupported action ${action}`);
}
} catch (e: any) {
console.error("Error: ", JSON.stringify(e));
if (e.message !== CUSTOM_ERROR.DIRECT_OPEN_WITH_NO_QUERY_PARAMS) {
logError(e);
}
logError(e);
throw e;
}
}
@ -285,3 +288,7 @@ function redirectToApp(redirectURL: string, status: string, reason?: string) {
}
window.location.href = completePath;
}
const redirectHome = () => {
window.location.href = "https://ente.io";
};

View file

@ -1,5 +0,0 @@
export const CUSTOM_ERROR = {
DIRECT_OPEN_WITH_NO_QUERY_PARAMS: "direct open with no query params",
MISSING_REQUIRED_QUERY_PARAM: "missing required query param",
INVALID_ACTION: "invalid action",
};