mirror of
https://github.com/tiddly-gittly/TidGi-Desktop.git
synced 2026-01-20 19:41:39 -08:00
fix: fix json while it errors
This commit is contained in:
parent
f7df1ca5df
commit
13bd7fa83b
2 changed files with 33 additions and 13 deletions
|
|
@ -4,6 +4,20 @@ import { parse as bestEffortJsonParser } from 'best-effort-json-parser';
|
|||
import { SETTINGS_FOLDER } from '@/constants/appPaths';
|
||||
import { logger } from '@services/libs/log';
|
||||
|
||||
export function fixSettingFileWhenError(jsonError: Error): void {
|
||||
logger.error('Setting file format bad: ' + jsonError.message);
|
||||
const jsonContent = fs.readFileSync(settings.file(), 'utf-8');
|
||||
logger.info('Try to fix JSON content.');
|
||||
try {
|
||||
const repaired = bestEffortJsonParser(jsonContent) as Record<string, unknown>;
|
||||
logger.info('Fix JSON content done, writing it.');
|
||||
fs.writeJSONSync(settings.file(), repaired);
|
||||
logger.info('Fix JSON content done, saved', { repaired });
|
||||
} catch (fixJSONError) {
|
||||
logger.error('Setting file format bad, and cannot be fixed: ' + (fixJSONError as Error).message, { jsonContent });
|
||||
}
|
||||
}
|
||||
|
||||
settings.configure({
|
||||
dir: SETTINGS_FOLDER,
|
||||
atomicSave: process.platform !== 'win32',
|
||||
|
|
@ -15,16 +29,6 @@ if (fs.existsSync(settings.file())) {
|
|||
fs.readJsonSync(settings.file());
|
||||
logger.info('Setting file format good.');
|
||||
} catch (jsonError) {
|
||||
logger.error('Setting file format bad: ' + (jsonError as Error).message);
|
||||
const jsonContent = fs.readFileSync(settings.file(), 'utf-8');
|
||||
logger.info('Try to fix JSON content.');
|
||||
try {
|
||||
const repaired = bestEffortJsonParser(jsonContent) as Record<string, unknown>;
|
||||
logger.info('Fix JSON content done, writing it.');
|
||||
fs.writeJSONSync(settings.file(), repaired);
|
||||
logger.info('Fix JSON content done, saved', { repaired });
|
||||
} catch (fixJSONError) {
|
||||
logger.error('Setting file format bad, and cannot be fixed: ' + (fixJSONError as Error).message, { jsonContent });
|
||||
}
|
||||
fixSettingFileWhenError(jsonError as Error);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
||||
/* eslint-disable @typescript-eslint/require-await */
|
||||
/* eslint-disable unicorn/no-null */
|
||||
import { injectable } from 'inversify';
|
||||
|
|
@ -26,9 +27,18 @@ import i18n from '@services/libs/i18n';
|
|||
import { defaultServerIP } from '@/constants/urls';
|
||||
import { logger } from '@services/libs/log';
|
||||
import { workspaceSorter } from './utils';
|
||||
import { fixSettingFileWhenError } from '@/helpers/configSetting';
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
||||
const debouncedSetSettingFile = debounce(async (workspaces: Record<string, IWorkspace>) => await settings.set(`workspaces`, workspaces as any), 500);
|
||||
const debouncedSetSettingFile = debounce(async (workspaces: Record<string, IWorkspace>) => {
|
||||
try {
|
||||
await settings.set(`workspaces`, workspaces as any);
|
||||
} catch (error) {
|
||||
logger.error('Setting file format bad in debouncedSetSettingFile, will try again', { workspaces });
|
||||
fixSettingFileWhenError(error as Error);
|
||||
await settings.set(`workspaces`, workspaces as any);
|
||||
}
|
||||
}, 500);
|
||||
|
||||
@injectable()
|
||||
export class Workspace implements IWorkspaceService {
|
||||
|
|
@ -212,7 +222,13 @@ export class Workspace implements IWorkspaceService {
|
|||
this.workspaces[id] = this.sanitizeWorkspace(workspace);
|
||||
await this.reactBeforeWorkspaceChanged(workspace);
|
||||
if (immediate === true) {
|
||||
await settings.set(`workspaces.${id}`, { ...workspace });
|
||||
try {
|
||||
await settings.set(`workspaces.${id}`, { ...workspace });
|
||||
} catch (error) {
|
||||
logger.error('Setting file format bad in public async set, will try again', { workspace });
|
||||
fixSettingFileWhenError(error as Error);
|
||||
await settings.set(`workspaces.${id}`, { ...workspace });
|
||||
}
|
||||
} else {
|
||||
void debouncedSetSettingFile(this.workspaces);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue