From e7c0b8320a6829315d9154d6d513bab4491c47ea Mon Sep 17 00:00:00 2001 From: KernelDeimos Date: Tue, 18 Jun 2024 22:08:44 -0400 Subject: [PATCH] feat: add querystring-informed errors --- packages/backend/src/api/APIError.js | 19 +++++++ packages/backend/src/routers/_default.js | 2 +- .../src/services/PuterHomepageService.js | 54 ++++++++++++++++++- src/initgui.js | 10 ++++ 4 files changed, 83 insertions(+), 2 deletions(-) diff --git a/packages/backend/src/api/APIError.js b/packages/backend/src/api/APIError.js index 72dbb069..4e36ac2d 100644 --- a/packages/backend/src/api/APIError.js +++ b/packages/backend/src/api/APIError.js @@ -16,6 +16,7 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ +const { URLSearchParams } = require("node:url"); const { quot } = require("../util/strutil"); /** @@ -518,6 +519,24 @@ module.exports = class APIError { status: this.status, }; } + + querystringize (extra) { + return new URLSearchParams(this.querystringize_(extra)); + } + + querystringize_ (extra) { + const fields = {}; + for ( const k in this.fields ) { + fields[`field_${k}`] = this.fields[k]; + } + return { + ...extra, + error: true, + message: this.message, + status: this.status, + ...fields, + }; + } get message () { const message = typeof this._message === 'function' diff --git a/packages/backend/src/routers/_default.js b/packages/backend/src/routers/_default.js index c6c521de..be5e2ad7 100644 --- a/packages/backend/src/routers/_default.js +++ b/packages/backend/src/routers/_default.js @@ -317,7 +317,7 @@ router.all('*', async function(req, res, next) { // index.js if(path === '/'){ const svc_puterHomepage = Context.get('services').get('puter-homepage'); - return svc_puterHomepage.send(res, { + return svc_puterHomepage.send({ req, res }, { title: app_title, description: description || config.short_description, short_description: config.short_description, diff --git a/packages/backend/src/services/PuterHomepageService.js b/packages/backend/src/services/PuterHomepageService.js index 31185f80..32d32e52 100644 --- a/packages/backend/src/services/PuterHomepageService.js +++ b/packages/backend/src/services/PuterHomepageService.js @@ -32,8 +32,28 @@ class PuterHomepageService extends BaseService { this.service_scripts.push(url); } - async send (res, meta, launch_options) { + async send ({ req, res }, meta, launch_options) { const config = this.global_config; + + if ( + req.query['puter.app_instance_id'] || + req.query['error_from_within_iframe'] + ) { + const easteregg = [ + 'puter in puter?', + 'Infinite recursion!', + 'what\'chu cookin\'?', + ]; + const message = req.query.message || + easteregg[ + Math.floor(Math.random(easteregg.length)) + ]; + + return res.send(this.generate_error_html({ + message, + })); + } + return res.send(this.generate_puter_page_html({ env: config.env, @@ -271,6 +291,38 @@ class PuterHomepageService extends BaseService { `; }; + + generate_error_html ({ message }) { + return ` + + + + + + +

${message}

+ + + `; + } } module.exports = { diff --git a/src/initgui.js b/src/initgui.js index eee07fb9..9622e7f8 100644 --- a/src/initgui.js +++ b/src/initgui.js @@ -286,6 +286,16 @@ window.initgui = async function(options){ } } + + //-------------------------------------------------------------------------------------- + // Display an error if the query parameters have an error + //-------------------------------------------------------------------------------------- + if ( window.url_query_params.has('error') ) { + // TODO: i18n + await UIAlert({ + message: window.url_query_params.get('message') + }); + } //-------------------------------------------------------------------------------------- // Get user referral code from URL query params