Skip to content

Commit f808bab

Browse files
committed
web: Add a 'paste' endpoint and better error handling
1 parent 335225d commit f808bab

5 files changed

Lines changed: 26 additions & 6 deletions

File tree

src/web/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { IS_ENABLED } from '@/enabled';
66
import { Logger } from '@/utils/logger';
77
import loadAPI from '@/web/loaders/api';
88
import loadBundles from '@/web/loaders/bundles';
9+
import loadErrorHandler from '@/web/loaders/errors';
910
import loadStatic from '@/web/loaders/static';
1011
import loadUI from '@/web/loaders/ui';
1112

@@ -24,6 +25,7 @@ if (IS_ENABLED.WEB) {
2425
.then(() => loadAPI(app))
2526
.then(() => loadBundles(app))
2627
.then(() => loadUI(app))
28+
.then(() => loadErrorHandler(app))
2729
.then(() => app.listen(port, () => Logger.log(`Web is running!`)));
2830
}
2931

src/web/loaders/api.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Router } from 'express';
22

33
import { fsPath } from '@/utils/fsPath';
4-
import { errorHandler } from '@/web/loaders/errors';
54
import { readFileStructure } from '@/web/loaders/util';
65

76
import type { APIRoute } from '@/types/web';
@@ -19,5 +18,4 @@ export default async function init(app: Application): Promise<void> {
1918
);
2019

2120
app.use('/api', router);
22-
app.use('/api', errorHandler);
2321
}

src/web/loaders/errors.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1+
import { escapeHTML } from 'ps-client/tools';
2+
13
import { Logger } from '@/utils/logger';
24
import { WebError } from '@/utils/webError';
35

4-
import type { NextFunction, Request, Response } from 'express';
6+
import type { Application, NextFunction, Request, Response } from 'express';
57

6-
export function errorHandler(err: Error, req: Request, res: Response, _next: NextFunction): void {
8+
function errorHandler(err: Error, req: Request, res: Response, _next: NextFunction): void {
79
if (!(err instanceof WebError)) Logger.log(req, err);
10+
const message = escapeHTML(err.message ?? 'An internal server error occurred!');
811
res
9-
// .header('content-type', 'application/text')
1012
.status('statusCode' in err && typeof err.statusCode === 'number' ? err.statusCode : 501)
11-
.send(err.message ?? 'An internal server error occurred!');
13+
.send(
14+
`<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>${message}</title></head><body><pre>${message}</pre></body></html>`
15+
);
16+
}
17+
18+
export default function init(app: Application): void {
19+
app.use(errorHandler);
1220
}

src/web/pastes/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.txt

src/web/ui/paste/[pasteId].tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { fsPath } from '@/utils/fsPath';
2+
import { WebError } from '@/utils/webError';
3+
4+
import type { UIRouteHandler } from '@/types/web';
5+
6+
export const handler: UIRouteHandler = (req, res, next) => {
7+
const { pasteId } = req.params;
8+
res.sendFile(fsPath('web', 'pastes', `${pasteId}.txt`), () => {
9+
return next(new WebError(`Paste '${pasteId}' not found`, 404));
10+
});
11+
};

0 commit comments

Comments
 (0)