Skip to content

Commit

Permalink
Use platform-agnostic exception classes
Browse files Browse the repository at this point in the history
Scala.js doesn't support NoSuchFileException etc.
  • Loading branch information
kitbellew committed Feb 12, 2025
1 parent 2146d6a commit 7ce3c82
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import java.io.InputStream
import java.io.PrintStream
import java.io.PrintWriter
import java.nio.file.Files
import java.nio.file.NoSuchFileException
import java.nio.file.Path

import scala.io.Codec
Expand Down Expand Up @@ -123,7 +122,7 @@ case class CliOptions(
*/
def configPath: Path = tempConfigPath.getOrElse(
canonicalConfigFile
.fold(throw new NoSuchFileException("Config file not found"))(_.get),
.fold(throw new RuntimeException("Config file not found"))(_.get),
)

private[cli] lazy val canonicalConfigFile: Option[Try[Path]] = gitOps
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import org.scalafmt.Versions
import org.scalafmt.config.ScalafmtConfig
import org.scalafmt.sysops._

import java.nio.file.NoSuchFileException
import java.nio.file.Path

import FileTestOps._
Expand Down Expand Up @@ -66,7 +65,7 @@ class CliOptionsTest extends FunSuite {
test(".configPath returns path to workingDirectory's .scalafmt.conf by default, if exists") {
assertEquals(baseCliOptions.config, None)
assertEquals(baseCliOptions.configStr, None)
intercept[NoSuchFileException](baseCliOptions.configPath)
intercept[RuntimeException](baseCliOptions.configPath)
}

test(".scalafmtConfig returns the configuration encoded from configStr if configStr is exists") {
Expand Down Expand Up @@ -98,7 +97,7 @@ class CliOptionsTest extends FunSuite {
val opt = baseCliOptions.copy(config = Some(configPath))
assert(opt.scalafmtConfig.isInstanceOf[Configured.NotOk])
val confError = opt.scalafmtConfig.asInstanceOf[Configured.NotOk].error
assert(confError.cause.exists(_.isInstanceOf[NoSuchFileException]))
assert(confError.cause.exists(_.isInstanceOf[RuntimeException]))
}

test(".scalafmtConfig returns Configured.NotOk for invalid configuration") {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.scalafmt.sysops

import java.nio.file.AccessDeniedException
import java.nio.file.NoSuchFileException
import java.nio.file.Path
import java.nio.file.Paths

Expand Down Expand Up @@ -42,7 +40,7 @@ object FileOps {
): Option[Try[Path]] = config.fold(tryGetConfigInDir(workingDirectory)) { x =>
val file = workingDirectory.join(x)
tryCheckConfigFile(file)
.orElse(Some(Failure(new NoSuchFileException(s"Config missing: $file"))))
.orElse(Some(Failure(new RuntimeException(s"Config missing: $file"))))
}

def tryGetConfigInDir(dir: AbsoluteFile): Option[Try[Path]] =
Expand All @@ -51,6 +49,6 @@ object FileOps {
private def tryCheckConfigFile(file: AbsoluteFile): Option[Try[Path]] =
if (!file.exists) None
else if (file.isRegularFile) Some(Success(file.path))
else Some(Failure(new AccessDeniedException(s"Config not a file: $file")))
else Some(Failure(new RuntimeException(s"Config not a file: $file")))

}

0 comments on commit 7ce3c82

Please sign in to comment.