Safety

org.llm4s.error.Safety
@Stable
object Safety

Turns code that throws into a Result, and an exception into an LLMError.

The extensions in org.llm4s.syntax (Try(...).toResult, throwable.toLLMError) are these functions under another spelling.

Attributes

Source
Safety.scala
Graph
Supertypes
class Object
trait Matchable
class Any
Self type
Safety.type

Members list

Value members

Concrete methods

The error for an exception, by its type alone:

The error for an exception, by its type alone:

  • a cancellation - mapped while the current thread is interrupted, or caused by an InterruptedException or a ClosedByInterruptException (see CancelledError.isCancellation) - is a CancelledError. Mapping classifies only and never sets the interrupt flag: it may run on a thread other than the interrupted one, such as a Future callback's pool thread;
  • a java.io.InterruptedIOException that is not a cancellation - a SocketTimeoutException, OkHttp's call timeout - is a NetworkError Request timeout;
  • a java.net.ConnectException is a NetworkError Connection failed;
  • anything else is an UnknownError carrying the exception as its cause.

The exception's message is never read for meaning: an exception that says HTTP 401 is an UnknownError here. A client that knows it holds a vendor SDK's exception classifies it itself.

The message of an UnknownError built here is redacted and capped (Redaction.safeBody), since an exception's message often echoes a response body and with it a request header or a key. The cause is not: it is the original exception, whose own getMessage and stack trace are unredacted, so a logger or handler that prints the cause (logger.error(msg, err.cause), printStackTrace) writes that text in the clear.

Attributes

Source
Safety.scala
def fromTry[A](t: Try[A]): Result[A]

The Try's value as a Right, or its exception, mapped by fromThrowable, as a Left.

The Try's value as a Right, or its exception, mapped by fromThrowable, as a Left.

Attributes

Source
Safety.scala
def safely[A](thunk: => A): Result[A]

Runs thunk and returns its value as a Right; a non-fatal exception it throws becomes a Left, mapped by fromThrowable. A fatal one, an InterruptedException included, propagates, as from scala.util.Try.

Runs thunk and returns its value as a Right; a non-fatal exception it throws becomes a Left, mapped by fromThrowable. A fatal one, an InterruptedException included, propagates, as from scala.util.Try.

Attributes

Source
Safety.scala