question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow `MoreFiles.fileTraverser()` to have customizable error handling

See original GitHub issue

Context

I’ve noticed that when using Kotlin’s File.walk function, if it doesn’t have permission to read a subdirectory, it will just skip over that subdirectory by default.

If one wanted to handle that case as an error, they’d use FileTreeWalk.onFail. For example,

aDirectory.walk().onFail { fileOrSubdirectory, ex -> println(ex.message) }

(This is the same behaviour as Java’s Files.walkFileTree, where one implements FileVisitor::visitFileFailed to also handle any unreadable files/directories.)

By comparison, Java’s Files.walk stream and Guava’s MoreFiles.fileTraverser() let the unreadable directory bubble up as an IOException. This means that if either of these APIs can’t read a file/directory, they stop there and then, rather than reading on.

I think this behaviour makes these two APIs impractical sometimes. In fact, there seems to be evidence for this, with a StackOverflow question and a now-closed Java bug report on Java’s Files.walk.

Feature request

Consider giving MoreFiles.fileTraverser() a customizable, more lenient error-handling mechanism than just bubbling up the first IOException that occurs.

For example, Guava could introduce something like Kotlin’s FileTreeWalk.onFail, which would allow code like,

Path directory = ...;
Iterable<Path> filesAndDirectories =
    MoreFiles.fileTraverser()
        .onFail((file, ex) -> System.err.println(ex.getMessage()))
        .breadthFirst(directory);
for (var path : filesAndDirectories) {
  println(path)
}

This would forward any IOExceptions to onFail, rather than throwing them, and resume traversing afterwards.

Note: Implementation-wise, this probably means that MoreFiles.fileTraverser() would need to return a new sub-type of Traverser that has an onFail method, or a new type altogether.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
PrachiPrakashcommented, Jul 21, 2021

@jbduncan that okay I guess was just looking around the code found this interesting so tried to give it a shot. not aware that this was still under discussion.

0reactions
jbduncancommented, Oct 25, 2021

@ManishOffi Ah, sorry, I can’t, as I’m not a member of the Guava team or even a Googler. Furthermore it looks like the API still hasn’t been decided upon, so there wouldn’t be anything to code anyway.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Get Started with Custom Error Handling in Spring Boot (Java)
Learn how to implement custom error handling logic in Spring Boot. You will see two approaches based on the @ControllerAdvice annotation.
Read more >
Handle errors in ASP.NET Core | Microsoft Learn
To allow unauthenticated users to view the custom error handling page, ensure that it supports anonymous access. Access the exception. Use ...
Read more >
Implement Custom Exceptions in Java: Why, When and How
Custom exceptions provide you the flexibility to add attributes and methods that are not part of a standard Java exception. These can store ......
Read more >
Custom Error Message Handling for REST API - Baeldung
Sometimes, we're going to need to handle a custom exception that doesn't have a default implementation in the base class, as we'll get...
Read more >
Custom Error Handling in REST Controllers with Spring Boot
We use not only the well-known ControllerAdvice and ExceptionHandler annotations but also DefaultErrorAttributes and ErrorController to make ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found