Skip to content
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

Catch errors that can be thrown by registered schema providers #323 #324

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions src/schema-extension-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there an output channel where this error can be logged? or we keep it at console level so only developers will see it?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to have output channel, we need provide, as much as possible, information when something goes wrong.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is better to handle it in a separate PR as it is a distinct feature to have an outputchannel. i created #327

}
}
return matches;
Expand Down
26 changes: 25 additions & 1 deletion test/schemaProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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"
}
]
});
});
});

Expand Down Expand Up @@ -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 thrown and not caught');
}

function onRequestSchemaContentThrowError(schemaUri: string): string | undefined {
throw new Error('test what happens when an error is thrown and not caught');
}

function onRequestSchema1Content(schemaUri: string): string | undefined {
return schemaJSON;
}
Expand Down