Enhance error handling in setupHandler.js for improved robustness

- Replaced assertions with explicit error throwing for missing mainWindow and session.
- Updated protocol handler to return a 400 response for unsupported HTTP requests, enhancing security and clarity.
- Improved internal server error response formatting.
This commit is contained in:
Luke Hagar
2025-07-23 16:00:31 -05:00
parent cfd43bac1c
commit 59bcd5b3a7

View File

@@ -179,8 +179,13 @@ export async function createRequest(request, session) {
* @type {import('./setupHandler.d').setupHandler}
*/
export async function setupHandler(mainWindow) {
assert(mainWindow, 'mainWindow is required');
assert(mainWindow.webContents.session, 'mainWindow.webContents.session is required');
if (!mainWindow) {
throw new Error('mainWindow is required for setupHandler');
}
if (!mainWindow.webContents?.session) {
throw new Error('mainWindow.webContents.session is required for setupHandler');
}
let url = process.env.VITE_DEV_SERVER || Origin
@@ -211,7 +216,12 @@ export async function setupHandler(mainWindow) {
// Handle all http://127.0.0.1 requests
protocol.handle(Protocol, async (request) => {
assert(request.url.startsWith(url), 'External HTTP not supported, use HTTPS');
if (!request.url.startsWith(url)) {
return new Response('External HTTP not supported, use HTTPS instead', {
status: 400,
headers: { 'content-type': 'text/plain' }
});
}
const req = await createRequest(request, mainWindow.webContents.session);