Skip to content

Commit d7f5b66

Browse files
authored
fix: substitute error message popup with log (#753)
* fix: substitute error message popup with log * fix: add change set * fix: correct file name
1 parent b9e99f1 commit d7f5b66

File tree

6 files changed

+94
-45
lines changed

6 files changed

+94
-45
lines changed

.changeset/five-emus-love.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@ui5-language-assistant/vscode-ui5-language-assistant-bas-ext": patch
3+
"vscode-ui5-language-assistant": patch
4+
"@ui5-language-assistant/language-server": patch
5+
---
6+
7+
fix: Substitute error message popup with log
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
export const PACKAGE_NAME = "@ui5-language-assistant/language-server";
2+
export const BUILD_CONTEXT_ERROR_MSG =
3+
"An error has occurred building context. Please open an [issue](https://github.com/SAP/ui5-language-assistant/issues)";
4+
export const SDK_MSG =
5+
"[SAPUI5 SDK](https://tools.hana.ondemand.com/#sapui5) is not accessible. Connect to the internet or setup local web server for offline work.";

packages/language-server/src/server.ts

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import { getLogger, setLogLevel } from "./logger";
5454
import { initI18n } from "./i18n";
5555
import { isXMLView, getCDNBaseUrl } from "@ui5-language-assistant/logic-utils";
5656
import { getDefinition } from "@ui5-language-assistant/xml-views-definition";
57+
import { handleContextError } from "./utils";
5758

5859
const connection = createConnection(ProposedFeatures.all);
5960
const documents = new TextDocuments(TextDocument);
@@ -147,10 +148,7 @@ connection.onCompletion(
147148
initializationOptions?.modelCachePath
148149
);
149150
if (!isContext(context)) {
150-
connection.sendNotification(
151-
"UI5LanguageAssistant/context-error",
152-
context
153-
);
151+
handleContextError(context);
154152
return [];
155153
}
156154
const version = context.ui5Model.version;
@@ -207,10 +205,7 @@ connection.onHover(
207205
initializationOptions?.modelCachePath
208206
);
209207
if (!isContext(context)) {
210-
connection.sendNotification(
211-
"UI5LanguageAssistant/context-error",
212-
context
213-
);
208+
handleContextError(context);
214209
return;
215210
}
216211
const version = context.ui5Model.version;
@@ -257,10 +252,7 @@ const validateOpenDocuments = async (): Promise<void> => {
257252
initializationOptions?.modelCachePath
258253
);
259254
if (!isContext(context)) {
260-
connection.sendNotification(
261-
"UI5LanguageAssistant/context-error",
262-
context
263-
);
255+
handleContextError(context);
264256
return;
265257
}
266258
const diagnostics = getXMLViewDiagnostics({
@@ -288,10 +280,7 @@ const validateIdsOfOpenDocuments = async (): Promise<void> => {
288280
initializationOptions?.modelCachePath
289281
);
290282
if (!isContext(context)) {
291-
connection.sendNotification(
292-
"UI5LanguageAssistant/context-error",
293-
context
294-
);
283+
handleContextError(context);
295284
return;
296285
}
297286
const idDiagnostics = getXMLViewIdDiagnostics({
@@ -422,10 +411,7 @@ documents.onDidChangeContent(async (changeEvent): Promise<void> => {
422411
document.getText()
423412
);
424413
if (!isContext(context)) {
425-
connection.sendNotification(
426-
"UI5LanguageAssistant/context-error",
427-
context
428-
);
414+
handleContextError(context);
429415
return;
430416
}
431417

@@ -476,10 +462,7 @@ connection.onCodeAction(async (params) => {
476462
textDocument.getText()
477463
);
478464
if (!isContext(context)) {
479-
connection.sendNotification(
480-
"UI5LanguageAssistant/context-error",
481-
context
482-
);
465+
handleContextError(context);
483466
return;
484467
}
485468

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { getLogger } from "../logger";
2+
import { BUILD_CONTEXT_ERROR_MSG, SDK_MSG } from "../constant";
3+
4+
/**
5+
* Handles context-related errors by logging them with appropriate error messages.
6+
*
7+
* This function categorizes errors based on whether they have an error code:
8+
* - Errors with a code property are logged with SDK_MSG
9+
* - Errors without a code property are logged with BUILD_CONTEXT_ERROR_MSG
10+
*
11+
* @param error - The error object to handle. Must be an Error instance with an optional code property
12+
* @param error.code - Optional error code that determines which error message to use
13+
*/
14+
export function handleContextError(error: Error & { code?: string }): void {
15+
if (error.code) {
16+
getLogger().error(SDK_MSG, { error });
17+
} else {
18+
getLogger().error(BUILD_CONTEXT_ERROR_MSG, { error });
19+
}
20+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { handleContextError } from "../../../src/utils";
2+
import { getLogger } from "../../../src/logger";
3+
import { BUILD_CONTEXT_ERROR_MSG, SDK_MSG } from "../../../src/constant";
4+
5+
// Mock the logger module
6+
jest.mock("../../../src/logger");
7+
8+
describe("handleContextError", () => {
9+
let mockLogger: jest.Mocked<{
10+
error: jest.Mock;
11+
}>;
12+
13+
beforeEach(() => {
14+
// Clear all mocks before each test
15+
jest.clearAllMocks();
16+
17+
// Create a mock logger with an error method
18+
mockLogger = {
19+
error: jest.fn(),
20+
};
21+
22+
// Make getLogger return our mock logger
23+
(getLogger as jest.Mock).mockReturnValue(mockLogger);
24+
});
25+
26+
it("should log error with SDK_MSG", () => {
27+
// Arrange
28+
const errorWithCode = Object.assign(new Error("Connection failed"), {
29+
code: "ECONNREFUSED",
30+
});
31+
32+
// Act
33+
handleContextError(errorWithCode);
34+
35+
// Assert
36+
expect(getLogger).toHaveBeenCalled();
37+
expect(mockLogger.error).toHaveBeenCalledTimes(1);
38+
expect(mockLogger.error).toHaveBeenCalledWith(SDK_MSG, {
39+
error: errorWithCode,
40+
});
41+
});
42+
it("should log error with BUILD_CONTEXT_ERROR_MSG", () => {
43+
// Arrange
44+
const errorWithoutCode = new Error("Build failed");
45+
46+
// Act
47+
handleContextError(errorWithoutCode);
48+
49+
// Assert
50+
expect(getLogger).toHaveBeenCalled();
51+
expect(mockLogger.error).toHaveBeenCalledTimes(1);
52+
expect(mockLogger.error).toHaveBeenCalledWith(BUILD_CONTEXT_ERROR_MSG, {
53+
error: errorWithoutCode,
54+
});
55+
});
56+
});

packages/vscode-ui5-language-assistant/src/extension.ts

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,6 @@ function init(context: ExtensionContext): void {
6868
"UI5LanguageAssistant/ui5Model",
6969
async (model: UI5Model): Promise<void> => await updateCurrentModel(model)
7070
);
71-
client.onNotification(
72-
"UI5LanguageAssistant/context-error",
73-
(error: Error) => handleContextError(error)
74-
);
7571
client.onNotification(
7672
"UI5LanguageAssistant/manifestVersionChanged",
7773
(manifestChange: ManifestVersionChange) =>
@@ -232,23 +228,6 @@ async function updateCurrentModel(model: UI5Model | undefined): Promise<void> {
232228
}
233229
}
234230

235-
let showedOnce = false;
236-
function handleContextError(error: Error & { code?: string }) {
237-
if (showedOnce) {
238-
return;
239-
}
240-
showedOnce = true;
241-
if (error.code) {
242-
window.showErrorMessage(
243-
"[SAPUI5 SDK](https://tools.hana.ondemand.com/#sapui5) is not accessible. Connect to the internet or setup local web server for offline work."
244-
);
245-
} else {
246-
window.showErrorMessage(
247-
"An error has occurred building context. Please open an [issue](https://github.com/SAP/ui5-language-assistant/issues)"
248-
);
249-
}
250-
}
251-
252231
export async function deactivate(): Promise<Thenable<void>> {
253232
if (!client) {
254233
return undefined;

0 commit comments

Comments
 (0)