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 fs from "fs";
import { IncomingHttpHeaders } from "http"; import { IncomingHttpHeaders } from "http";
import { Helmet } from "inferno-helmet"; import { Helmet } from "inferno-helmet";
import { matchPath, StaticRouter } from "inferno-router"; import { StaticRouter, matchPath } from "inferno-router";
import { renderToString } from "inferno-server"; import { renderToString } from "inferno-server";
import IsomorphicCookie from "isomorphic-cookie"; import IsomorphicCookie from "isomorphic-cookie";
import { GetSite, GetSiteResponse, LemmyHttp } from "lemmy-js-client"; import { GetSite, GetSiteResponse, LemmyHttp } from "lemmy-js-client";
@ -141,7 +141,7 @@ server.get("/*", async (req, res) => {
const routeData = await Promise.all(promises); 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) { if (routeData[0] && routeData[0].error) {
const error = routeData[0].error; const error = routeData[0].error;
console.error(error); console.error(error);
@ -223,10 +223,17 @@ server.get("/*", async (req, res) => {
<script defer src='/static/js/client.js'></script> <script defer src='/static/js/client.js'></script>
</body> </body>
</html> </html>
`); `);
} catch (err) { } catch (err) {
console.error(err); var formatted: string;
return res.send(`404: ${removeAuthParam(err)}`); 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 { WebSocketService } from "../../services";
import { import {
capitalizeFirstLetter, capitalizeFirstLetter,
check_auth,
isBrowser, isBrowser,
myAuth, myAuth,
randomStr, randomStr,
@ -60,6 +61,8 @@ export class AdminSettings extends Component<any, AdminSettingsState> {
this.parseMessage = this.parseMessage.bind(this); this.parseMessage = this.parseMessage.bind(this);
this.subscription = wsSubscribe(this.parseMessage); this.subscription = wsSubscribe(this.parseMessage);
check_auth();
// Only fetch the data if coming from another route // Only fetch the data if coming from another route
if (this.isoData.path == this.context.router.route.match.url) { if (this.isoData.path == this.context.router.route.match.url) {
this.state = { this.state = {

View file

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

View file

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