Skip to content

Commit

Permalink
Send real errors to telemetry
Browse files Browse the repository at this point in the history
Every call to `telemetry.sendError` contained the same boilerplate code
to create an object with the property `error`, which contains a string
representation of the real error. This change moves this conversion into
the `Telemetry` implementation. This reduces the need for boilerplate
code in other places.

This change also affects `monaco-yaml`. Because the telemetry now
resolves the real error, the browser can log the error to the console
with a better stack trace.
  • Loading branch information
remcohaszing committed Jul 7, 2024
1 parent f039273 commit 10dc8e6
Show file tree
Hide file tree
Showing 10 changed files with 15 additions and 21 deletions.
3 changes: 1 addition & 2 deletions src/languageserver/handlers/settingsHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*--------------------------------------------------------------------------------------------*/
import { configure as configureHttpRequests, xhr } from 'request-light';
import { Connection, DidChangeConfigurationNotification, DocumentFormattingRequest } from 'vscode-languageserver';
import { convertErrorToTelemetryMsg } from '../../languageservice/utils/objects';
import { isRelativePath, relativeToAbsolutePath } from '../../languageservice/utils/paths';
import { checkSchemaURI, JSON_SCHEMASTORE_URL, KUBERNETES_SCHEMA_URL } from '../../languageservice/utils/schemaUrls';
import { LanguageService, LanguageSettings, SchemaPriority } from '../../languageservice/yamlLanguageService';
Expand All @@ -28,7 +27,7 @@ export class SettingsHandler {
// Register for all configuration changes.
await this.connection.client.register(DidChangeConfigurationNotification.type);
} catch (err) {
this.telemetry.sendError('yaml.settings.error', { error: convertErrorToTelemetryMsg(err) });
this.telemetry.sendError('yaml.settings.error', err);
}
}
this.connection.onDidChangeConfiguration(() => this.pullConfiguration());
Expand Down
5 changes: 3 additions & 2 deletions src/languageserver/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import { Connection } from 'vscode-languageserver';
import { TelemetryEvent, Telemetry } from '../languageservice/telemetry';
import { convertErrorToTelemetryMsg } from '../languageservice/utils/objects';

export class TelemetryImpl implements Telemetry {
constructor(private readonly connection: Connection) {}
Expand All @@ -13,8 +14,8 @@ export class TelemetryImpl implements Telemetry {
this.connection.telemetry.logEvent(event);
}

sendError(name: string, properties: unknown): void {
this.send({ name, type: 'track', properties: properties });
sendError(name: string, error: unknown): void {
this.send({ name, type: 'track', properties: { error: convertErrorToTelemetryMsg(error) } });
}

sendTrack(name: string, properties: unknown): void {
Expand Down
5 changes: 2 additions & 3 deletions src/languageservice/services/documentSymbols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { TextDocument } from 'vscode-languageserver-textdocument';
import { yamlDocumentsCache } from '../parser/yaml-documents';
import { Telemetry } from '../telemetry';
import { isMap, isSeq, Node } from 'yaml';
import { convertErrorToTelemetryMsg } from '../utils/objects';

export class YAMLDocumentSymbols {
private jsonDocumentSymbols;
Expand Down Expand Up @@ -53,7 +52,7 @@ export class YAMLDocumentSymbols {
}
}
} catch (err) {
this.telemetry?.sendError('yaml.documentSymbols.error', { error: convertErrorToTelemetryMsg(err) });
this.telemetry?.sendError('yaml.documentSymbols.error', err);
}
return results;
}
Expand All @@ -75,7 +74,7 @@ export class YAMLDocumentSymbols {
}
}
} catch (err) {
this.telemetry?.sendError('yaml.hierarchicalDocumentSymbols.error', { error: convertErrorToTelemetryMsg(err) });
this.telemetry?.sendError('yaml.hierarchicalDocumentSymbols.error', err);
}

return results;
Expand Down
3 changes: 1 addition & 2 deletions src/languageservice/services/yamlCodeLens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { YAMLSchemaService } from './yamlSchemaService';
import { JSONSchema } from '../jsonSchema';
import { Telemetry } from '../telemetry';
import { getSchemaUrls } from '../utils/schemaUrls';
import { convertErrorToTelemetryMsg } from '../utils/objects';
import { getSchemaTitle } from '../utils/schemaUtils';

export class YamlCodeLens {
Expand Down Expand Up @@ -39,7 +38,7 @@ export class YamlCodeLens {
result.push(lens);
}
} catch (err) {
this.telemetry?.sendError('yaml.codeLens.error', { error: convertErrorToTelemetryMsg(err) });
this.telemetry?.sendError('yaml.codeLens.error', err);
}

return result;
Expand Down
6 changes: 3 additions & 3 deletions src/languageservice/services/yamlCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { YAMLSchemaService } from './yamlSchemaService';
import { ResolvedSchema } from 'vscode-json-languageservice/lib/umd/services/jsonSchemaService';
import { JSONSchema, JSONSchemaRef } from '../jsonSchema';
import { stringifyObject, StringifySettings } from '../utils/json';
import { convertErrorToTelemetryMsg, isDefined, isString } from '../utils/objects';
import { isDefined, isString } from '../utils/objects';
import * as nls from 'vscode-nls';
import { setKubernetesParserOption } from '../parser/isKubernetes';
import { asSchema } from '../parser/jsonParser07';
Expand Down Expand Up @@ -281,7 +281,7 @@ export class YamlCompletion {
}
},
error: (message: string) => {
this.telemetry?.sendError('yaml.completion.error', { error: convertErrorToTelemetryMsg(message) });
this.telemetry?.sendError('yaml.completion.error', message);
},
log: (message: string) => {
console.log(message);
Expand Down Expand Up @@ -533,7 +533,7 @@ export class YamlCompletion {
const types: { [type: string]: boolean } = {};
this.getValueCompletions(schema, currentDoc, node, offset, document, collector, types, doComplete);
} catch (err) {
this.telemetry?.sendError('yaml.completion.error', { error: convertErrorToTelemetryMsg(err) });
this.telemetry?.sendError('yaml.completion.error', err);
}

this.finalizeParentCompletion(result);
Expand Down
3 changes: 1 addition & 2 deletions src/languageservice/services/yamlDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { isAlias } from 'yaml';
import { Telemetry } from '../telemetry';
import { yamlDocumentsCache } from '../parser/yaml-documents';
import { matchOffsetToDocument } from '../utils/arrUtils';
import { convertErrorToTelemetryMsg } from '../utils/objects';
import { TextBuffer } from '../utils/textBuffer';

export class YamlDefinition {
Expand All @@ -33,7 +32,7 @@ export class YamlDefinition {
}
}
} catch (err) {
this.telemetry?.sendError('yaml.definition.error', { error: convertErrorToTelemetryMsg(err) });
this.telemetry?.sendError('yaml.definition.error', err);
}

return undefined;
Expand Down
3 changes: 1 addition & 2 deletions src/languageservice/services/yamlHover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { JSONSchema } from '../jsonSchema';
import { URI } from 'vscode-uri';
import * as path from 'path';
import { Telemetry } from '../telemetry';
import { convertErrorToTelemetryMsg } from '../utils/objects';
import { ASTNode } from 'vscode-json-languageservice';
import { stringify as stringifyYAML } from 'yaml';

Expand Down Expand Up @@ -55,7 +54,7 @@ export class YAMLHover {
currentDoc.currentDocIndex = currentDocIndex;
return this.getHover(document, position, currentDoc);
} catch (error) {
this.telemetry?.sendError('yaml.hover.error', { error: convertErrorToTelemetryMsg(error) });
this.telemetry?.sendError('yaml.hover.error', error);
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/languageservice/services/yamlLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { DocumentLink } from 'vscode-languageserver-types';
import { TextDocument } from 'vscode-languageserver-textdocument';
import { Telemetry } from '../telemetry';
import { yamlDocumentsCache } from '../parser/yaml-documents';
import { convertErrorToTelemetryMsg } from '../utils/objects';

export class YamlLinks {
constructor(private readonly telemetry?: Telemetry) {}
Expand All @@ -23,7 +22,7 @@ export class YamlLinks {
// Wait for all the promises to return and then flatten them into one DocumentLink array
return Promise.all(linkPromises).then((yamlLinkArray) => [].concat(...yamlLinkArray));
} catch (err) {
this.telemetry?.sendError('yaml.documentLink.error', { error: convertErrorToTelemetryMsg(err) });
this.telemetry?.sendError('yaml.documentLink.error', err);
}
}
}
3 changes: 1 addition & 2 deletions src/languageservice/services/yamlValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { JSONValidation } from 'vscode-json-languageservice/lib/umd/services/jso
import { YAML_SOURCE } from '../parser/jsonParser07';
import { TextBuffer } from '../utils/textBuffer';
import { yamlDocumentsCache } from '../parser/yaml-documents';
import { convertErrorToTelemetryMsg } from '../utils/objects';
import { Telemetry } from '../telemetry';
import { AdditionalValidator } from './validation/types';
import { UnusedAnchorsValidator } from './validation/unused-anchors';
Expand Down Expand Up @@ -107,7 +106,7 @@ export class YAMLValidation {
index++;
}
} catch (err) {
this.telemetry?.sendError('yaml.validation.error', { error: convertErrorToTelemetryMsg(err) });
this.telemetry?.sendError('yaml.validation.error', err);
}

let previousErr: Diagnostic;
Expand Down
2 changes: 1 addition & 1 deletion src/languageservice/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface TelemetryEvent {
export interface Telemetry {
send(event: TelemetryEvent): void;

sendError(name: string, properties: unknown): void;
sendError(name: string, error: unknown): void;

sendTrack(name: string, properties: unknown): void;
}

0 comments on commit 10dc8e6

Please sign in to comment.