From 8f2ca666775c701bd51f5cbccdc0d1f02664bc70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Pupier?= Date: Wed, 22 Jul 2020 18:08:05 +0200 Subject: [PATCH 1/2] Catch errors that can be thrown by registered schema providers #323 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit in case, one of the registered is throwing an error, it avoids to break the other registered ones. Signed-off-by: Aurélien Pupier --- src/schema-extension-api.ts | 30 +++++++++++++++++------------- test/schemaProvider.test.ts | 26 +++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/src/schema-extension-api.ts b/src/schema-extension-api.ts index c61d17c7..bebf3aa3 100644 --- a/src/schema-extension-api.ts +++ b/src/schema-extension-api.ts @@ -92,24 +92,28 @@ class SchemaExtensionAPI implements ExtensionAPI { public requestCustomSchema(resource: string): string[] { const matches = []; for (let customKey of Object.keys(this._customSchemaContributors)) { - const contributor = this._customSchemaContributors[customKey]; - let uri: string; - if (contributor.label && workspace.textDocuments) { - const labelRegexp = new RegExp(contributor.label, 'g'); - for (const doc of workspace.textDocuments) { - if (doc.uri.toString() === resource) { - if (labelRegexp.test(doc.getText())) { - uri = contributor.requestSchema(resource); - return [uri]; + try { + const contributor = this._customSchemaContributors[customKey]; + let uri: string; + if (contributor.label && workspace.textDocuments) { + const labelRegexp = new RegExp(contributor.label, 'g'); + for (const doc of workspace.textDocuments) { + if (doc.uri.toString() === resource) { + if (labelRegexp.test(doc.getText())) { + uri = contributor.requestSchema(resource); + return [uri]; + } } } } - } - uri = contributor.requestSchema(resource); + uri = contributor.requestSchema(resource); - if (uri) { - matches.push(uri); + if (uri) { + matches.push(uri); + } + } catch (error) { + console.log(`Error thrown while requesting schema ` + error); } } return matches; diff --git a/test/schemaProvider.test.ts b/test/schemaProvider.test.ts index 03180a19..813e85d7 100644 --- a/test/schemaProvider.test.ts +++ b/test/schemaProvider.test.ts @@ -5,7 +5,6 @@ import * as vscode from 'vscode'; import { getDocUri, activate, testCompletion, testHover, testDiagnostics, sleep } from './helper'; -import { Uri } from 'vscode'; describe('Tests for schema provider feature', () => { const docUri = getDocUri('completion/completion.yaml'); @@ -134,6 +133,23 @@ describe('Tests for schema provider feature', () => { } ] }); + }); + + it('Multiple contributors with one throwing an error', async () => { + const client = await activate(docUri); + client._customSchemaContributors = {}; + client.registerContributor(SCHEMA2, onRequestSchema2URI, onRequestSchema2Content); + client.registerContributor("schemathrowingerror", onRequestSchemaURIThrowError, onRequestSchemaContentThrowError); + + await testCompletion(docUri, new vscode.Position(0, 0), { + items: [ + { + label: "apple", + kind: 9, + documentation: "An apple" + } + ] + }); }); }); @@ -161,6 +177,14 @@ function onRequestSchema1URI(resource: string): string | undefined { return undefined; } +function onRequestSchemaURIThrowError(resource: string): string | undefined { + throw new Error('test what happens when an error is trhown and not caught'); +} + +function onRequestSchemaContentThrowError(schemaUri: string): string | undefined { + throw new Error('test what happens when an error is trhown and not caught'); +} + function onRequestSchema1Content(schemaUri: string): string | undefined { return schemaJSON; } From 19de5f7a2d7b1aa7b9870c1a06fa9e85a8829ce3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Pupier?= Date: Wed, 22 Jul 2020 18:16:48 +0200 Subject: [PATCH 2/2] Apply suggestions from code review - typo and indentation Co-authored-by: Josh Pinkney --- test/schemaProvider.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/schemaProvider.test.ts b/test/schemaProvider.test.ts index 813e85d7..fd5d2de3 100644 --- a/test/schemaProvider.test.ts +++ b/test/schemaProvider.test.ts @@ -144,7 +144,7 @@ describe('Tests for schema provider feature', () => { await testCompletion(docUri, new vscode.Position(0, 0), { items: [ { - label: "apple", + label: "apple", kind: 9, documentation: "An apple" } @@ -178,11 +178,11 @@ function onRequestSchema1URI(resource: string): string | undefined { } function onRequestSchemaURIThrowError(resource: string): string | undefined { - throw new Error('test what happens when an error is trhown and not caught'); + throw new Error('test what happens when an error is thrown and not caught'); } function onRequestSchemaContentThrowError(schemaUri: string): string | undefined { - throw new Error('test what happens when an error is trhown and not caught'); + throw new Error('test what happens when an error is thrown and not caught'); } function onRequestSchema1Content(schemaUri: string): string | undefined {