Skip to content

Commit 123f477

Browse files
committed
Enhance error handling in failover / retry helper
1 parent 90ef9a3 commit 123f477

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

openai-core/src/main/scala/io/cequence/openaiscala/RetryHelpers.scala

+14-4
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,25 @@ trait RetryHelpers {
148148
ec: ExecutionContext,
149149
scheduler: Scheduler
150150
): Future[T] = {
151+
assert(
152+
inputsAndMessagesToTryInOrder.nonEmpty || lastException.nonEmpty,
153+
"At least one input or last exception must be defined!"
154+
)
155+
151156
inputsAndMessagesToTryInOrder match {
152157
case Nil =>
153-
val lastExceptionMessage = lastException.map(_.getMessage).getOrElse("N/A")
154-
Future.failed(
155-
new OpenAIScalaClientException(
156-
s"No more failover inputs to try! Last error: ${lastExceptionMessage}"
158+
val lastExceptionActual = lastException.getOrElse(
159+
throw new OpenAIScalaClientException(
160+
s"Should never happen. No last exception provided!"
157161
)
158162
)
159163

164+
logger.error(
165+
s"No more failover inputs to try! Throwing the last error: ${lastExceptionActual.getMessage}"
166+
)
167+
168+
Future.failed(lastExceptionActual)
169+
160170
case _ =>
161171
val (input, inputLogMessage) = inputsAndMessagesToTryInOrder.head
162172

openai-examples/src/main/scala/io/cequence/openaiscala/examples/Example.scala

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ trait ExampleBase[T <: CloseableService] {
2323

2424
def main(args: Array[String]): Unit = {
2525
run.recover { case e: Exception =>
26+
println("Error received:")
2627
e.printStackTrace()
2728
closeAll()
2829
System.exit(1)

0 commit comments

Comments
 (0)