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

View file

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

View file

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

View file

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