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

Fix acquireFor to not throw errors on resource open #102

Open
wants to merge 1 commit into
base: 2.12.x
Choose a base branch
from
Open
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
12 changes: 6 additions & 6 deletions core/src/main/scala/scalax/io/Resource.scala
Original file line number Diff line number Diff line change
Expand Up @@ -149,20 +149,20 @@ trait Resource[+R] extends ManagedResourceOperations[R] with ResourceOps[R, Reso
def open(): OpenedResource[R]

/**
* Open the resource execute the function and either return all errors as a list or the result of the
* Open the resource, execute the function, and either return all errors as a list or the result of the
* function execution.
*
* On open and close error handlers in ResourceContext are called. If they then raise errors
* the errors are captured and returned as a Right[List[Throwable]]
* On open and close error handlers in ResourceContext are called. If they throw exceptions, then
* the errors are captured and returned as a Left[List[Throwable]].
*
* Perhaps the worst method I have ever written :-(
*/
final def acquireFor[B](f: R => B): ExtractedEither[List[Throwable], B] = {

// perhaps the worst written method I have ever done :-(
val resourceEither = allCatch.either {
open()
}

var closeExceptions: List[Throwable] = Nil

/** Close resource and assign any exceptions to closeException */
Expand All @@ -180,7 +180,8 @@ trait Resource[+R] extends ManagedResourceOperations[R] with ResourceOps[R, Reso

resourceEither match {
case Left(t) =>
ExtractedEither(Left(List(context.openErrorHandler(t))))
// Do not rethrow the error. Return it instead.
ExtractedEither(Left(List(t)))
case Right(resource) =>
val result =
try Right(f(resource.get))
Expand All @@ -198,7 +199,6 @@ trait Resource[+R] extends ManagedResourceOperations[R] with ResourceOps[R, Reso
} else {
ExtractedEither(Right(result.right.get))
}

}
}
}
Expand Down
27 changes: 27 additions & 0 deletions file/src/test/scala/scalaio/test/AcquireForTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package scalaio.test

import java.io.{FileNotFoundException, InputStream}

import org.junit.Test

import scalax.file.Path

/**
* Test that a file can be opened and that open errors are correctly populated in the resulting Either,
* and not rethrown, per the contract.
*/
class AcquireForTest {

def noop(stream: InputStream): Unit = {
// Not op'ing.
}

@Test
def test(): Unit = {
val result = Path.fromString("foobarfile").inputStream.acquireFor { noop }
result.either match {
case Left(seq) if seq.head.getClass == classOf[FileNotFoundException] => assert(true)
case _ => assert(false)
}
}
}