Skip to content

Commit

Permalink
Merge pull request #9 from commercetools/error-model
Browse files Browse the repository at this point in the history
Add specific error model
  • Loading branch information
satabin authored Apr 4, 2024
2 parents 5c6f507 + 3af3cf2 commit 47d4969
Show file tree
Hide file tree
Showing 16 changed files with 257 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,28 @@ class SQSAdministration[F[_]](client: SqsAsyncClient, getQueueUrl: String => F[S
.build())
}
}.void
.adaptError(makeQueueException(_, name))

override def delete(name: String): F[Unit] =
getQueueUrl(name).flatMap { queueUrl =>
F.fromCompletableFuture {
F.delay {
client.deleteQueue(
DeleteQueueRequest
.builder()
.queueUrl(queueUrl)
.build())
getQueueUrl(name)
.flatMap { queueUrl =>
F.fromCompletableFuture {
F.delay {
client.deleteQueue(
DeleteQueueRequest
.builder()
.queueUrl(queueUrl)
.build())
}
}
}
}.void
.void
.adaptError(makeQueueException(_, name))

override def exists(name: String): F[Boolean] =
getQueueUrl(name).as(true).recover { case _: QueueDoesNotExistException => false }
getQueueUrl(name)
.as(true)
.recover { case _: QueueDoesNotExistException => false }
.adaptError(makeQueueException(_, name))

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.commercetools.queue.aws.sqs

import cats.effect.{Async, Resource}
import cats.syntax.functor._
import cats.syntax.monadError._
import com.commercetools.queue.{Deserializer, QueueAdministration, QueueClient, QueuePublisher, QueueSubscriber, Serializer}
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider
import software.amazon.awssdk.http.async.SdkAsyncHttpClient
Expand All @@ -35,15 +36,16 @@ class SQSClient[F[_]] private (client: SqsAsyncClient)(implicit F: Async[F]) ext
client.getQueueUrl(GetQueueUrlRequest.builder().queueName(name).build())
}
}.map(_.queueUrl)
.adaptError(makeQueueException(_, name))

override def administration: QueueAdministration[F] =
new SQSAdministration(client, getQueueUrl(_))

override def publish[T: Serializer](name: String): QueuePublisher[F, T] =
new SQSPublisher(client, getQueueUrl(name))
new SQSPublisher(client, name, getQueueUrl(name))

override def subscribe[T: Deserializer](name: String): QueueSubscriber[F, T] =
new SQSSubscriber[F, T](getQueueUrl(name), client)
new SQSSubscriber[F, T](client, name, getQueueUrl(name))

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ package com.commercetools.queue.aws.sqs

import cats.effect.Async
import cats.syntax.functor._
import com.commercetools.queue.MessageContext
import cats.syntax.monadError._
import com.commercetools.queue.{Action, MessageContext}
import software.amazon.awssdk.services.sqs.SqsAsyncClient
import software.amazon.awssdk.services.sqs.model.{ChangeMessageVisibilityRequest, DeleteMessageRequest}

Expand All @@ -31,6 +32,7 @@ class SQSMessageContext[F[_], T](
receiptHandle: String,
val messageId: String,
lockTTL: Int,
queueName: String,
queueUrl: String,
client: SqsAsyncClient
)(implicit F: Async[F])
Expand All @@ -42,6 +44,7 @@ class SQSMessageContext[F[_], T](
client.deleteMessage(DeleteMessageRequest.builder().queueUrl(queueUrl).receiptHandle(receiptHandle).build())
}
}.void
.adaptError(makeMessageException(_, queueName, messageId, Action.Ack))

override def nack(): F[Unit] =
F.fromCompletableFuture {
Expand All @@ -55,6 +58,7 @@ class SQSMessageContext[F[_], T](
.build())
}
}.void
.adaptError(makeMessageException(_, queueName, messageId, Action.Nack))

override def extendLock(): F[Unit] =
F.fromCompletableFuture {
Expand All @@ -68,5 +72,6 @@ class SQSMessageContext[F[_], T](
.build())
}
}.void
.adaptError(makeMessageException(_, queueName, messageId, Action.ExtendLock))

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ import software.amazon.awssdk.services.sqs.SqsAsyncClient

class SQSPublisher[F[_], T](
client: SqsAsyncClient,
queueName: String,
getQueueUrl: F[String]
)(implicit
F: Async[F],
serializer: Serializer[T])
extends QueuePublisher[F, T] {

override def pusher: Resource[F, QueuePusher[F, T]] =
Resource.eval(getQueueUrl).map(new SQSPusher(client, _))
Resource.eval(getQueueUrl).map(new SQSPusher(client, queueName, _))

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
package com.commercetools.queue.aws.sqs

import cats.effect.Async
import cats.syntax.all._
import cats.syntax.either._
import cats.syntax.flatMap._
import cats.syntax.functor._
import cats.syntax.monadError._
import com.commercetools.queue.{Deserializer, MessageContext, QueuePuller}
import fs2.Chunk
import software.amazon.awssdk.services.sqs.SqsAsyncClient
Expand All @@ -29,6 +32,7 @@ import scala.jdk.CollectionConverters._

class SQSPuller[F[_], T](
client: SqsAsyncClient,
queueName: String,
queueUrl: String,
lockTTL: Int
)(implicit
Expand All @@ -50,19 +54,24 @@ class SQSPuller[F[_], T](
.build())
}
}.flatMap { response =>
Chunk.iterator(response.messages().iterator().asScala).traverse { message =>
deserializer.deserialize(message.body()).liftTo[F].map { payload =>
new SQSMessageContext(
payload,
Instant.ofEpochMilli(message.attributes().get(MessageSystemAttributeName.SENT_TIMESTAMP).toLong),
message.attributesAsStrings().asScala.toMap,
message.receiptHandle(),
message.messageId(),
lockTTL,
queueUrl,
client
)
Chunk
.iterator(response.messages().iterator().asScala)
.traverse { message =>
deserializer.deserialize(message.body()).liftTo[F].map { payload =>
new SQSMessageContext(
payload = payload,
enqueuedAt =
Instant.ofEpochMilli(message.attributes().get(MessageSystemAttributeName.SENT_TIMESTAMP).toLong),
metadata = message.attributesAsStrings().asScala.toMap,
receiptHandle = message.receiptHandle(),
messageId = message.messageId(),
lockTTL = lockTTL,
queueName = queueName,
queueUrl = queueUrl,
client = client
)
}
}
}
}
}.widen[Chunk[MessageContext[F, T]]]
.adaptError(makePullQueueException(_, queueName))
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,21 @@ package com.commercetools.queue.aws.sqs

import cats.effect.Async
import cats.syntax.functor._
import cats.syntax.monadError._
import com.commercetools.queue.{QueuePusher, Serializer}
import software.amazon.awssdk.services.sqs.SqsAsyncClient
import software.amazon.awssdk.services.sqs.model.{SendMessageBatchRequest, SendMessageBatchRequestEntry, SendMessageRequest}

import scala.concurrent.duration.FiniteDuration
import scala.jdk.CollectionConverters._

class SQSPusher[F[_], T](client: SqsAsyncClient, queueUrl: String)(implicit serializer: Serializer[T], F: Async[F])
class SQSPusher[F[_], T](
client: SqsAsyncClient,
queueName: String,
queueUrl: String
)(implicit
serializer: Serializer[T],
F: Async[F])
extends QueuePusher[F, T] {

override def push(message: T, delay: Option[FiniteDuration]): F[Unit] =
Expand All @@ -40,6 +47,7 @@ class SQSPusher[F[_], T](client: SqsAsyncClient, queueUrl: String)(implicit seri
.build())
}
}.void
.adaptError(makePushQueueException(_, queueName))

override def push(messages: List[T], delay: Option[FiniteDuration]): F[Unit] =
F.fromCompletableFuture {
Expand All @@ -59,5 +67,6 @@ class SQSPusher[F[_], T](client: SqsAsyncClient, queueUrl: String)(implicit seri
.build())
}
}.void
.adaptError(makePushQueueException(_, queueName))

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ import software.amazon.awssdk.services.sqs.SqsAsyncClient
import software.amazon.awssdk.services.sqs.model.{GetQueueAttributesRequest, QueueAttributeName}

class SQSSubscriber[F[_], T](
getQueueUrl: F[String],
client: SqsAsyncClient
client: SqsAsyncClient,
queueName: String,
getQueueUrl: F[String]
)(implicit
F: Async[F],
deserializer: Deserializer[T])
Expand All @@ -41,13 +42,14 @@ class SQSSubscriber[F[_], T](
.build())
}
}.map(_.attributes().get(QueueAttributeName.VISIBILITY_TIMEOUT).toInt)
.adaptError(makeQueueException(_, queueName))

override def puller: Resource[F, QueuePuller[F, T]] =
Resource.eval {
for {
queueUrl <- getQueueUrl
lockTTL <- getLockTTL(queueUrl)
} yield new SQSPuller(client, queueUrl, lockTTL)
} yield new SQSPuller(client, queueName, queueUrl, lockTTL)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2024 Commercetools GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.commercetools.queue.aws

import com.commercetools.queue.{Action, CannotPullException, CannotPushException, MessageException, QueueAlreadyExistException, QueueDoesNotExistException, QueueException, UnknownQueueException}
import software.amazon.awssdk.services.sqs.model.{QueueDoesNotExistException => AwsQueueDoesNotExistException, QueueNameExistsException}

package object sqs {

def makeQueueException(t: Throwable, queueName: String): QueueException = t match {
case _: AwsQueueDoesNotExistException => QueueDoesNotExistException(queueName, t)
case _: QueueNameExistsException => QueueAlreadyExistException(queueName, t)
case t: QueueException => t
case _ => UnknownQueueException(queueName, t)
}

def makePushQueueException(t: Throwable, queueName: String): QueueException =
new CannotPushException(queueName, makeQueueException(t, queueName))

def makePullQueueException(t: Throwable, queueName: String): QueueException =
new CannotPullException(queueName, makeQueueException(t, queueName))

def makeMessageException(t: Throwable, queueName: String, msgId: String, action: Action): QueueException =
new MessageException(msgId = msgId, action = action, inner = makeQueueException(t, queueName))

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.commercetools.queue.azure.servicebus

import cats.effect.Async
import cats.syntax.functor._
import cats.syntax.monadError._
import com.azure.messaging.servicebus.administration.ServiceBusAdministrationClient
import com.azure.messaging.servicebus.administration.models.CreateQueueOptions
import com.commercetools.queue.QueueAdministration
Expand All @@ -36,11 +37,16 @@ class ServiceBusAdministration[F[_]](client: ServiceBusAdministrationClient)(imp
.setDefaultMessageTimeToLive(Duration.ofMillis(messageTTL.toMillis))
.setLockDuration(Duration.ofMillis(lockTTL.toMillis))))
.void
.adaptError(makeQueueException(_, name))

override def delete(name: String): F[Unit] =
F.blocking(client.deleteQueue(name)).void
F.blocking(client.deleteQueue(name))
.void
.adaptError(makeQueueException(_, name))

override def exists(name: String): F[Boolean] =
F.blocking(client.getQueueExists(name)).map(_.booleanValue)
F.blocking(client.getQueueExists(name))
.map(_.booleanValue)
.adaptError(makeQueueException(_, name))

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@

package com.commercetools.queue.azure.servicebus

import cats.effect.kernel.Async
import cats.syntax.all._
import cats.effect.Async
import cats.syntax.either._
import cats.syntax.flatMap._
import cats.syntax.functor._
import cats.syntax.monadError._
import com.azure.messaging.servicebus.ServiceBusReceiverClient
import com.commercetools.queue.{Deserializer, MessageContext, QueuePuller}
import fs2.Chunk
Expand All @@ -27,7 +30,8 @@ import scala.concurrent.duration.FiniteDuration
import scala.jdk.CollectionConverters._

class ServiceBusPuller[F[_], Data](
receiver: ServiceBusReceiverClient
receiver: ServiceBusReceiverClient,
queueName: String
)(implicit
F: Async[F],
deserializer: Deserializer[Data])
Expand All @@ -44,5 +48,7 @@ class ServiceBusPuller[F[_], Data](
new ServiceBusMessageContext(data, sbMessage, receiver)
})
}
.widen[Chunk[MessageContext[F, Data]]]
.adaptError(makePullQueueException(_, queueName))

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ import java.time.ZoneOffset
import scala.concurrent.duration.FiniteDuration
import scala.jdk.CollectionConverters._

class ServiceBusPusher[F[_], Data](sender: ServiceBusSenderClient)(implicit serializer: Serializer[Data], F: Async[F])
class ServiceBusPusher[F[_], Data](
sender: ServiceBusSenderClient,
queueName: String
)(implicit
serializer: Serializer[Data],
F: Async[F])
extends QueuePusher[F, Data] {

override def push(message: Data, delay: Option[FiniteDuration]): F[Unit] = {
Expand All @@ -34,7 +39,9 @@ class ServiceBusPusher[F[_], Data](sender: ServiceBusSenderClient)(implicit seri
F.realTimeInstant
.map(now => sbMessage.setScheduledEnqueueTime(now.plusMillis(delay.toMillis).atOffset(ZoneOffset.UTC)))
} *>
F.blocking(sender.sendMessage(sbMessage)).void
F.blocking(sender.sendMessage(sbMessage))
.void
.adaptError(makePushQueueException(_, queueName))
}

override def push(messages: List[Data], delay: Option[FiniteDuration]): F[Unit] = {
Expand All @@ -46,7 +53,9 @@ class ServiceBusPusher[F[_], Data](sender: ServiceBusSenderClient)(implicit seri
}
}
} *>
F.blocking(sender.sendMessages(sbMessages.asJava)).void
F.blocking(sender.sendMessages(sbMessages.asJava))
.void
.adaptError(makePushQueueException(_, queueName))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ class ServiceBusQueuePublisher[F[_], Data](
} { s =>
F.delay(s.close())
}
.map(new ServiceBusPusher(_))
.map(new ServiceBusPusher(_, queueName))

}
Loading

0 comments on commit 47d4969

Please sign in to comment.