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

[data] introduce Environment and improve KyoException #1057

Merged
merged 9 commits into from
Jan 28, 2025
7 changes: 1 addition & 6 deletions kyo-core/jvm/src/main/scala/kyo/StreamCompression.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ import scala.annotation.tailrec

object StreamCompression:

final class StreamCompressionException(reason: String | Throwable)(using Frame) extends KyoException(
message = reason match
case string: String => Text(string)
case _: Throwable => null,
cause = reason
)
final class StreamCompressionException(cause: Text | Throwable)(using Frame) extends KyoException("", cause)

enum CompressionLevel(val value: Int) derives CanEqual:
case Default extends CompressionLevel(-1)
Expand Down
21 changes: 11 additions & 10 deletions kyo-data/shared/src/main/scala/kyo/KyoException.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@ package kyo

import kyo.*
import kyo.Ansi.*
import kyo.internal.Environment
import scala.util.control.NoStackTrace

class KyoException private[kyo] (
message: Text | Null = null,
cause: Text | Throwable | Null = null
)(using val frame: Frame) extends Exception(
Option(message) match
case Some(message) => message.toString;
case _ => null,
class KyoException private[kyo] (message: Text = "", cause: Text | Throwable = "")(using val frame: Frame)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fall back to empty string instead of null

extends Exception(
message.toString,
cause match
case cause: Throwable => cause;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙀 is the ; the cause of the trouble? I'm so sorry 😿

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's harmless :)

case cause: Throwable => cause
case _ => null
) with NoStackTrace:

Expand All @@ -27,8 +24,12 @@ class KyoException private[kyo] (
case _: Throwable => Absent
case cause: Text @unchecked => Maybe(cause)

val msg = frame.render(("⚠️ KyoException".red.bold :: Maybe(message).toList ::: detail.toList).map(_.show)*)
s"\n$msg\n"
if Environment.isDevelopment then
val msg = frame.render(("⚠️ KyoException".red.bold :: Maybe(message).toList ::: detail.toList).map(_.show)*)
s"\n$msg\n"
else
detail.map(_.toString).getOrElse(null)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about .fold("")(_.take(1000).toString)?

end if
end getMessage

end KyoException
37 changes: 37 additions & 0 deletions kyo-data/shared/src/main/scala/kyo/internal/Environment.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package kyo.internal

/** Environment detection utility.
*
* Provides functionality to detect whether the JVM is running in a development environment. This information is used to control the
* verbosity of error messages and debugging information in Kyo's implementations.
*
* The development mode can be controlled in two ways:
* 1. Explicitly via the system property "-Dkyo.development-mode.enable=true"
* 2. Automatically by detecting SBT in the classpath
* }}}
*/
private[kyo] object Environment:

/** Determines if the execution is a development environment.
*
* This method checks the following conditions in order:
*
* 1. If system property "kyo.development-mode.enable" exists:
* - Returns true if the property value is "true" (case insensitive)
* - Returns false if the property value is anything else
* 2. If the property doesn't exist:
* - Returns true if SBT is detected in the classpath (contains "org.scala-sbt")
* - Returns false otherwise
*
* @return
* true if running in a development environment, false otherwise
*/
val isDevelopment: Boolean = inferIsDevelopment()

private[internal] def inferIsDevelopment(): Boolean =
sys.props.get("kyo.development-mode.enable").map(_.toLowerCase) match
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

development can also be enabled explicitly via the kyo.development-mode.enable system property

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this. What would you think about kyo.development?

fwbrasil marked this conversation as resolved.
Show resolved Hide resolved
case Some("true") => true
case Some(_) => false
case _ => sys.props.get("java.class.path").exists(_.contains("org.scala-sbt"))

end Environment
Loading