Skip to content
1 change: 1 addition & 0 deletions src/components/tests/header.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ describe( 'Header', () => {
message:
"Please verify your site's local path directory contains the standard WordPress installation files and try again. If this problem persists, please contact support.",
error,
showOpenLogs: true,
} );
} );
} );
Expand Down
1 change: 1 addition & 0 deletions src/hooks/use-site-details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ export function SiteDetailsProvider( { children }: SiteDetailsProviderProps ) {
"Please verify your site's local path directory contains the standard WordPress installation files and try again. If this problem persists, please contact support."
),
error,
showOpenLogs: true,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm enabling a new button for cases in which Studio can't start the site.

Will add more under https://github.com/Automattic/dotcom-forge/issues/10143

} );
await getIpcApi().stopServer( id );
}
Expand Down
23 changes: 18 additions & 5 deletions src/ipc-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import { sanitizeForLogging } from './lib/sanitize-for-logging';
import { sortSites } from './lib/sort-sites';
import { installSqliteIntegration, keepSqliteIntegrationUpdated } from './lib/sqlite-versions';
import * as windowsHelpers from './lib/windows-helpers';
import { writeLogToFile, type LogLevel } from './logging';
import { getLogsFilePath, writeLogToFile, type LogLevel } from './logging';
import { popupMenu, setupMenu } from './menu';
import { SiteServer, createSiteWorkingDirectory } from './site-server';
import { DEFAULT_SITE_PATH, getResourcesPath, getSiteThumbnailPath } from './storage/paths';
Expand Down Expand Up @@ -927,19 +927,32 @@ export async function showMessageBox(

export async function showErrorMessageBox(
event: IpcMainInvokeEvent,
{ title, message, error }: { title: string; message: string; error?: unknown }
{
title,
message,
error,
showOpenLogs = false,
}: { title: string; message: string; error?: unknown; showOpenLogs?: boolean }
) {
// Remove prepended error message added by IPC handler
const filteredError = ( error as Error )?.message?.replace(
/Error invoking remote method '\w+': Error:/g,
''
);
await showMessageBox( event, {
const response = await showMessageBox( event, {
type: 'error',
message: title,
detail: error ? `${ message }\n\nError: ${ filteredError }` : message,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's bring it back so the specific error can still be appended, but let's drop the 'Error: '.

buttons: [ __( 'OK' ) ],
detail: error ? `${ message }\n\n${ filteredError }` : message,
buttons: [ ...( showOpenLogs ? [ __( 'Open Studio Logs' ) ] : [] ), __( 'OK' ) ],
} );

if ( showOpenLogs && response.response === 0 ) {
const logFilePath = getLogsFilePath();
const err = await shell.openPath( logFilePath );
if ( err ) {
console.error( `Error opening logs file: ${ logFilePath } ${ err }` );
}
}
}

export async function showNotification(
Expand Down
10 changes: 10 additions & 0 deletions src/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from 'path';
import * as FileStreamRotator from 'file-stream-rotator';

let logStream: ReturnType< typeof FileStreamRotator.getStream > | null = null;
let currentLogFile = '';

// Intentional typo of 'erro' so all levels the same number of characters
export type LogLevel = 'info' | 'warn' | 'erro';
Expand Down Expand Up @@ -37,6 +38,8 @@ export function setupLogging( {
verbose: true, // file-stream-rotator itself will log to console too
} );

currentLogFile = path.join( logDir, 'current.log' );

const makeLogger =
( level: LogLevel, originalLogger: typeof console.log ) =>
( ...args: Parameters< typeof console.log > ) => {
Expand Down Expand Up @@ -99,3 +102,10 @@ function formatLogMessageArg( arg: unknown ): string {

return JSON.stringify( arg, null, 2 );
}

export function getLogsFilePath(): string {
if ( ! currentLogFile ) {
throw new Error( 'Logging system not initialized' );
}
return currentLogFile;
}