Files
WT-Offline/apps/launcher/src/shared/errors.ts
T
N0VAN0M4LY 5d1d7001b8 feat(saves): edit account levels and avatars
Read rank thresholds and available avatar artwork from the selected copy. Persist avatar selections in the player profile and preserve free research points when a level changes.
2026-11-29 18:19:05 +08:00

107 lines
3.8 KiB
TypeScript

/**
* 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.<code>`.
* 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',
/** 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',
'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',
'launch_no_copy',
'launch_busy',
'launch_lab_required',
'launch_lab_missing',
'launch_lab_version',
'launch_lab_failed',
'launch_game_running',
'launch_copy_in_steam',
'launch_build_failed',
'launch_build_unavailable',
'launch_port_busy',
'launch_server_exited',
/** 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',
/** The folder chosen for the copy cannot take it (params: problem, one of TARGET_PROBLEMS). */
'target_not_usable',
/** The game copy would go into 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',
/** Copying the game failed; what was copied has been removed. */
'copy_failed',
/** 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<Record<string, string | number>>;
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;
}
}