-
Notifications
You must be signed in to change notification settings - Fork 401
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Explicit and customizable error messages for Endpoint BadRequest (#2650) #2714
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c2f6942
Explicit and customizable error messages for Endpoint BadRequest (#2650)
987Nabil 969df3d
Update zio-http/shared/src/main/scala/zio/http/codec/BinaryCodecWithS…
987Nabil 0d97412
Review changes
987Nabil 1fedb49
Integrate changes from main
987Nabil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
zio-http/jvm/src/test/scala/zio/http/endpoint/BadRequestSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package zio.http.endpoint | ||
|
||
import zio.test._ | ||
|
||
import zio.schema.codec.JsonCodec | ||
import zio.schema.{DeriveSchema, Schema} | ||
|
||
import zio.http._ | ||
import zio.http.codec._ | ||
import zio.http.template._ | ||
|
||
object BadRequestSpec extends ZIOSpecDefault { | ||
|
||
override def spec = | ||
suite("BadRequestSpec")( | ||
test("should return html rendered error message by default for html accept header") { | ||
val endpoint = Endpoint(Method.GET / "test") | ||
.query(QueryCodec.queryInt("age")) | ||
.out[Unit] | ||
val route = endpoint.implement(handler((_: Int) => ())) | ||
val app = route.toHttpApp | ||
val request = | ||
Request(method = Method.GET, url = url"/test?age=1&age=2").addHeader(Header.Accept(MediaType.text.`html`)) | ||
val expectedBody = | ||
html( | ||
body( | ||
h1("Codec Error"), | ||
p("There was an error en-/decoding the request/response"), | ||
p("SchemaTransformationFailure", idAttr := "name"), | ||
p("Expected single value for query parameter age, but got 2 instead", idAttr := "message"), | ||
), | ||
) | ||
for { | ||
response <- app.runZIO(request) | ||
body <- response.body.asString | ||
} yield assertTrue(body == expectedBody.encode.toString) | ||
}, | ||
test("should return json rendered error message by default for json accept header") { | ||
val endpoint = Endpoint(Method.GET / "test") | ||
.query(QueryCodec.queryInt("age")) | ||
.out[Unit] | ||
val route = endpoint.implement(handler((_: Int) => ())) | ||
val app = route.toHttpApp | ||
val request = | ||
Request(method = Method.GET, url = url"/test?age=1&age=2") | ||
.addHeader(Header.Accept(MediaType.application.json)) | ||
val expectedBody = | ||
"""{"name":"SchemaTransformationFailure","message":"Expected single value for query parameter age, but got 2 instead"}""" | ||
for { | ||
response <- app.runZIO(request) | ||
body <- response.body.asString | ||
} yield assertTrue(body == expectedBody) | ||
}, | ||
test("should return json rendered error message by default as fallback for unsupported accept header") { | ||
val endpoint = Endpoint(Method.GET / "test") | ||
.query(QueryCodec.queryInt("age")) | ||
.out[Unit] | ||
val route = endpoint.implement(handler((_: Int) => ())) | ||
val app = route.toHttpApp | ||
val request = | ||
Request(method = Method.GET, url = url"/test?age=1&age=2") | ||
.addHeader(Header.Accept(MediaType.application.`atf`)) | ||
val expectedBody = | ||
"""{"name":"SchemaTransformationFailure","message":"Expected single value for query parameter age, but got 2 instead"}""" | ||
for { | ||
response <- app.runZIO(request) | ||
body <- response.body.asString | ||
} yield assertTrue(body == expectedBody) | ||
}, | ||
test("should return empty body after calling Endpoint#emptyErrorResponse") { | ||
val endpoint = Endpoint(Method.GET / "test") | ||
.query(QueryCodec.queryInt("age")) | ||
.out[Unit] | ||
.emptyErrorResponse | ||
val route = endpoint.implement(handler((_: Int) => ())) | ||
val app = route.toHttpApp | ||
val request = | ||
Request(method = Method.GET, url = url"/test?age=1&age=2") | ||
.addHeader(Header.Accept(MediaType.application.`atf`)) | ||
val expectedBody = "" | ||
for { | ||
response <- app.runZIO(request) | ||
body <- response.body.asString | ||
} yield assertTrue(body == expectedBody) | ||
}, | ||
test("should return custom error message") { | ||
val endpoint = Endpoint(Method.GET / "test") | ||
.query(QueryCodec.queryInt("age")) | ||
.out[Unit] | ||
val route = endpoint.implement(handler((_: Int) => ())) | ||
val app = route.toHttpApp | ||
val request = | ||
Request(method = Method.GET, url = url"/test?age=1&age=2") | ||
.addHeader(Header.Accept(MediaType.application.json)) | ||
val expectedBody = | ||
"""{"name":"SchemaTransformationFailure","message":"Expected single value for query parameter age, but got 2 instead"}""" | ||
for { | ||
response <- app.runZIO(request) | ||
body <- response.body.asString | ||
} yield assertTrue(body == expectedBody) | ||
}, | ||
test("should use custom error codec over default error codec") { | ||
val endpoint = Endpoint(Method.GET / "test") | ||
.query(QueryCodec.queryInt("age")) | ||
.out[Unit] | ||
.outCodecError(default) | ||
val route = endpoint.implement(handler((_: Int) => ())) | ||
val app = route.toHttpApp | ||
val request = | ||
Request(method = Method.GET, url = url"/test?age=1&age=2") | ||
.addHeader(Header.Accept(MediaType.application.json)) | ||
val expectedBody = | ||
"""{"name2":"SchemaTransformationFailure","message2":"Expected single value for query parameter age, but got 2 instead"}""" | ||
for { | ||
response <- app.runZIO(request) | ||
body <- response.body.asString | ||
} yield assertTrue(body == expectedBody) | ||
}, | ||
) | ||
|
||
private val defaultCodecErrorSchema: Schema[HttpCodecError] = | ||
Schema[DefaultCodecError2].transformOrFail[HttpCodecError]( | ||
codecError => Right(HttpCodecError.CustomError(codecError.name2, codecError.message2)), | ||
{ | ||
case HttpCodecError.CustomError(name, message) => Right(DefaultCodecError2(name, message)) | ||
case e: HttpCodecError => Right(DefaultCodecError2(e.productPrefix, e.getMessage())) | ||
}, | ||
) | ||
|
||
private val testHttpContentCodec: HttpContentCodec[HttpCodecError] = | ||
HttpContentCodec.from( | ||
MediaType.application.json -> BinaryCodecWithSchema( | ||
JsonCodec.schemaBasedBinaryCodec(defaultCodecErrorSchema), | ||
defaultCodecErrorSchema, | ||
), | ||
) | ||
|
||
val default: HttpCodec[HttpCodecType.ResponseType, HttpCodecError] = | ||
ContentCodec.content(testHttpContentCodec) ++ StatusCodec.BadRequest | ||
|
||
final case class DefaultCodecError2(name2: String, message2: String) | ||
|
||
private object DefaultCodecError2 { | ||
implicit val schema: Schema[DefaultCodecError2] = DeriveSchema.gen[DefaultCodecError2] | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
zio-http/shared/src/main/scala/zio/http/codec/BinaryCodecWithSchema.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package zio.http.codec | ||
|
||
import zio.schema.Schema | ||
import zio.schema.codec.BinaryCodec | ||
|
||
final case class BinaryCodecWithSchema[A](codec: BinaryCodec[A], schema: Schema[A]) | ||
|
||
object BinaryCodecWithSchema { | ||
def fromBinaryCodec[A](codec: BinaryCodec[A])(implicit schema: Schema[A]): BinaryCodecWithSchema[A] = | ||
BinaryCodecWithSchema(codec, schema) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -334,7 +334,7 @@ object HttpCodec extends ContentCodecs with HeaderCodecs with MethodCodecs with | |
def unused: HttpCodec[Any, ZNothing] = Halt | ||
|
||
final case class Enumeration[Value](unit: Unit) extends AnyVal { | ||
def apply[AtomTypes, Sub1 <: Value: ClassTag, Sub2 <: Value: ClassTag]( | ||
def f2[AtomTypes, Sub1 <: Value: ClassTag, Sub2 <: Value: ClassTag]( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why were these all renamed?? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After changing the order, the compiler could not infer the right apply. So I gave them explicit names. |
||
codec1: HttpCodec[AtomTypes, Sub1], | ||
codec2: HttpCodec[AtomTypes, Sub2], | ||
): HttpCodec[AtomTypes, Value] = | ||
|
@@ -346,7 +346,7 @@ object HttpCodec extends ContentCodecs with HeaderCodecs with MethodCodecs with | |
}, | ||
) | ||
|
||
def apply[AtomTypes, Sub1 <: Value: ClassTag, Sub2 <: Value: ClassTag, Sub3 <: Value: ClassTag]( | ||
def f3[AtomTypes, Sub1 <: Value: ClassTag, Sub2 <: Value: ClassTag, Sub3 <: Value: ClassTag]( | ||
codec1: HttpCodec[AtomTypes, Sub1], | ||
codec2: HttpCodec[AtomTypes, Sub2], | ||
codec3: HttpCodec[AtomTypes, Sub3], | ||
|
@@ -360,7 +360,7 @@ object HttpCodec extends ContentCodecs with HeaderCodecs with MethodCodecs with | |
}, | ||
) | ||
|
||
def apply[ | ||
def f4[ | ||
AtomTypes, | ||
Sub1 <: Value: ClassTag, | ||
Sub2 <: Value: ClassTag, | ||
|
@@ -384,7 +384,7 @@ object HttpCodec extends ContentCodecs with HeaderCodecs with MethodCodecs with | |
}, | ||
) | ||
|
||
def apply[ | ||
def f5[ | ||
AtomTypes, | ||
Sub1 <: Value: ClassTag, | ||
Sub2 <: Value: ClassTag, | ||
|
@@ -411,7 +411,7 @@ object HttpCodec extends ContentCodecs with HeaderCodecs with MethodCodecs with | |
}, | ||
) | ||
|
||
def apply[ | ||
def f6[ | ||
AtomTypes, | ||
Sub1 <: Value: ClassTag, | ||
Sub2 <: Value: ClassTag, | ||
|
@@ -441,7 +441,7 @@ object HttpCodec extends ContentCodecs with HeaderCodecs with MethodCodecs with | |
}, | ||
) | ||
|
||
def apply[ | ||
def f7[ | ||
AtomTypes, | ||
Sub1 <: Value: ClassTag, | ||
Sub2 <: Value: ClassTag, | ||
|
@@ -474,7 +474,7 @@ object HttpCodec extends ContentCodecs with HeaderCodecs with MethodCodecs with | |
}, | ||
) | ||
|
||
def apply[ | ||
def f8[ | ||
AtomTypes, | ||
Sub1 <: Value: ClassTag, | ||
Sub2 <: Value: ClassTag, | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer a 2nd matches, rather than introducing a boolean parameter with a default. Maybe
matchesIgnoreParams
.