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

Source.fromResource("some_file") throws NPE instead of FileNotFound exception #11743

Open
vasiliybondarenko opened this issue Sep 19, 2019 · 3 comments · May be fixed by scala/scala#8443

Comments

@vasiliybondarenko
Copy link

commented Sep 19, 2019

Source.fromResource("some_file") throws NPE instead of FileNotFound exception for non existent files.
Source.fromFile("some_file") throws FileNotFoundException in that case

@som-snytt

This comment has been minimized.

Copy link

commented Sep 19, 2019

A resource need not be a file resource. A workaround would be of the form:

Option(ClassLoader.getSystemResource("junk")).map(io.Source.fromURL(_))

I think Source is acknowledged to be convenient but not fully realized. It has a report facility that might be developed further.

@nogurenn

This comment has been minimized.

Copy link

commented Sep 24, 2019

@som-snytt What would be the desired change for this?

@eed3si9n

This comment has been minimized.

Copy link
Member

commented Sep 24, 2019

https://github.com/scala/scala/blob/2480754c33cff3bbd98f0ca268638254ea67e2d8/src/library/scala/io/Source.scala#L178-L179

  def fromResource(resource: String, classLoader: ClassLoader = Thread.currentThread().getContextClassLoader())(implicit codec: Codec): BufferedSource =
    fromInputStream(classLoader.getResourceAsStream(resource))

maybe it should be

  def fromResource(resource: String, classLoader: ClassLoader = Thread.currentThread().getContextClassLoader())(implicit codec: Codec): BufferedSource =
    Option(classLoader.getResourceAsStream(resource)) match {
      case Some(in) => fromInputStream(in)
      case None     => throw new IllegalArgumentException(s"resource '$resource' was not found")
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.