Improve error handling (fixes #897)

This commit is contained in:
Felix Ableitner 2023-05-12 02:32:35 +02:00
parent c5fd084577
commit 32c095a850
5 changed files with 27 additions and 10 deletions

View file

@ -2,7 +2,7 @@ import express from "express";
import fs from "fs";
import { IncomingHttpHeaders } from "http";
import { Helmet } from "inferno-helmet";
import { matchPath, StaticRouter } from "inferno-router";
import { StaticRouter, matchPath } from "inferno-router";
import { renderToString } from "inferno-server";
import IsomorphicCookie from "isomorphic-cookie";
import { GetSite, GetSiteResponse, LemmyHttp } from "lemmy-js-client";
@ -141,7 +141,7 @@ server.get("/*", async (req, res) => {
const routeData = await Promise.all(promises);
// Redirect to the 404 if there's an API error
// Handle API errors
if (routeData[0] && routeData[0].error) {
const error = routeData[0].error;
console.error(error);
@ -209,7 +209,7 @@ server.get("/*", async (req, res) => {
<!-- Current theme and more -->
${helmet.link.toString()}
</head>
<body ${helmet.bodyAttributes.toString()}>
@ -223,10 +223,17 @@ server.get("/*", async (req, res) => {
<script defer src='/static/js/client.js'></script>
</body>
</html>
`);
`);
} catch (err) {
console.error(err);
return res.send(`404: ${removeAuthParam(err)}`);
var formatted: string;
if (err.status && err.message) {
res.status(err.status);
formatted = `${err.status}: ${err.message}`;
} else {
res.status(500);
formatted = err;
}
res.send(formatted);
}
});

View file

@ -17,6 +17,7 @@ import { InitialFetchRequest } from "../../interfaces";
import { WebSocketService } from "../../services";
import {
capitalizeFirstLetter,
check_auth,
isBrowser,
myAuth,
randomStr,
@ -60,6 +61,8 @@ export class AdminSettings extends Component<any, AdminSettingsState> {
this.parseMessage = this.parseMessage.bind(this);
this.subscription = wsSubscribe(this.parseMessage);
check_auth();
// Only fetch the data if coming from another route
if (this.isoData.path == this.context.router.route.match.url) {
this.state = {

View file

@ -67,6 +67,9 @@ export class Setup extends Component<any, State> {
}
render() {
if (this.state.siteRes.site_view.local_site.site_setup) {
throw { status: 403, message: "Site is already setup" };
}
return (
<div className="container-lg">
<Helmet title={this.documentTitle} />

View file

@ -29,6 +29,7 @@ import { i18n } from "../../i18next";
import { CommentViewType, InitialFetchRequest } from "../../interfaces";
import { UserService, WebSocketService } from "../../services";
import {
check_auth,
commentsToFlatNodes,
createCommentLikeRes,
editCommentRes,
@ -111,10 +112,7 @@ export class Inbox extends Component<any, InboxState> {
this.handleSortChange = this.handleSortChange.bind(this);
this.handlePageChange = this.handlePageChange.bind(this);
if (!UserService.Instance.myUserInfo && isBrowser()) {
toast(i18n.t("not_logged_in"), "danger");
this.context.router.history.push(`/login`);
}
check_auth();
this.parseMessage = this.parseMessage.bind(this);
this.subscription = wsSubscribe(this.parseMessage);

View file

@ -1603,3 +1603,9 @@ export function getQueryString<T extends Record<string, string | undefined>>(
"?"
);
}
export function check_auth() {
if (!UserService.Instance.myUserInfo) {
throw { status: 401, message: "Login required" };
}
}