Skip to content
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
7 changes: 7 additions & 0 deletions .changeset/five-emus-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@ui5-language-assistant/vscode-ui5-language-assistant-bas-ext": patch
"vscode-ui5-language-assistant": patch
"@ui5-language-assistant/language-server": patch
---

fix: Substitute error message popup with log
4 changes: 4 additions & 0 deletions packages/language-server/src/constant.ts
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
export const PACKAGE_NAME = "@ui5-language-assistant/language-server";
export const BUILD_CONTEXT_ERROR_MSG =
"An error has occurred building context. Please open an [issue](https://github.com/SAP/ui5-language-assistant/issues)";
export const SDK_MSG =
"[SAPUI5 SDK](https://tools.hana.ondemand.com/#sapui5) is not accessible. Connect to the internet or setup local web server for offline work.";
31 changes: 7 additions & 24 deletions packages/language-server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import { getLogger, setLogLevel } from "./logger";
import { initI18n } from "./i18n";
import { isXMLView, getCDNBaseUrl } from "@ui5-language-assistant/logic-utils";
import { getDefinition } from "@ui5-language-assistant/xml-views-definition";
import { handleContextError } from "./utils";

const connection = createConnection(ProposedFeatures.all);
const documents = new TextDocuments(TextDocument);
Expand Down Expand Up @@ -147,10 +148,7 @@ connection.onCompletion(
initializationOptions?.modelCachePath
);
if (!isContext(context)) {
connection.sendNotification(
"UI5LanguageAssistant/context-error",
context
);
handleContextError(context);
return [];
}
const version = context.ui5Model.version;
Expand Down Expand Up @@ -207,10 +205,7 @@ connection.onHover(
initializationOptions?.modelCachePath
);
if (!isContext(context)) {
connection.sendNotification(
"UI5LanguageAssistant/context-error",
context
);
handleContextError(context);
return;
}
const version = context.ui5Model.version;
Expand Down Expand Up @@ -257,10 +252,7 @@ const validateOpenDocuments = async (): Promise<void> => {
initializationOptions?.modelCachePath
);
if (!isContext(context)) {
connection.sendNotification(
"UI5LanguageAssistant/context-error",
context
);
handleContextError(context);
return;
}
const diagnostics = getXMLViewDiagnostics({
Expand Down Expand Up @@ -288,10 +280,7 @@ const validateIdsOfOpenDocuments = async (): Promise<void> => {
initializationOptions?.modelCachePath
);
if (!isContext(context)) {
connection.sendNotification(
"UI5LanguageAssistant/context-error",
context
);
handleContextError(context);
return;
}
const idDiagnostics = getXMLViewIdDiagnostics({
Expand Down Expand Up @@ -422,10 +411,7 @@ documents.onDidChangeContent(async (changeEvent): Promise<void> => {
document.getText()
);
if (!isContext(context)) {
connection.sendNotification(
"UI5LanguageAssistant/context-error",
context
);
handleContextError(context);
return;
}

Expand Down Expand Up @@ -476,10 +462,7 @@ connection.onCodeAction(async (params) => {
textDocument.getText()
);
if (!isContext(context)) {
connection.sendNotification(
"UI5LanguageAssistant/context-error",
context
);
handleContextError(context);
return;
}

Expand Down
20 changes: 20 additions & 0 deletions packages/language-server/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { getLogger } from "../logger";
import { BUILD_CONTEXT_ERROR_MSG, SDK_MSG } from "../constant";

/**
* Handles context-related errors by logging them with appropriate error messages.
*
* This function categorizes errors based on whether they have an error code:
* - Errors with a code property are logged with SDK_MSG
* - Errors without a code property are logged with BUILD_CONTEXT_ERROR_MSG
*
* @param error - The error object to handle. Must be an Error instance with an optional code property
* @param error.code - Optional error code that determines which error message to use
*/
export function handleContextError(error: Error & { code?: string }): void {
if (error.code) {
getLogger().error(SDK_MSG, { error });
} else {
getLogger().error(BUILD_CONTEXT_ERROR_MSG, { error });
}
}
56 changes: 56 additions & 0 deletions packages/language-server/test/unit/utils/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { handleContextError } from "../../../src/utils";
import { getLogger } from "../../../src/logger";
import { BUILD_CONTEXT_ERROR_MSG, SDK_MSG } from "../../../src/constant";

// Mock the logger module
jest.mock("../../../src/logger");

describe("handleContextError", () => {
let mockLogger: jest.Mocked<{
error: jest.Mock;
}>;

beforeEach(() => {
// Clear all mocks before each test
jest.clearAllMocks();

// Create a mock logger with an error method
mockLogger = {
error: jest.fn(),
};

// Make getLogger return our mock logger
(getLogger as jest.Mock).mockReturnValue(mockLogger);
});

it("should log error with SDK_MSG", () => {
// Arrange
const errorWithCode = Object.assign(new Error("Connection failed"), {
code: "ECONNREFUSED",
});

// Act
handleContextError(errorWithCode);

// Assert
expect(getLogger).toHaveBeenCalled();
expect(mockLogger.error).toHaveBeenCalledTimes(1);
expect(mockLogger.error).toHaveBeenCalledWith(SDK_MSG, {
error: errorWithCode,
});
});
it("should log error with BUILD_CONTEXT_ERROR_MSG", () => {
// Arrange
const errorWithoutCode = new Error("Build failed");

// Act
handleContextError(errorWithoutCode);

// Assert
expect(getLogger).toHaveBeenCalled();
expect(mockLogger.error).toHaveBeenCalledTimes(1);
expect(mockLogger.error).toHaveBeenCalledWith(BUILD_CONTEXT_ERROR_MSG, {
error: errorWithoutCode,
});
});
});
21 changes: 0 additions & 21 deletions packages/vscode-ui5-language-assistant/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ function init(context: ExtensionContext): void {
"UI5LanguageAssistant/ui5Model",
async (model: UI5Model): Promise<void> => await updateCurrentModel(model)
);
client.onNotification(
"UI5LanguageAssistant/context-error",
(error: Error) => handleContextError(error)
);
client.onNotification(
"UI5LanguageAssistant/manifestVersionChanged",
(manifestChange: ManifestVersionChange) =>
Expand Down Expand Up @@ -232,23 +228,6 @@ async function updateCurrentModel(model: UI5Model | undefined): Promise<void> {
}
}

let showedOnce = false;
function handleContextError(error: Error & { code?: string }) {
if (showedOnce) {
return;
}
showedOnce = true;
if (error.code) {
window.showErrorMessage(
"[SAPUI5 SDK](https://tools.hana.ondemand.com/#sapui5) is not accessible. Connect to the internet or setup local web server for offline work."
);
} else {
window.showErrorMessage(
"An error has occurred building context. Please open an [issue](https://github.com/SAP/ui5-language-assistant/issues)"
);
}
}

export async function deactivate(): Promise<Thenable<void>> {
if (!client) {
return undefined;
Expand Down