Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use platform-agnostic exception classes #4807

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")))

}