Skip to content

Commit

Permalink
chore: add a log on the parentId set null (#6876)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
marckong and gatzjames authored Nov 30, 2023
1 parent a5d8431 commit fc7ccf3
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/insomnia/src/main/sentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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);
}
19 changes: 19 additions & 0 deletions packages/insomnia/src/models/capture-exception.util.ts
Original file line number Diff line number Diff line change
@@ -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;
}
17 changes: 16 additions & 1 deletion packages/insomnia/src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);

Expand All @@ -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) {
Expand All @@ -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)) {
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/insomnia/src/models/websocket-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function init(): BaseWebSocketResponse {
};
}

export function migrate(doc: Response) {
export function migrate(doc: WebSocketResponse) {
return doc;
}

Expand Down

0 comments on commit fc7ccf3

Please sign in to comment.