Skip to content

Commit

Permalink
feat: add querystring-informed errors
Browse files Browse the repository at this point in the history
  • Loading branch information
KernelDeimos committed Jun 19, 2024
1 parent 616f28d commit e7c0b83
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 2 deletions.
19 changes: 19 additions & 0 deletions packages/backend/src/api/APIError.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
const { URLSearchParams } = require("node:url");
const { quot } = require("../util/strutil");

/**
Expand Down Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/routers/_default.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
54 changes: 53 additions & 1 deletion packages/backend/src/services/PuterHomepageService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down Expand Up @@ -271,6 +291,38 @@ class PuterHomepageService extends BaseService {
</html>`;
};

generate_error_html ({ message }) {
return `
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
@font-face {
font-family: 'Inter';
src: url('/fonts/Inter-Thin.ttf') format('truetype');
font-weight: 100;
}
BODY {
box-sizing: border-box;
margin: 0;
height: 100vh;
width: 100vw;
background-color: #2f70ab;
color: #f2f7f7;
font-family: "Inter", "Helvetica Neue", HelveticaNeue, Helvetica, Arial, sans-serif;
display: flex;
align-items: center;
justify-content: center;
}
</style>
</head>
<body>
<h1>${message}</h1>
</body>
</html>
`;
}
}

module.exports = {
Expand Down
10 changes: 10 additions & 0 deletions src/initgui.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e7c0b83

Please sign in to comment.