Skip to content

Commit

Permalink
More accurate exeption handling in brightcove client
Browse files Browse the repository at this point in the history
  • Loading branch information
ekrojo77 committed Jan 9, 2025
1 parent 574bc37 commit 2dc515d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@ import io.circe.generic.codec.DerivedAsObjectCodec.deriveCodec
import io.circe.parser.*
import sttp.client3.{HttpClientSyncBackend, UriContext, basicRequest}
import no.ndla.common.configuration.HasBaseProps
import no.ndla.common.errors.{
TokenDecodingException,
TokenRetrievalException,
VideoSourceParsingException,
VideoSourceRetrievalException
}

import scala.util.Try
import scala.util.{Failure, Success, Try}

case class TokenResponse(access_token: String, token_type: String, expires_in: Int)

Expand All @@ -30,16 +36,15 @@ trait NdlaBrightcoveClient {
basicRequest.auth
.basic(clientID, clientSecret)
.post(uri"${props.BrightCoveAuthUri}?grant_type=client_credentials")
val authResponse = request.send(backend)
Try {
authResponse.body match {
case Right(jsonString) =>
decode[TokenResponse](jsonString) match {
case Right(tokenResponse) => tokenResponse.access_token
case Left(error) => throw new Exception(s"Failed to decode token response: ${error.getMessage}")
}
case Left(error) => throw new Exception(s"Failed to get token: ${error}")
}
Try(request.send(backend).body) match {
case Success(Right(jsonString)) =>
decode[TokenResponse](jsonString) match {
case Right(tokenResponse) => Success(tokenResponse.access_token)
case Left(error) =>
Failure(new TokenDecodingException(s"Failed to decode token response: ${error.getMessage}"))
}
case Success(Left(error)) => Failure(new TokenRetrievalException(s"Failed to get token: ${error}"))
case Failure(exception) => Failure(new TokenRetrievalException(exception.getMessage))
}
}

Expand All @@ -53,19 +58,20 @@ trait NdlaBrightcoveClient {
implicit val backend = HttpClientSyncBackend()

val response = request.send(backend)
Try {
response.body match {
case Right(jsonString) =>
parse(jsonString) match {
case Right(json) =>
json.asArray match {
case Some(videoSources) => videoSources
case None => throw new Exception("Failed to parse video source")
}
case Left(error) => throw new Exception(s"Failed to parse video source: ${error.getMessage}")
}
case Left(error) => throw new Exception(s"Failed to get video source: ${error}")
}
Try(request.send(backend).body) match {
case Success(Right(jsonString)) =>
parse(jsonString) match {
case Right(json) =>
json.asArray match {
case Some(videoSources) => Success(videoSources)
case None => Failure(new VideoSourceParsingException("Failed to parse video source"))
}
case Left(error) =>
Failure(new VideoSourceParsingException(s"Failed to parse video source: ${error.getMessage}"))
}
case Success(Left(error)) =>
Failure(new VideoSourceRetrievalException(s"Failed to get video source: ${error}"))
case Failure(exception) => Failure(new VideoSourceRetrievalException(exception.getMessage))
}
}
}
Expand Down
14 changes: 9 additions & 5 deletions common/src/main/scala/no/ndla/common/errors/NDLAErrors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ object AccessDeniedException {
def forbidden: AccessDeniedException =
AccessDeniedException("User is missing required permission(s) to perform this operation")
}
case class NotFoundException(message: String) extends RuntimeException(message)
case class RollbackException(ex: Throwable) extends RuntimeException
case class FileTooBigException() extends RuntimeException
case class InvalidStatusException(message: String) extends RuntimeException(message)
case class InvalidStateException(message: String) extends RuntimeException(message)
case class NotFoundException(message: String) extends RuntimeException(message)
case class RollbackException(ex: Throwable) extends RuntimeException
case class FileTooBigException() extends RuntimeException
case class InvalidStatusException(message: String) extends RuntimeException(message)
case class InvalidStateException(message: String) extends RuntimeException(message)
case class TokenRetrievalException(message: String) extends RuntimeException(message)
case class TokenDecodingException(message: String) extends RuntimeException(message)
case class VideoSourceRetrievalException(message: String) extends RuntimeException(message)
case class VideoSourceParsingException(message: String) extends RuntimeException(message)

0 comments on commit 2dc515d

Please sign in to comment.