Skip to content

Commit

Permalink
fix: abridge call graph creation error messages in analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
muscar committed Jul 22, 2020
1 parent c70c657 commit 7a68ad3
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/lib/error-format.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export function abridgeErrorMessage(
msg: string,
maxLen: number,
ellipsis?: string,
): string {
if (msg.length <= maxLen) {
return msg;
}
const e = ellipsis || ' ... ';
const toKeep = (maxLen - e.length) / 2;
return msg.slice(0, toKeep) + e + msg.slice(msg.length - toKeep, msg.length);
}
11 changes: 10 additions & 1 deletion src/lib/monitor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,12 @@ import {
} from './utils';
import { countPathsToGraphRoot } from '../utils';
import * as alerts from '../alerts';
import { abridgeErrorMessage } from '../error-format';

const debug = Debug('snyk');

const ANALYTICS_PAYLOAD_MAX_LENGTH = 1024;

// TODO(kyegupov): clean up the type, move to snyk-cli-interface repository

interface MonitorBody {
Expand Down Expand Up @@ -220,7 +223,13 @@ async function monitorDepTree(
let callGraphPayload;
if (options.reachableVulns && scannedProject.callGraph?.innerError) {
const err = scannedProject.callGraph as CallGraphError;
analytics.add('callGraphError', err.innerError.toString());
analytics.add(
'callGraphError',
abridgeErrorMessage(
err.innerError.toString(),
ANALYTICS_PAYLOAD_MAX_LENGTH,
),
);
alerts.registerAlerts([
{
type: 'error',
Expand Down
11 changes: 10 additions & 1 deletion src/lib/snyk-test/run-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,12 @@ import { assembleIacLocalPayloads, parseIacTestResult } from './run-iac-test';
import { Payload, PayloadBody, DepTreeFromResolveDeps } from './types';
import { CallGraphError } from '@snyk/cli-interface/legacy/common';
import * as alerts from '../alerts';
import { abridgeErrorMessage } from '../error-format';

const debug = debugModule('snyk');

const ANALYTICS_PAYLOAD_MAX_LENGTH = 1024;

export = runTest;

async function sendAndParseResults(
Expand Down Expand Up @@ -520,7 +523,13 @@ async function assembleLocalPayloads(
(scannedProject.callGraph as CallGraphError).innerError
) {
const err = scannedProject.callGraph as CallGraphError;
analytics.add('callGraphError', err.innerError.toString());
analytics.add(
'callGraphError',
abridgeErrorMessage(
err.innerError.toString(),
ANALYTICS_PAYLOAD_MAX_LENGTH,
),
);
alerts.registerAlerts([
{
type: 'error',
Expand Down
22 changes: 22 additions & 0 deletions test/error-format.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { test } from 'tap';
import { abridgeErrorMessage } from '../src/lib/error-format';

test('abridge empty string', async (t) => {
t.equal(abridgeErrorMessage('', 10), '');
});

test('abridge shorter than max length', async (t) => {
t.equal(abridgeErrorMessage('hello', 10), 'hello');
});

test('abridge same length as max length', async (t) => {
t.equal(abridgeErrorMessage('hello', 5), 'hello');
});

test('abridge longer than max length', async (t) => {
t.equal(abridgeErrorMessage('hello there', 10), 'he ... ere');
});

test('abridge longer than max length (custom ellipsis)', async (t) => {
t.equal(abridgeErrorMessage('hello there', 10, '--'), 'hell--here');
});

0 comments on commit 7a68ad3

Please sign in to comment.