withSizeLimit Akka directive not working for nested Future method calls

47 Views Asked by At

I am trying to upload a file to S3 & then to Google Cloud. However I also want to check the file size before upload. It should be <=3Mb. I have created a Route and it has a withSizeLimit Directive.

fileUploadRoute { (request) =>
    authorizeUser { _ =>                        
        withSizeLimit(3000000) {
          extractDataBytes { dataBytes =>
            complete(uploadFileToCloud(dataBytes))
          }
        }
      }
    
  
private def uploadFileToCloud(
    content: Source[ByteString, Any]
): Future[FileUploadResult] = {
//Upload file to S3
uploadFileto S3(dataBytes)
.flatMap { awsResponse =>
//Upload file to GCP
    uploadFileToGCP(dataBytes)
    .flatMap { gcpResponse =>
    //do something
}
}
.recoverWith {
case (ex: Exception) =>
    //do something
}
}

If file size is greater than 3Mb, recoverWith block executes but the standard client error from the route is not being thrown. Am I using the correct directive?

Assume I can consume the incoming stream twice.

0

There are 0 best solutions below