Skip to content

Commit

Permalink
#3040 - Apply suggestions from reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
nanoblit committed Sep 29, 2023
1 parent 430efdb commit 1f1b011
Show file tree
Hide file tree
Showing 43 changed files with 140 additions and 188 deletions.
8 changes: 0 additions & 8 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import {
Ketcher,
RemoteStructServiceProvider,
StructServiceProvider,
KetcherLogger,
LogLevel,
} from 'ketcher-core';
import { PolymerToggler } from './PolymerToggler';

Expand Down Expand Up @@ -74,12 +72,6 @@ const App = () => {
onInit={(ketcher: Ketcher) => {
window.ketcher = ketcher;

KetcherLogger.settings = {
enabled: true,
level: LogLevel.LOG,
showTrace: false,
};

window.parent.postMessage(
{
eventType: 'init',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ export function identifyStructFormat(
if (JSON.parse(sanitizedString)) {
return SupportedFormat.ket;
}
} catch (er) {
KetcherLogger.showExceptionLocation(
'identifyStructFormat.ts::identifyStructFromat',
);
} catch (e) {
KetcherLogger.error('identifyStructFormat.ts::identifyStructFromat', e);
} // eslint-disable-line

const isRXN = sanitizedString.includes('$RXN');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,14 @@ export class ServerFormatter implements StructFormatter {
);

return convertResult.struct;
} catch (error: any) {
} catch (e: any) {
let message;
if (error.message === 'Server is not compatible') {
if (e.message === 'Server is not compatible') {
message = `${formatProperties.name} is not supported.`;
} else {
message = `Convert error!\n${error.message || error}`;
message = `Convert error!\n${e.message || e}`;
}
KetcherLogger.showExceptionLocation(
'serverFormatter.ts::getStructureFromStructAsync',
);
KetcherLogger.error('serverFormatter.ts::getStructureFromStructAsync', e);
throw new Error(message);
}
}
Expand Down Expand Up @@ -111,12 +109,13 @@ export class ServerFormatter implements StructFormatter {
parsedStruct.rescale();
}
return parsedStruct;
} catch (error: any) {
if (error.message !== 'Server is not compatible') {
KetcherLogger.showExceptionLocation(
} catch (e: any) {
if (e.message !== 'Server is not compatible') {
KetcherLogger.error(
'serverFormatter.ts::getStructureFromStringAsync',
e,
);
throw Error(`Convert error!\n${error.message || error}`);
throw Error(`Convert error!\n${e.message || e}`);
}

const formatError =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class Render {
return this.options;
}
} catch (e) {
KetcherLogger.showExceptionLocation('raphaelRenderer.ts::updateOptions');
KetcherLogger.error('raphaelRenderer.ts::updateOptions', e);
console.log('Not a valid settings object');
}
return false;
Expand Down
4 changes: 1 addition & 3 deletions packages/ketcher-core/src/domain/entities/sgroupForest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,7 @@ export class SGroupForest {
assert(this.parent.has(id), 'sgid is not in the forest');
assert(this.children.has(id), 'sgid is not in the forest');
} catch (e) {
KetcherLogger.showExceptionLocation(
'sgroupForest.ts::SGroupForest::remove',
);
KetcherLogger.error('sgroupForest.ts::SGroupForest::remove', e);
console.info('error: sgid is not in the forest');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,10 @@ export class KetSerializer implements Serializer<Struct> {
let parsedFileContent: IKetMacromoleculesContent;
try {
parsedFileContent = JSON.parse(fileContent);
} catch (error) {
KetcherLogger.showExceptionLocation(
} catch (e) {
KetcherLogger.error(
'ketSerializer.ts::KetSerializer::parseAndValidateMacromolecules',
e,
);
editor.events.error.dispatch('Error during file parsing');
return { error: true };
Expand Down
20 changes: 7 additions & 13 deletions packages/ketcher-core/src/domain/serializers/mol/molSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ export class MolSerializer implements Serializer<Struct> {

try {
return molfile.parseCTFile(parseCTFileParams);
} catch (ex) {
KetcherLogger.showExceptionLocation(
'molSerializer::MolSerializer::deserialize',
);
} catch (e) {
KetcherLogger.error('molSerializer::MolSerializer::deserialize', e);
if (this.options.badHeaderRecover) {
try {
// check whether there's an extra empty line on top
Expand All @@ -59,10 +57,8 @@ export class MolSerializer implements Serializer<Struct> {
...parseCTFileParams,
molfileLines: lines.slice(1),
});
} catch (ex1) {
KetcherLogger.showExceptionLocation(
'molSerializer::MolSerializer::deserialize',
);
} catch (e1) {
KetcherLogger.error('molSerializer::MolSerializer::deserialize', e1);
}
try {
// check for a missing first line
Expand All @@ -71,13 +67,11 @@ export class MolSerializer implements Serializer<Struct> {
...parseCTFileParams,
molfileLines: [''].concat(lines),
});
} catch (ex2) {
KetcherLogger.showExceptionLocation(
'molSerializer::MolSerializer::deserialize',
);
} catch (e2) {
KetcherLogger.error('molSerializer::MolSerializer::deserialize', e2);
}
}
throw ex;
throw e;
}
}

Expand Down
10 changes: 4 additions & 6 deletions packages/ketcher-core/src/domain/serializers/mol/molfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,10 @@ export class Molfile {

try {
common.prepareForSaving[sgroup.type](sgroup, mol);
} catch (ex: any) {
KetcherLogger.showExceptionLocation(
'molfile.ts::Molfile::prepareSGroups',
);
if (!skipErrors || typeof ex.id !== 'number') {
throw new Error(`Error: ${ex.message}`);
} catch (e: any) {
KetcherLogger.error('molfile.ts::Molfile::prepareSGroups', e);
if (!skipErrors || typeof e.id !== 'number') {
throw new Error(`Error: ${e.message}`);
}
errorIgnore = true;
}
Expand Down
14 changes: 5 additions & 9 deletions packages/ketcher-core/src/domain/serializers/smi/smiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,9 @@ Smiles.prototype.saveMolecule = function (struct, ignoreErrors) {
if (sg.type === 'MUL') {
try {
SGroup.prepareMulForSaving(sg, struct);
} catch (ex) {
KetcherLogger.showExceptionLocation(
'smiles.js::Smiles.prototype.saveMolecule',
);
throw Error('Bad s-group (' + ex.message + ')');
} catch (error) {
KetcherLogger.error('smiles.js::Smiles.prototype.saveMolecule', error);
throw Error('Bad s-group (' + error.message + ')');
}
}
// 'SMILES data format doesn\'t support s-groups'
Expand Down Expand Up @@ -250,10 +248,8 @@ Smiles.prototype.saveMolecule = function (struct, ignoreErrors) {
this.atoms[atomIdx].chirality = 1;
} else this.atoms[atomIdx].chirality = 2;
});
} catch (ex) {
KetcherLogger.showExceptionLocation(
'smiles.js::Smiles.prototype.saveMolecule',
);
} catch (e) {
KetcherLogger.error('smiles.js::Smiles.prototype.saveMolecule', e);
// TODO: add error handler call
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,9 @@ function pollDeferred(process, complete, timeGap, startTimeGap) {
try {
if (complete(val)) resolve(val);
else setTimeout(iterate, timeGap);
} catch (e) {
KetcherLogger.showExceptionLocation(
'remoteStructService.ts::pollDeferred',
);
reject(e);
} catch (error) {
KetcherLogger.error('remoteStructService.ts::pollDeferred', error);
reject(error);
}
},
(err) => reject(err),
Expand Down Expand Up @@ -172,8 +170,9 @@ export class RemoteStructService implements StructService {
imagoVersions = response.imago_versions;
isAvailable = true;
} catch (e) {
KetcherLogger.showExceptionLocation(
KetcherLogger.error(
'remoteStructService.ts::RemoteStructService::info',
e,
);
indigoVersion = '';
imagoVersions = [];
Expand Down
31 changes: 19 additions & 12 deletions packages/ketcher-core/src/utilities/KetcherLogger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export enum LogLevel {
ERROR = 0,
WARN = 1,
LOG = 2,
INFO = 2,
LOG = 3,
}

export interface LogSettings {
Expand All @@ -27,42 +28,48 @@ export class KetcherLogger {
}
}

static log(message: unknown): void {
static log(...messages: unknown[]): void {
if (!this.isMinimumLogLevel(LogLevel.LOG)) {
return;
}

const { showTrace } = this.settings;

if (showTrace) {
window.console.trace(message);
window.console.trace(messages);
} else {
window.console.log(message);
window.console.log(messages);
}
}

static warn(warning: unknown): void {
if (!this.isMinimumLogLevel(LogLevel.WARN)) {
static info(...messages: unknown[]): void {
if (!this.isMinimumLogLevel(LogLevel.INFO)) {
return;
}

window.console.warn(warning);
const { showTrace } = this.settings;

if (showTrace) {
window.console.trace(messages);
} else {
window.console.info(messages);
}
}

static error(error: unknown): void {
if (!this.isMinimumLogLevel(LogLevel.ERROR)) {
static warn(...warnings: unknown[]): void {
if (!this.isMinimumLogLevel(LogLevel.WARN)) {
return;
}

window.console.error(error);
window.console.warn(warnings);
}

static showExceptionLocation(errorLocation: string): void {
static error(...errors: unknown[]): void {
if (!this.isMinimumLogLevel(LogLevel.ERROR)) {
return;
}

window.console.error(`An exception occured in: ${errorLocation}`);
window.console.error(errors);
}

private static isMinimumLogLevel(minimumLevel: LogLevel): boolean {
Expand Down
4 changes: 2 additions & 2 deletions packages/ketcher-core/src/utilities/runAsyncAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export const runAsyncAction = async <T = any>(
const res = await action();
eventEmitter.emit(KetcherAsyncEvents.SUCCESS);
return res;
} catch {
KetcherLogger.showExceptionLocation('runAsyncAction.ts::runAsyncAction');
} catch (e) {
KetcherLogger.error('runAsyncAction.ts::runAsyncAction', e);
eventEmitter.emit(KetcherAsyncEvents.FAILURE);
return undefined;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function* fetchData() {
yield call(fetchDataCall);
yield put(initSuccess(editorSlice));
} catch (e) {
KetcherLogger.showExceptionLocation('editorSaga.ts::fetchData');
KetcherLogger.error('editorSaga.ts::fetchData', e);
yield put(initFailure(editorSlice));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const normalizeStruct = (molV2000StringOrStruct: string | Struct) => {
try {
return new MolSerializer().deserialize(molV2000StringOrStruct);
} catch (e) {
KetcherLogger.showExceptionLocation('StructRenderer.tsx::normalizeStruct');
KetcherLogger.error('StructRenderer.tsx::normalizeStruct', e);
throw Error('Could not parse Struct');
}
};
Expand Down
6 changes: 2 additions & 4 deletions packages/ketcher-react/src/script/editor/tool/apoint.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ export async function editRGroupAttachmentPoint(
);
editor.update(action);
}
} catch (_error) {
KetcherLogger.showExceptionLocation(
'apoint.utils.ts::editRGroupAttachmentPoint',
);
} catch (e) {
KetcherLogger.error('apoint.utils.ts::editRGroupAttachmentPoint', e);
// close modal without any operations
}
}
4 changes: 2 additions & 2 deletions packages/ketcher-react/src/script/editor/tool/atom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ export function atomLongtapEvent(tool, render) {
: fromAtomAddition(render.ctab, dragCtx.xy0, newatom);
editor.update(action);
})
.catch(() => {
KetcherLogger.showExceptionLocation('atom.ts::atomLongtapEvent');
.catch((e) => {
KetcherLogger.error('atom.ts::atomLongtapEvent', e);
}); // w/o changes
}, 750);

Expand Down
4 changes: 2 additions & 2 deletions packages/ketcher-react/src/script/editor/tool/rgroupatom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ function propsDialog(editor, id, pos) {
editor.update(fromAtomsAttrs(editor.render.ctab, id, elem, false));
}
})
.catch(() => {
KetcherLogger.showExceptionLocation('rgroupatom.ts:propsDialog');
.catch((e) => {
KetcherLogger.error('rgroupatom.ts:propsDialog', e);
}); // w/o changes
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,8 @@ class RGroupFragmentTool implements Tool {

editor.update(action);
})
.catch(() => {
KetcherLogger.showExceptionLocation(
'rgroupfragment.ts::RGroupFragmentTool::click',
);
.catch((e) => {
KetcherLogger.error('rgroupfragment.ts::RGroupFragmentTool::click', e);
}); // w/o changes

return true;
Expand Down
6 changes: 2 additions & 4 deletions packages/ketcher-react/src/script/editor/tool/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,10 +467,8 @@ class SelectTool implements Tool {
editor.update(fromTextUpdating(struct, ci.id, content));
}
})
.catch(() => {
KetcherLogger.showExceptionLocation(
'select.ts::SelectTool::dblclick',
);
.catch((e) => {
KetcherLogger.error('select.ts::SelectTool::dblclick', e);
});
}
return true;
Expand Down
7 changes: 2 additions & 5 deletions packages/ketcher-react/src/script/editor/tool/sgroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,11 +538,8 @@ class SGroupTool implements Tool {
editor.selection(null);
}
})
.catch((error) => {
KetcherLogger.showExceptionLocation(
'sgroup.ts::SGroupTool::sgroupDialog',
);
console.error(error);
.catch((e) => {
KetcherLogger.error('sgroup.ts::SGroupTool::sgroupDialog', e);
});
}
}
Expand Down
Loading

0 comments on commit 1f1b011

Please sign in to comment.