Skip to content

Commit

Permalink
Improve ProjectException error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
azinneera authored and gayaldassanayake committed Aug 2, 2024
1 parent a47ae04 commit 0cbc5f3
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Check warning on line 108 in compiler/ballerina-lang/src/main/java/io/ballerina/projects/directory/SingleFileProject.java

View check run for this annotation

Codecov / codecov/patch

compiler/ballerina-lang/src/main/java/io/ballerina/projects/directory/SingleFileProject.java#L108

Added line #L108 was not covered by tests
}
return this.currentPackage().getDefaultModule().documentIds().iterator().next();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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 warning on line 65 in compiler/ballerina-lang/src/main/java/io/ballerina/projects/util/ProjectPaths.java

View check run for this annotation

Codecov / codecov/patch

compiler/ballerina-lang/src/main/java/io/ballerina/projects/util/ProjectPaths.java#L65

Added line #L65 was not covered by tests
}

// Check if the file is inside a Ballerina package directory
Expand All @@ -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 warning on line 82 in compiler/ballerina-lang/src/main/java/io/ballerina/projects/util/ProjectPaths.java

View check run for this annotation

Codecov / codecov/patch

compiler/ballerina-lang/src/main/java/io/ballerina/projects/util/ProjectPaths.java#L82

Added line #L82 was not covered by tests
}

// check if the file is a source file in the default module
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand Down Expand Up @@ -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"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand Down

0 comments on commit 0cbc5f3

Please sign in to comment.