Skip to content

Commit

Permalink
implement getMessage in Smithy4sThrowable (#1572)
Browse files Browse the repository at this point in the history
  • Loading branch information
lewisjkl authored Aug 5, 2024
1 parent 858f1f4 commit 7afbb5c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ When adding entries, please treat them as if they could end up in a release any

Thank you!

# 0.18.24

* Add `getMessage` implementation in `Smithy4sThrowable` which will be overridden in cases where the error structure contains a message field, but otherwise will be used to prevent a useless `null` result when `getMessage` is called.

# 0.18.23

## Validated newtypes [#1454](https://github.com/disneystreaming/smithy4s/pull/1454)
Expand Down
13 changes: 11 additions & 2 deletions modules/bootstrapped/test/src/smithy4s/ErrorMessageTraitSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,20 @@ class ErrorMessageTraitSpec extends FunSuite {
)
}

test("Generated getMessage") {
test("Generated - no message") {
val e = ClientError(400, "oopsy")

val expected = "smithy4s.example.ClientError(400, oopsy)"
expect.eql(e.getMessage, null)
expect.eql(e.getMessage, expected)
expect.eql(e.toString, s"smithy4s.example.ClientError: $expected")
}

test("Generated - has message") {
val e =
ErrorCustomTypeMessage(Some(CustomErrorMessageType("This is a test.")))

val expected = "smithy4s.example.ErrorCustomTypeMessage: This is a test."
expect.eql(e.getMessage, "This is a test.")
expect.eql(e.toString, expected)
}

Expand Down
20 changes: 13 additions & 7 deletions modules/core/src/smithy4s/Smithy4sThrowable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,8 @@ package smithy4s

trait Smithy4sThrowable extends Throwable { self: Product =>

/**
* implementing toString, because implementing getMessage
* lead to `smithy4s.example.ClientError: smithy4s.example.ClientError(400, "oops")`
* which felt weird
*/
override def toString(): String = {
private def show(message: String): String = {
val name = getClass().getName()
val message = getLocalizedMessage()
if (message == null) {
val sb = new StringBuilder()
sb.append(name)
Expand All @@ -41,4 +35,16 @@ trait Smithy4sThrowable extends Throwable { self: Product =>
s"$name: $message"
}
}

override def getMessage(): String = this.show(message = null)

/**
* implementing toString, because implementing getMessage
* lead to `smithy4s.example.ClientError: smithy4s.example.ClientError(400, "oops")`
* which felt weird
*/
override def toString(): String = {
val message = getLocalizedMessage()
this.show(message = message)
}
}

0 comments on commit 7afbb5c

Please sign in to comment.