Skip to content

Commit e7c0b83

Browse files
committed
feat: add querystring-informed errors
1 parent 616f28d commit e7c0b83

File tree

4 files changed

+83
-2
lines changed

4 files changed

+83
-2
lines changed

packages/backend/src/api/APIError.js

+19
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* You should have received a copy of the GNU Affero General Public License
1717
* along with this program. If not, see <https://www.gnu.org/licenses/>.
1818
*/
19+
const { URLSearchParams } = require("node:url");
1920
const { quot } = require("../util/strutil");
2021

2122
/**
@@ -518,6 +519,24 @@ module.exports = class APIError {
518519
status: this.status,
519520
};
520521
}
522+
523+
querystringize (extra) {
524+
return new URLSearchParams(this.querystringize_(extra));
525+
}
526+
527+
querystringize_ (extra) {
528+
const fields = {};
529+
for ( const k in this.fields ) {
530+
fields[`field_${k}`] = this.fields[k];
531+
}
532+
return {
533+
...extra,
534+
error: true,
535+
message: this.message,
536+
status: this.status,
537+
...fields,
538+
};
539+
}
521540

522541
get message () {
523542
const message = typeof this._message === 'function'

packages/backend/src/routers/_default.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ router.all('*', async function(req, res, next) {
317317
// index.js
318318
if(path === '/'){
319319
const svc_puterHomepage = Context.get('services').get('puter-homepage');
320-
return svc_puterHomepage.send(res, {
320+
return svc_puterHomepage.send({ req, res }, {
321321
title: app_title,
322322
description: description || config.short_description,
323323
short_description: config.short_description,

packages/backend/src/services/PuterHomepageService.js

+53-1
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,28 @@ class PuterHomepageService extends BaseService {
3232
this.service_scripts.push(url);
3333
}
3434

35-
async send (res, meta, launch_options) {
35+
async send ({ req, res }, meta, launch_options) {
3636
const config = this.global_config;
37+
38+
if (
39+
req.query['puter.app_instance_id'] ||
40+
req.query['error_from_within_iframe']
41+
) {
42+
const easteregg = [
43+
'puter in puter?',
44+
'Infinite recursion!',
45+
'what\'chu cookin\'?',
46+
];
47+
const message = req.query.message ||
48+
easteregg[
49+
Math.floor(Math.random(easteregg.length))
50+
];
51+
52+
return res.send(this.generate_error_html({
53+
message,
54+
}));
55+
}
56+
3757
return res.send(this.generate_puter_page_html({
3858
env: config.env,
3959

@@ -271,6 +291,38 @@ class PuterHomepageService extends BaseService {
271291
272292
</html>`;
273293
};
294+
295+
generate_error_html ({ message }) {
296+
return `
297+
<!DOCTYPE html>
298+
<html>
299+
<head>
300+
<style type="text/css">
301+
@font-face {
302+
font-family: 'Inter';
303+
src: url('/fonts/Inter-Thin.ttf') format('truetype');
304+
font-weight: 100;
305+
}
306+
BODY {
307+
box-sizing: border-box;
308+
margin: 0;
309+
height: 100vh;
310+
width: 100vw;
311+
background-color: #2f70ab;
312+
color: #f2f7f7;
313+
font-family: "Inter", "Helvetica Neue", HelveticaNeue, Helvetica, Arial, sans-serif;
314+
display: flex;
315+
align-items: center;
316+
justify-content: center;
317+
}
318+
</style>
319+
</head>
320+
<body>
321+
<h1>${message}</h1>
322+
</body>
323+
</html>
324+
`;
325+
}
274326
}
275327

276328
module.exports = {

src/initgui.js

+10
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,16 @@ window.initgui = async function(options){
286286

287287
}
288288
}
289+
290+
//--------------------------------------------------------------------------------------
291+
// Display an error if the query parameters have an error
292+
//--------------------------------------------------------------------------------------
293+
if ( window.url_query_params.has('error') ) {
294+
// TODO: i18n
295+
await UIAlert({
296+
message: window.url_query_params.get('message')
297+
});
298+
}
289299

290300
//--------------------------------------------------------------------------------------
291301
// Get user referral code from URL query params

0 commit comments

Comments
 (0)