Skip to content

Commit 2dfb3d5

Browse files
authored
Merge pull request #324 from apupier/323-catchThrownErrorInOtherExtensions
Catch errors that can be thrown by registered schema providers #323
2 parents 9ee0532 + 19de5f7 commit 2dfb3d5

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

src/schema-extension-api.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,24 +92,28 @@ class SchemaExtensionAPI implements ExtensionAPI {
9292
public requestCustomSchema(resource: string): string[] {
9393
const matches = [];
9494
for (let customKey of Object.keys(this._customSchemaContributors)) {
95-
const contributor = this._customSchemaContributors[customKey];
96-
let uri: string;
97-
if (contributor.label && workspace.textDocuments) {
98-
const labelRegexp = new RegExp(contributor.label, 'g');
99-
for (const doc of workspace.textDocuments) {
100-
if (doc.uri.toString() === resource) {
101-
if (labelRegexp.test(doc.getText())) {
102-
uri = contributor.requestSchema(resource);
103-
return [uri];
95+
try {
96+
const contributor = this._customSchemaContributors[customKey];
97+
let uri: string;
98+
if (contributor.label && workspace.textDocuments) {
99+
const labelRegexp = new RegExp(contributor.label, 'g');
100+
for (const doc of workspace.textDocuments) {
101+
if (doc.uri.toString() === resource) {
102+
if (labelRegexp.test(doc.getText())) {
103+
uri = contributor.requestSchema(resource);
104+
return [uri];
105+
}
104106
}
105107
}
106108
}
107-
}
108109

109-
uri = contributor.requestSchema(resource);
110+
uri = contributor.requestSchema(resource);
110111

111-
if (uri) {
112-
matches.push(uri);
112+
if (uri) {
113+
matches.push(uri);
114+
}
115+
} catch (error) {
116+
console.log(`Error thrown while requesting schema ` + error);
113117
}
114118
}
115119
return matches;

test/schemaProvider.test.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import * as vscode from 'vscode';
77
import { getDocUri, activate, testCompletion, testHover, testDiagnostics, sleep } from './helper';
8-
import { Uri } from 'vscode';
98

109
describe('Tests for schema provider feature', () => {
1110
const docUri = getDocUri('completion/completion.yaml');
@@ -134,6 +133,23 @@ describe('Tests for schema provider feature', () => {
134133
}
135134
]
136135
});
136+
});
137+
138+
it('Multiple contributors with one throwing an error', async () => {
139+
const client = await activate(docUri);
140+
client._customSchemaContributors = {};
141+
client.registerContributor(SCHEMA2, onRequestSchema2URI, onRequestSchema2Content);
142+
client.registerContributor("schemathrowingerror", onRequestSchemaURIThrowError, onRequestSchemaContentThrowError);
143+
144+
await testCompletion(docUri, new vscode.Position(0, 0), {
145+
items: [
146+
{
147+
label: "apple",
148+
kind: 9,
149+
documentation: "An apple"
150+
}
151+
]
152+
});
137153
});
138154
});
139155

@@ -161,6 +177,14 @@ function onRequestSchema1URI(resource: string): string | undefined {
161177
return undefined;
162178
}
163179

180+
function onRequestSchemaURIThrowError(resource: string): string | undefined {
181+
throw new Error('test what happens when an error is thrown and not caught');
182+
}
183+
184+
function onRequestSchemaContentThrowError(schemaUri: string): string | undefined {
185+
throw new Error('test what happens when an error is thrown and not caught');
186+
}
187+
164188
function onRequestSchema1Content(schemaUri: string): string | undefined {
165189
return schemaJSON;
166190
}

0 commit comments

Comments
 (0)