From 0cbc5f30e305301c9fe9759f58738b9981a2dee4 Mon Sep 17 00:00:00 2001 From: azinneera Date: Thu, 1 Aug 2024 21:39:55 +0530 Subject: [PATCH] Improve ProjectException error messages --- .../java/io/ballerina/projects/directory/BuildProject.java | 2 +- .../java/io/ballerina/projects/directory/ProjectLoader.java | 2 +- .../io/ballerina/projects/directory/SingleFileProject.java | 2 +- .../java/io/ballerina/projects/internal/ProjectFiles.java | 2 +- .../main/java/io/ballerina/projects/util/ProjectPaths.java | 6 +++--- .../java/io/ballerina/projects/test/TestBuildProject.java | 5 ++--- .../io/ballerina/projects/test/TestUnknownTypeProject.java | 3 ++- 7 files changed, 11 insertions(+), 11 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/directory/BuildProject.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/directory/BuildProject.java index e3f6a5dc96c8..371dbcfcc5eb 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/directory/BuildProject.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/directory/BuildProject.java @@ -254,7 +254,7 @@ public DocumentId documentId(Path file) { } } } - throw new ProjectException("provided path does not belong to the project"); + throw new ProjectException("'" + file.toString() + "' does not belong to the current project"); } private boolean isFilePathInProject(Path filepath) { diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/directory/ProjectLoader.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/directory/ProjectLoader.java index a4744b81c66b..f6145abf8e2e 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/directory/ProjectLoader.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/directory/ProjectLoader.java @@ -92,7 +92,7 @@ public static Project loadProject(Path path, ProjectEnvironmentBuilder projectEn } if (!ProjectPaths.isBalFile(absFilePath)) { - throw new ProjectException("provided path is not a valid Ballerina source file"); + throw new ProjectException("'" + absFilePath + "' is not a valid Ballerina source file"); } try { diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/directory/SingleFileProject.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/directory/SingleFileProject.java index b1687fa78b45..599d9afc9347 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/directory/SingleFileProject.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/directory/SingleFileProject.java @@ -105,7 +105,7 @@ public Project duplicate() { public DocumentId documentId(Path file) { if (!this.sourceRoot.toAbsolutePath().normalize().toString().equals( file.toAbsolutePath().normalize().toString())) { - throw new ProjectException("provided path does not belong to the project"); + throw new ProjectException("'" + file + "' does not belong to the current project"); } return this.currentPackage().getDefaultModule().documentIds().iterator().next(); } diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/internal/ProjectFiles.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/internal/ProjectFiles.java index e912df197391..595d5a22c3a5 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/internal/ProjectFiles.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/internal/ProjectFiles.java @@ -367,7 +367,7 @@ public static void validateBuildProjectDirPath(Path projectDirPath) { } if (ProjectUtils.findProjectRoot(projectDirPath.toAbsolutePath().getParent()) != null) { - throw new ProjectException("Provided path is already within a Ballerina package: " + projectDirPath); + throw new ProjectException("'" + projectDirPath + "' is already within a Ballerina package"); } checkReadPermission(projectDirPath); diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/util/ProjectPaths.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/util/ProjectPaths.java index ece202a30f84..885c3c33939e 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/util/ProjectPaths.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/util/ProjectPaths.java @@ -45,7 +45,7 @@ public class ProjectPaths { public static Path packageRoot(Path filepath) throws ProjectException { // check if the file exists if (!Files.exists(filepath)) { - throw new ProjectException("provided path does not exist:" + filepath); + throw new ProjectException("'" + filepath + "'" + " does not exist"); } if (Files.isDirectory(filepath)) { @@ -62,7 +62,7 @@ public static Path packageRoot(Path filepath) throws ProjectException { // check if the file is a regular file if (!Files.isRegularFile(filepath)) { - throw new ProjectException("provided path is not a regular file: " + filepath); + throw new ProjectException("'" + filepath + "'" + " is not a regular file"); } // Check if the file is inside a Ballerina package directory @@ -79,7 +79,7 @@ public static Path packageRoot(Path filepath) throws ProjectException { } if (!isBalFile(filepath)) { - throw new ProjectException("provided path is not a valid Ballerina source file: " + filepath); + throw new ProjectException("'" + filepath + "' is not a valid Ballerina source file"); } // check if the file is a source file in the default module diff --git a/project-api/project-api-test/src/test/java/io/ballerina/projects/test/TestBuildProject.java b/project-api/project-api-test/src/test/java/io/ballerina/projects/test/TestBuildProject.java index 0f4dcbaad25d..6bdfd9dd7c76 100644 --- a/project-api/project-api-test/src/test/java/io/ballerina/projects/test/TestBuildProject.java +++ b/project-api/project-api-test/src/test/java/io/ballerina/projects/test/TestBuildProject.java @@ -464,8 +464,7 @@ public void testLoadBallerinaProjectInProject() { TestUtils.loadBuildProject(projectPath); Assert.fail("expected an invalid project exception"); } catch (ProjectException e) { - Assert.assertTrue(e.getMessage().contains("Provided path is already within a Ballerina package: " + - projectPath)); + Assert.assertEquals(e.getMessage(), "'" + projectPath + "' is already within a Ballerina package"); } } @@ -838,7 +837,7 @@ public void testAccessNonExistingDocument() { DocumentId oldDocumentId = buildProject.documentId(filePath); // get the document ID Assert.fail(); } catch (ProjectException e) { - Assert.assertTrue(e.getMessage().contains("provided path does not belong to the project")); + Assert.assertTrue(e.getMessage().contains("'" + filePath + "' does not belong to the current project")); } } diff --git a/project-api/project-api-test/src/test/java/io/ballerina/projects/test/TestUnknownTypeProject.java b/project-api/project-api-test/src/test/java/io/ballerina/projects/test/TestUnknownTypeProject.java index b5c976da6767..03c36eb10eeb 100644 --- a/project-api/project-api-test/src/test/java/io/ballerina/projects/test/TestUnknownTypeProject.java +++ b/project-api/project-api-test/src/test/java/io/ballerina/projects/test/TestUnknownTypeProject.java @@ -333,7 +333,8 @@ public void testLoadBalaProjectByNonBalFile() { project = TestUtils.loadProject(projectPath); Assert.fail("project loading with a non-bal file is expected to fail"); } catch (Exception e) { - Assert.assertTrue(e.getMessage().contains("provided path is not a valid Ballerina source file")); + Assert.assertEquals(e.getMessage(), "'" + projectPath.toAbsolutePath() + + "' is not a valid Ballerina source file"); } }