-
Notifications
You must be signed in to change notification settings - Fork 573
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: abridge call graph creation error messages in analytics #1279
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know. But it seems to round it when used in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be better to round it before, less JS magic |
||
return msg.slice(0, toKeep) + e + msg.slice(msg.length - toKeep, msg.length); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should go also to the test file? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Testing that method would be quite involved, and the functional tests for abridging the string should cover a reasonable number of cases. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be nice to say why we are doing this trimming with a comment There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is pretty self-explanatory from the code, no? E.g. |
||
err.innerError.toString(), | ||
ANALYTICS_PAYLOAD_MAX_LENGTH, | ||
), | ||
); | ||
alerts.registerAlerts([ | ||
{ | ||
type: 'error', | ||
|
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) => { | ||
muscar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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'); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe have it as an optional param on line 4?
ellipsis = '...': string
,There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that supported by TypeScript?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional parameters? in JS and TS