Skip to content

Commit

Permalink
Merge branch 'develop' into feat-support-json-schema-validation
Browse files Browse the repository at this point in the history
  • Loading branch information
kobenguyent authored Dec 5, 2023
2 parents fc10d53 + 13ee014 commit 8017182
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 6 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
10 changes: 6 additions & 4 deletions packages/insomnia/src/ui/routes/remote-collections.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,10 @@ const remoteCompareCache: Record<string, { ahead: number; behind: number }> = {}
const remoteBackendProjectsCache: Record<string, BackendProject[]> = {};

export const syncDataAction: ActionFunction = async ({ params }) => {
const { projectId, workspaceId } = params;
invariant(typeof projectId === 'string', 'Project Id is required');
invariant(typeof workspaceId === 'string', 'Workspace Id is required');
try {
const { projectId, workspaceId } = params;
invariant(typeof projectId === 'string', 'Project Id is required');
invariant(typeof workspaceId === 'string', 'Workspace Id is required');

const project = await models.project.getById(projectId);
invariant(project, 'Project not found');
invariant(project.remoteId, 'Project is not remote');
Expand All @@ -210,6 +209,9 @@ export const syncDataAction: ActionFunction = async ({ params }) => {
};
} catch (e) {
const errorMessage = e instanceof Error ? e.message : 'Unknown error while syncing data.';
delete remoteBranchesCache[workspaceId];
delete remoteCompareCache[workspaceId];
delete remoteBackendProjectsCache[workspaceId];
return {
error: errorMessage,
};
Expand Down

0 comments on commit 8017182

Please sign in to comment.