Skip to content

Commit

Permalink
Improve error messaging.
Browse files Browse the repository at this point in the history
  • Loading branch information
zainab-ali committed Feb 3, 2025
1 parent dfbfcfd commit b02ee1a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2024 SiriusXM
*
* 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 snapshot4s

private object ErrorMessages {
val nonExistent: String = "Snapshot does not exist. Run 'sbt snapshot4sPromote' to create it."
val failure: String = "Snapshot not equal. Run 'sbt snapshot4sPromote' to update it."
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private object MunitResultLike {
private def resultToAssertion[A](result: Result[A], loc: Location): Unit = {
result match {
case _: Result.Success[?] => ()
case _: Result.NonExistent[?] => throw Assertions.fail("Snapshot does not exist.")(loc)
case _: Result.NonExistent[?] => throw Assertions.fail(ErrorMessages.nonExistent)(loc)
case Result.Failure(found, snapshot) =>
throw Assertions.fail(diffReport(found.toString, snapshot.toString))(loc)
}
Expand All @@ -44,6 +44,6 @@ private object MunitResultLike {
obtained = found,
expected = expected
)
diff.createReport("Snapshot not equal", printObtainedAsStripMargin = false)
diff.createReport(ErrorMessages.failure, printObtainedAsStripMargin = false)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private object ScalaTestResultLike {
case _: Result.Success[?] => Succeeded
case _: Result.NonExistent[?] =>
throw new TestFailedException(
_ => Some("Snapshot does not exist."),
_ => Some(ErrorMessages.nonExistent),
cause = None,
posOrStackDepthFun = Left(pos),
payload = None,
Expand All @@ -53,7 +53,7 @@ private object ScalaTestResultLike {
case Result.Failure(found, snapshot) =>
val diff = prettifier(found, snapshot)
throw new TestFailedException(
_ => Some(s"Snapshots not equal. Expected: ${diff.right}, but got ${diff.left}"),
_ => Some(s"${ErrorMessages.failure} Expected: ${diff.right}, but got ${diff.left}"),
cause = None,
posOrStackDepthFun = Left(pos),
payload = None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package snapshot4s.weaver

import snapshot4s.ErrorMessages

private object Diff {

import munit.diff.Diff
Expand All @@ -25,6 +27,6 @@ private object Diff {
obtained = found,
expected = expected
)
diff.createReport("Snapshot not equal", printObtainedAsStripMargin = false)
diff.createReport(ErrorMessages.failure, printObtainedAsStripMargin = false)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import _root_.weaver.{AssertionException, Expectations, SourceLocation}
import cats.data.{NonEmptyList, Validated}
import cats.effect.IO

import snapshot4s.{Result, ResultLike}
import snapshot4s.{ErrorMessages, Result, ResultLike}

object WeaverResultLike {

Expand All @@ -40,7 +40,7 @@ object WeaverResultLike {
case _: Result.NonExistent[?] =>
Expectations(
Validated.invalidNel[AssertionException, Unit](
AssertionException("Snapshot does not exist.", NonEmptyList.of(loc))
AssertionException(ErrorMessages.nonExistent, NonEmptyList.of(loc))
)
)
case Result.Failure(found, snapshot) =>
Expand Down

0 comments on commit b02ee1a

Please sign in to comment.