chore: add a log on the parentId set null (#6876)

* chore: add a log on the parentId set null

* add more logs

* fix: capture exception instead

* fix: add a hack

* chore: add comments

* fix: ensure process error from sentry

* rename fn

* docs: add a comment

* fix: sentry exception catcher load

---------

Co-authored-by: gatzjames <jamesgatzos@gmail.com>
This commit is contained in:
Mark Kim
2023-11-30 11:10:02 -05:00
committed by GitHub
parent a5d843171a
commit fc7ccf33fa
4 changed files with 41 additions and 2 deletions

View File

@@ -4,6 +4,7 @@ import type { SentryRequestType } from '@sentry/types';
import * as session from '../account/session';
import { ChangeBufferEvent, database as db } from '../common/database';
import { SENTRY_OPTIONS } from '../common/sentry';
import { ExceptionCallback, registerCaptureException } from '../models/capture-exception.util';
import * as models from '../models/index';
import { isSettings } from '../models/settings';
@@ -44,4 +45,8 @@ export function initializeSentry() {
...SENTRY_OPTIONS,
transport: ElectronSwitchableTransport,
});
// this is a hack for logging the sentry error synthetically made for database parent id null issue
// currently the database modules are used in the inso-cli as well as it uses NeDB (why?)
registerCaptureException(Sentry.captureException as ExceptionCallback);
}

View File

@@ -0,0 +1,19 @@
/**
* This is a HACK to work around inso cli using the database module used for Insomnia desktop client.
* Now this is getting coupled with Electron side, and CLI should not be really related to the electron at all.
* That is another tech debt.
*/
export type ExceptionCallback = (exception: unknown, captureContext?: unknown) => string;
let captureException: ExceptionCallback = (exception: unknown) => {
console.error(exception);
return '';
};
export function loadCaptureException() {
return captureException;
}
export function registerCaptureException(fn: ExceptionCallback) {
captureException = fn;
}

View File

@@ -16,6 +16,7 @@ import {
import { generateId } from '../common/misc';
import * as _apiSpec from './api-spec';
import * as _caCertificate from './ca-certificate';
import { loadCaptureException } from './capture-exception.util';
import * as _clientCertificate from './client-certificate';
import * as _cookieJar from './cookie-jar';
import * as _environment from './environment';
@@ -163,6 +164,17 @@ export function canDuplicate(type: string) {
return model ? model.canDuplicate : false;
}
const assertModelWithParentId = (model: BaseModel, info: string) => {
if ((model.type === 'Project' || model.type === 'Workspace') && !model.parentId) {
const msg = `[bug] parent id is set null unexpectedly ${model.type} - ${model._id}. ${info}`;
console.warn(msg);
const err = new Error(msg);
const capture = loadCaptureException();
capture(err);
}
};
export async function initModel<T extends BaseModel>(type: string, ...sources: Record<string, any>[]): Promise<T> {
const model = getModel(type);
@@ -186,6 +198,7 @@ export async function initModel<T extends BaseModel>(type: string, ...sources: R
model.init(),
);
const fullObject = Object.assign({}, objectDefaults, ...sources);
assertModelWithParentId(fullObject, 'initModel');
// Generate an _id if there isn't one yet
if (!fullObject._id) {
@@ -195,7 +208,7 @@ export async function initModel<T extends BaseModel>(type: string, ...sources: R
// Migrate the model
// NOTE: Do migration before pruning because we might need to look at those fields
const migratedDoc = model.migrate(fullObject);
assertModelWithParentId(migratedDoc, 'model.migrate');
// Prune extra keys from doc
for (const key of Object.keys(migratedDoc)) {
if (!objectDefaults.hasOwnProperty(key)) {
@@ -204,6 +217,8 @@ export async function initModel<T extends BaseModel>(type: string, ...sources: R
}
}
assertModelWithParentId(migratedDoc, 'model.migrate after prune');
// @ts-expect-error -- TSCONVERSION not sure why this error is occurring
return migratedDoc;
}

View File

@@ -60,7 +60,7 @@ export function init(): BaseWebSocketResponse {
};
}
export function migrate(doc: Response) {
export function migrate(doc: WebSocketResponse) {
return doc;
}