From 5f7827be4bf20b8ff347394cd1fba3b8409b97a9 Mon Sep 17 00:00:00 2001 From: Lauri Lehmijoki Date: Sat, 28 Jun 2014 06:48:36 +0300 Subject: [PATCH] Print stack trace on --verbose and failure --- changelog.md | 6 ++++++ lib/s3_website/version.rb | 2 +- src/main/scala/s3/website/CloudFront.scala | 5 +++-- src/main/scala/s3/website/Logger.scala | 2 +- src/main/scala/s3/website/S3.scala | 11 ++++++----- src/main/scala/s3/website/package.scala | 22 ++++++++++++---------- 6 files changed, 29 insertions(+), 19 deletions(-) diff --git a/changelog.md b/changelog.md index fd739c4b..b3aef416 100644 --- a/changelog.md +++ b/changelog.md @@ -2,6 +2,12 @@ This project uses [Semantic Versioning](http://semver.org). +## 2.1.13 + +* Print JVM stack trace on error and `--verbose` + + This eases debugging + ## 2.1.12 * Exit with status 1 when encountering an unrecognised CLI option diff --git a/lib/s3_website/version.rb b/lib/s3_website/version.rb index a7b80fa6..dad7bf3d 100644 --- a/lib/s3_website/version.rb +++ b/lib/s3_website/version.rb @@ -1,3 +1,3 @@ module S3Website - VERSION = '2.1.12' + VERSION = '2.1.13' end diff --git a/src/main/scala/s3/website/CloudFront.scala b/src/main/scala/s3/website/CloudFront.scala index 29ade9a7..01c4dd9e 100644 --- a/src/main/scala/s3/website/CloudFront.scala +++ b/src/main/scala/s3/website/CloudFront.scala @@ -1,5 +1,6 @@ package s3.website +import s3.website.ErrorReport._ import s3.website.model.{FileUpdate, Config} import com.amazonaws.services.cloudfront.{AmazonCloudFrontClient, AmazonCloudFront} import com.amazonaws.services.cloudfront.model.{TooManyInvalidationsInProgressException, Paths, InvalidationBatch, CreateInvalidationRequest} @@ -60,8 +61,8 @@ object CloudFront { def reportMessage = s"${Invalidated.renderVerb} ${invalidatedItemsCount ofType "item"} on CloudFront" } - case class FailedInvalidation(error: Throwable) extends FailureReport { - def reportMessage = s"Failed to invalidate the CloudFront distribution (${error.getMessage})" + case class FailedInvalidation(error: Throwable)(implicit logger: Logger) extends ErrorReport { + def reportMessage = errorMessage(s"Failed to invalidate the CloudFront distribution", error) } def awsCloudFrontClient(config: Config) = new AmazonCloudFrontClient(awsCredentials(config)) diff --git a/src/main/scala/s3/website/Logger.scala b/src/main/scala/s3/website/Logger.scala index a04818f6..7b1dba44 100644 --- a/src/main/scala/s3/website/Logger.scala +++ b/src/main/scala/s3/website/Logger.scala @@ -8,7 +8,7 @@ class Logger(val verboseOutput: Boolean) { def fail(msg: String) = log(Failure, msg) def info(report: SuccessReport) = log(Success, report.reportMessage) - def info(report: FailureReport) = fail(report.reportMessage) + def info(report: ErrorReport) = fail(report.reportMessage) def pending(msg: String) = log(Wait, msg) diff --git a/src/main/scala/s3/website/S3.scala b/src/main/scala/s3/website/S3.scala index 10024141..a94ab873 100644 --- a/src/main/scala/s3/website/S3.scala +++ b/src/main/scala/s3/website/S3.scala @@ -1,5 +1,6 @@ package s3.website +import s3.website.ErrorReport.errorMessage import s3.website.model._ import com.amazonaws.services.s3.{AmazonS3, AmazonS3Client} import com.amazonaws.services.s3.model._ @@ -140,7 +141,7 @@ object S3 { type UpdateFutures = Seq[Either[ErrorReport, Future[PushErrorOrSuccess]]] type ErrorOrS3Files = Either[ErrorReport, Seq[S3File]] - sealed trait PushFailureReport extends FailureReport + sealed trait PushFailureReport extends ErrorReport sealed trait PushSuccessReport extends SuccessReport { def s3Key: String } @@ -208,12 +209,12 @@ object S3 { def reportMessage = s"${Deleted.renderVerb} $s3Key" } - case class FailedUpload(s3Key: String, error: Throwable) extends PushFailureReport { - def reportMessage = s"Failed to upload $s3Key (${error.getMessage})" + case class FailedUpload(s3Key: String, error: Throwable)(implicit logger: Logger) extends PushFailureReport { + def reportMessage = errorMessage(s"Failed to upload $s3Key", error) } - case class FailedDelete(s3Key: String, error: Throwable) extends PushFailureReport { - def reportMessage = s"Failed to delete $s3Key (${error.getMessage})" + case class FailedDelete(s3Key: String, error: Throwable)(implicit logger: Logger) extends PushFailureReport { + def reportMessage = errorMessage(s"Failed to delete $s3Key", error) } type S3ClientProvider = (Config) => AmazonS3 diff --git a/src/main/scala/s3/website/package.scala b/src/main/scala/s3/website/package.scala index 28ffa740..feb8b943 100644 --- a/src/main/scala/s3/website/package.scala +++ b/src/main/scala/s3/website/package.scala @@ -13,25 +13,27 @@ package object website { } trait SuccessReport extends Report - trait FailureReport extends Report - trait ErrorReport extends Report object ErrorReport { def apply(t: Throwable)(implicit logger: Logger) = new ErrorReport { - override def reportMessage = { - val extendedReport = - if (logger.verboseOutput) - Some(t.getStackTrace take 5) - else - None - s"${t.getMessage}${extendedReport.fold("")(stackTraceElems => "\n" + stackTraceElems.mkString("\n"))}" - } + override def reportMessage = errorMessage(t) } def apply(msg: String) = new ErrorReport { override def reportMessage = msg } + + def errorMessage(msg: String, t: Throwable)(implicit logger: Logger): String = s"$msg (${errorMessage(t)})" + + def errorMessage(t: Throwable)(implicit logger: Logger): String = { + val extendedReport = + if (logger.verboseOutput) + Some(t.getStackTrace) + else + None + s"${t.getMessage}${extendedReport.fold("")(stackTraceElems => "\n" + stackTraceElems.mkString("\n"))}" + } } trait RetrySetting {