diff --git a/packages/insomnia/src/main/sentry.ts b/packages/insomnia/src/main/sentry.ts index 3ff83467625..bbb8d57d26b 100644 --- a/packages/insomnia/src/main/sentry.ts +++ b/packages/insomnia/src/main/sentry.ts @@ -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); } diff --git a/packages/insomnia/src/models/capture-exception.util.ts b/packages/insomnia/src/models/capture-exception.util.ts new file mode 100644 index 00000000000..6e0e7f91a7d --- /dev/null +++ b/packages/insomnia/src/models/capture-exception.util.ts @@ -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; +} diff --git a/packages/insomnia/src/models/index.ts b/packages/insomnia/src/models/index.ts index 387e3a57213..a75ab76a437 100644 --- a/packages/insomnia/src/models/index.ts +++ b/packages/insomnia/src/models/index.ts @@ -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(type: string, ...sources: Record[]): Promise { const model = getModel(type); @@ -186,6 +198,7 @@ export async function initModel(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(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(type: string, ...sources: R } } + assertModelWithParentId(migratedDoc, 'model.migrate after prune'); + // @ts-expect-error -- TSCONVERSION not sure why this error is occurring return migratedDoc; } diff --git a/packages/insomnia/src/models/websocket-response.ts b/packages/insomnia/src/models/websocket-response.ts index c1b6f514776..419899823ad 100644 --- a/packages/insomnia/src/models/websocket-response.ts +++ b/packages/insomnia/src/models/websocket-response.ts @@ -60,7 +60,7 @@ export function init(): BaseWebSocketResponse { }; } -export function migrate(doc: Response) { +export function migrate(doc: WebSocketResponse) { return doc; }