Refactor how error data is passed from server to client

This commit is contained in:
abias 2023-05-16 20:34:15 -04:00
parent 24c4427c82
commit 4f1d357b5b
5 changed files with 13 additions and 23 deletions

View file

@ -130,6 +130,7 @@ server.get("/*", async (req, res) => {
// in order to remove the jwt on the browser. Necessary for wrong jwts
let site: GetSiteResponse | undefined = undefined;
let routeData: any[] = [];
let errorPageData: ErrorPageData | undefined;
try {
let try_site: any = await client.getSite(getSiteForm);
if (try_site.error == "not_logged_in") {
@ -165,7 +166,7 @@ server.get("/*", async (req, res) => {
}
}
} catch (error) {
routeData = getErrorRouteData(error, site);
errorPageData = getErrorRouteData(error, site);
}
// Redirect to the 404 if there's an API error
@ -175,7 +176,7 @@ server.get("/*", async (req, res) => {
if (error === "instance_is_private") {
return res.redirect(`/signup`);
} else {
routeData = getErrorRouteData(error, site);
errorPageData = getErrorRouteData(error, site);
}
}
@ -183,6 +184,7 @@ server.get("/*", async (req, res) => {
path,
site_res: site,
routeData,
errorPageData,
};
const wrapper = (
@ -293,7 +295,7 @@ async function fetchIconPng(iconUrl: string) {
}
function getErrorRouteData(error: string, site?: GetSiteResponse) {
const errorPageData: ErrorPageData = { type: "error" };
const errorPageData: ErrorPageData = {};
// Exact error should only be seen in a development environment. Users
// in production will get a more generic message.
@ -308,7 +310,7 @@ function getErrorRouteData(error: string, site?: GetSiteResponse) {
errorPageData.adminMatrixIds = adminMatrixIds;
}
return [errorPageData];
return errorPageData;
}
async function createSsrHtml(root: string, isoData: IsoDataOptionalSite) {

View file

@ -1,7 +1,7 @@
import { Component } from "inferno";
import { Link } from "inferno-router";
import { IsoDataOptionalSite } from "shared/interfaces";
import { ErrorPageData, setIsoData } from "../../utils";
import { setIsoData } from "../../utils";
export class ErrorPage extends Component<any, any> {
private isoData: IsoDataOptionalSite = setIsoData(this.context);
@ -11,7 +11,7 @@ export class ErrorPage extends Component<any, any> {
}
render() {
const errorPageData = this.getErrorPageData();
const { errorPageData } = this.isoData;
return (
<div className="container-lg text-center">
@ -65,15 +65,4 @@ export class ErrorPage extends Component<any, any> {
</div>
);
}
private getErrorPageData() {
const errorPageData = this.isoData.routeData[0] as
| ErrorPageData
| undefined;
if (errorPageData?.type === "error") {
return errorPageData;
}
return undefined;
}
}

View file

@ -1,5 +1,5 @@
import { Component } from "inferno";
import { ErrorPageData, setIsoData } from "../../utils";
import { setIsoData } from "../../utils";
import { ErrorPage } from "../app/error-page";
class ErrorGuard extends Component<any, any> {
@ -10,12 +10,10 @@ class ErrorGuard extends Component<any, any> {
}
render() {
const errorPageData = this.isoData.routeData[0] as
| ErrorPageData
| undefined;
const errorPageData = this.isoData.errorPageData;
const siteRes = this.isoData.site_res;
if (errorPageData?.type === "error" || !siteRes) {
if (errorPageData || !siteRes) {
return <ErrorPage />;
} else {
return this.props.children;

View file

@ -1,5 +1,6 @@
import { CommentView, GetSiteResponse, LemmyHttp } from "lemmy-js-client";
import type { ParsedQs } from "qs";
import { ErrorPageData } from "./utils";
/**
* This contains serialized data, it needs to be deserialized before use.
@ -8,6 +9,7 @@ export interface IsoData {
path: string;
routeData: any[];
site_res: GetSiteResponse;
errorPageData?: ErrorPageData;
}
export type IsoDataOptionalSite = Partial<IsoData> &

View file

@ -106,7 +106,6 @@ export type ThemeColor =
| "gray-dark";
export interface ErrorPageData {
type: "error";
error?: string;
adminMatrixIds?: string[];
}