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

Improve the document cache miss error message #2161

Merged
merged 3 commits into from
Feb 18, 2022
Merged
Changes from 1 commit
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
20 changes: 17 additions & 3 deletions packages/graphql-language-service-server/src/MessageProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ export class MessageProcessor {

const cachedDocument = this._getCachedDocument(textDocument.uri);
if (!cachedDocument) {
throw new Error('A cached document cannot be found.');
throw new Error(this._errorMessageForMissingDocument(textDocument.uri));
Copy link
Member

Choose a reason for hiding this comment

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

Sorry I meant to put this in a review before. Can we just remove these errors and return if !cachedDocument ? My earlier comment explains why. There is a section in the roadmap or somewhere where I discuss removing these

}

const found = cachedDocument.contents.find(content => {
Expand Down Expand Up @@ -537,7 +537,7 @@ export class MessageProcessor {

const cachedDocument = this._getCachedDocument(textDocument.uri);
if (!cachedDocument) {
throw new Error('A cached document cannot be found.');
throw new Error(this._errorMessageForMissingDocument(textDocument.uri));
}

const found = cachedDocument.contents.find(content => {
Expand Down Expand Up @@ -749,14 +749,28 @@ export class MessageProcessor {
const textDocument = params.textDocument;
const cachedDocument = this._getCachedDocument(textDocument.uri);
if (!cachedDocument || !cachedDocument.contents[0]) {
throw new Error('A cached document cannot be found.');
throw new Error(this._errorMessageForMissingDocument(textDocument.uri));
}

return this._languageService.getDocumentSymbols(
cachedDocument.contents[0].query,
textDocument.uri,
);
}

_errorMessageForMissingDocument(uri: string): string {
const allURIs = Object.keys(this._textDocumentCache);
if (allURIs.length === 0) {
return `A cached document cannot be found for ${uri}. There are no documents in the cache.`;
} else if (allURIs.length > 5) {
return `A cached document cannot be found for ${uri}. There are ${
allURIs.length
} documents in the cache: ${allURIs.join(', ')}`;
} else {
return `A cached document cannot be found for ${uri}. There are ${allURIs.length} documents in the cache.`;
}
}

// async handleReferencesRequest(params: ReferenceParams): Promise<Location[]> {
// if (!this._isInitialized) {
// return [];
Expand Down