/** * Error codes shared by the main process and the window. * * The main process never produces text meant for the player. It throws an AppError carrying a * code and optional parameters; the code crosses IPC inside Error.message (Electron passes only * the message of a rejected invoke), and the window shows the translation of `errors.`. * The underlying exception goes to the main process log. * * Dependency-free so the sandboxed preload could import it. */ export const APP_ERROR_CODES = [ /** A bug of ours: a handler returned something its contract does not allow. */ 'internal_error', 'app_update_unavailable', 'app_update_busy', 'app_update_launcher_busy', 'app_update_download_failed', 'app_update_installer_invalid', 'app_update_install_failed', /** The launcher's own settings file could not be written. */ 'settings_write_failed', 'logs_folder_open_failed', 'diagnostics_export_failed', 'copy_not_found', 'tuning_invalid', 'mods_read_only', 'mods_write_failed', 'mods_preset_invalid', 'mods_blocking_issues', 'mods_folder_invalid', 'save_not_found', 'save_archive_invalid', 'save_official_invalid', 'save_official_location', 'save_read_only', 'save_operation_failed', 'save_login_invalid', 'save_login_exists', 'save_password_invalid', 'save_nick_invalid', 'save_avatar_invalid', 'save_level_invalid', 'copies_write_failed', 'maintenance_busy', 'maintenance_location', 'maintenance_no_update', 'maintenance_cancelled', 'maintenance_failed', 'maintenance_source_changed', 'copy_location_in_use', 'decal_source_invalid', 'decal_import_failed', 'launch_no_copy', 'launch_busy', 'launch_failed', 'launch_platform_unsupported', 'launch_lab_required', 'launch_lab_missing', 'launch_lab_version', 'launch_lab_failed', 'launch_game_running', 'launch_copy_in_steam', 'launch_steam_confirmation_required', 'launch_location', 'launch_stock_version', /** The wtoffline/stock backup executable is missing or unreadable before launch. */ 'launch_stock_missing', 'launch_build_failed', 'launch_build_unavailable', 'launch_port_busy', 'launch_server_failed', 'launch_server_exited', 'launch_game_failed', /** Core files built for the launch (aces.exe, wto_ws.dll, wto_ws.cfg) are missing or unreadable. */ 'launch_game_files_missing', /** A folder the player chose does not exist or cannot be read (params: path). */ 'folder_unreadable', /** The chosen folder lacks the files of a War Thunder installation (params: path). */ 'not_a_game_folder', /** A patched client lacking wtoffline/stock needs an unpatched original game folder. */ 'stock_source_invalid', /** The chosen original game folder has a different version than the patched client. */ 'stock_source_version_mismatch', /** The folder chosen for the copy cannot take it (params: problem, one of TARGET_PROBLEMS). */ 'target_not_usable', /** A copy or data folder is inside a Steam library and the player has not confirmed the warning. */ 'steam_library_not_confirmed', /** The target drive has too little room (params: neededGb, freeGb). */ 'not_enough_space', /** Patches the copy cannot run safely without do not fit this game build (params: patches). */ 'essential_patches_unavailable', /** Some patches do not fit and the player has not chosen to go on without their features. */ 'missing_features_not_accepted', /** Working out which patches fit failed; the details are in the log. */ 'patch_check_failed', /** The packaged launcher itself sits inside the selected stock War Thunder folder. */ 'launcher_inside_source', /** Copying the game failed; what was copied has been removed. */ 'copy_failed', 'hardlink_unavailable', 'hardlink_no_match', 'hardlink_file_changed', /** The player stopped the installation; what was copied has been removed. */ 'install_cancelled', /** An installation is already running. */ 'install_busy', ] as const; export type AppErrorCode = (typeof APP_ERROR_CODES)[number]; export type AppErrorParams = Readonly>; const MARKER = 'wto-app-error:'; export class AppError extends Error { readonly code: AppErrorCode; readonly params: AppErrorParams; constructor(code: AppErrorCode, params: AppErrorParams = {}) { super(MARKER + JSON.stringify({ code, params })); this.name = 'AppError'; this.code = code; this.params = params; } } /** The code and parameters an AppError put into a message, or null for any other error. */ export function parseAppError(message: string): { code: AppErrorCode; params: AppErrorParams } | null { const start = message.indexOf(MARKER); if (start === -1) return null; try { const value = JSON.parse(message.slice(start + MARKER.length)) as { code?: unknown; params?: unknown }; if (!(APP_ERROR_CODES as readonly unknown[]).includes(value.code)) return null; const params = value.params && typeof value.params === 'object' ? (value.params as AppErrorParams) : {}; return { code: value.code as AppErrorCode, params }; } catch { return null; } }