diff --git a/zio-http/shared/src/main/scala/zio/http/codec/HttpContentCodec.scala b/zio-http/shared/src/main/scala/zio/http/codec/HttpContentCodec.scala index 6ae344c15b..ec789611bd 100644 --- a/zio-http/shared/src/main/scala/zio/http/codec/HttpContentCodec.scala +++ b/zio-http/shared/src/main/scala/zio/http/codec/HttpContentCodec.scala @@ -7,7 +7,7 @@ import zio._ import zio.stream.ZPipeline import zio.schema.codec._ -import zio.schema.{ DeriveSchema, Schema } +import zio.schema.{DeriveSchema, Schema} import zio.http.Header.Accept.MediaTypeWithQFactor import zio.http._ @@ -34,7 +34,7 @@ sealed trait HttpContentCodec[A] { self => request.body.asChunk.flatMap { bytes => ZIO.fromEither(codec.codec(config).decode(bytes)) } - case None => + case None => ZIO.fail(throw new IllegalArgumentException(s"No codec found for content type $contentType")) } } @@ -49,7 +49,7 @@ sealed trait HttpContentCodec[A] { self => response.body.asChunk.flatMap { bytes => ZIO.fromEither(codec.codec(config).decode(bytes)) } - case None => + case None => ZIO.fail(throw new IllegalArgumentException(s"No codec found for content type $contentType")) } } @@ -102,7 +102,7 @@ sealed trait HttpContentCodec[A] { self => } private[http] def chooseFirstOrDefault( - mediaTypes: Chunk[MediaTypeWithQFactor] + mediaTypes: Chunk[MediaTypeWithQFactor], ): (MediaType, BinaryCodecWithSchema[A]) = if (mediaTypes.isEmpty) { (defaultMediaType, defaultBinaryCodecWithSchema) @@ -157,17 +157,17 @@ object HttpContentCodec { def from[A]( codec: (MediaType, BinaryCodecWithSchema[A]), - codecs: (MediaType, BinaryCodecWithSchema[A])* + codecs: (MediaType, BinaryCodecWithSchema[A])*, ): HttpContentCodec[A] = Default(ListMap((codec +: codecs): _*)) implicit def fromSchema[A](implicit schema: Schema[A]): HttpContentCodec[A] = schemaCache - .getOrElseUpdate(schema, _: Schema[A] => json.only[A] ++ protobuf.only[A] ++ text.only[A]) + .getOrElseUpdate(schema, json.only[A] ++ protobuf.only[A] ++ text.only[A]) .asInstanceOf[HttpContentCodec[A]] final private case class Default[A]( - choices: ListMap[MediaType, BinaryCodecWithSchema[A]] + choices: ListMap[MediaType, BinaryCodecWithSchema[A]], ) extends HttpContentCodec[A] final case class Filtered[A](codec: HttpContentCodec[A], mediaType: MediaType) extends HttpContentCodec[A] { @@ -176,8 +176,8 @@ object HttpContentCodec { mediaType -> codec .lookup(mediaType) .getOrElse( - throw new IllegalArgumentException(s"MediaType $mediaType is not supported by $codec") - ) + throw new IllegalArgumentException(s"MediaType $mediaType is not supported by $codec"), + ), ) } @@ -215,47 +215,49 @@ object HttpContentCodec { case (Some(name), Some(message)) => Right(HttpCodecError.CustomError(name, message)) case _ => Left("Could not extract name and message from the DOM") } - }, { + }, + { case HttpCodecError.CustomError(name, message) => Right( html( body( h1("Codec Error"), p("There was an error en-/decoding the request/response"), - p(name, idAttr := "name"), - p(message, idAttr := "message") - ) - ) + p(name, idAttr := "name"), + p(message, idAttr := "message"), + ), + ), ) - case e: HttpCodecError => + case e: HttpCodecError => Right( html( body( h1("Codec Error"), p("There was an error en-/decoding the request/response"), p(e.productPrefix, idAttr := "name"), - p(e.getMessage(), idAttr := "message") - ) - ) + p(e.getMessage(), idAttr := "message"), + ), + ), ) - } + }, ) private val defaultCodecErrorSchema: Schema[HttpCodecError] = Schema[DefaultCodecError].transformOrFail[HttpCodecError]( - codecError => Right(HttpCodecError.CustomError(codecError.name, codecError.message)), { + codecError => Right(HttpCodecError.CustomError(codecError.name, codecError.message)), + { case HttpCodecError.CustomError(name, message) => Right(DefaultCodecError(name, message)) case e: HttpCodecError => Right(DefaultCodecError(e.productPrefix, e.getMessage())) - } + }, ) private val defaultHttpContentCodec: HttpContentCodec[HttpCodecError] = HttpContentCodec.from( - MediaType.text.`html` -> BinaryCodecWithSchema(TextBinaryCodec.fromSchema(domBasedSchema), domBasedSchema), + MediaType.text.`html` -> BinaryCodecWithSchema(TextBinaryCodec.fromSchema(domBasedSchema), domBasedSchema), MediaType.application.json -> BinaryCodecWithSchema( JsonCodec.schemaBasedBinaryCodec(defaultCodecErrorSchema), - defaultCodecErrorSchema - ) + defaultCodecErrorSchema, + ), ) val responseErrorCodec: HttpCodec[HttpCodecType.ResponseType, HttpCodecError] = @@ -270,11 +272,11 @@ object HttpContentCodec { BinaryCodecWithSchema( config => JsonCodec.schemaBasedBinaryCodec[A]( - JsonCodec.Config(ignoreEmptyCollections = config.ignoreEmptyCollections) + JsonCodec.Config(ignoreEmptyCollections = config.ignoreEmptyCollections), )(schema), - schema - ) - ) + schema, + ), + ), ) } @@ -284,8 +286,8 @@ object HttpContentCodec { Default( ListMap( MediaType.parseCustomMediaType("application/protobuf").get -> - BinaryCodecWithSchema(ProtobufCodec.protobufCodec[A], schema) - ) + BinaryCodecWithSchema(ProtobufCodec.protobufCodec[A], schema), + ), ) } @@ -294,11 +296,11 @@ object HttpContentCodec { def only[A](implicit schema: Schema[A]): HttpContentCodec[A] = Default( ListMap( - MediaType.text.`plain` -> + MediaType.text.`plain` -> BinaryCodecWithSchema(zio.http.codec.internal.TextBinaryCodec.fromSchema[A](schema), schema), MediaType.application.`octet-stream` -> - BinaryCodecWithSchema(zio.http.codec.internal.TextBinaryCodec.fromSchema[A](schema), schema) - ) + BinaryCodecWithSchema(zio.http.codec.internal.TextBinaryCodec.fromSchema[A](schema), schema), + ), ) } @@ -317,12 +319,12 @@ object HttpContentCodec { } implicit val byteChunkCodec: HttpContentCodec[Chunk[Byte]] = { - HttpContentCodec( + Default( ListMap( MediaType.allMediaTypes .filter(_.binary) - .map(mt => mt -> BinaryCodecWithSchema(ByteChunkBinaryCodec, Schema.chunk[Byte])): _* - ) + .map(mt => mt -> BinaryCodecWithSchema(ByteChunkBinaryCodec, Schema.chunk[Byte])): _*, + ), ) } @@ -341,12 +343,12 @@ object HttpContentCodec { } implicit val byteCodec: HttpContentCodec[Byte] = { - HttpContentCodec( + Default( ListMap( MediaType.allMediaTypes .filter(_.binary) - .map(mt => mt -> BinaryCodecWithSchema(ByteBinaryCodec, Schema[Byte])): _* - ) + .map(mt => mt -> BinaryCodecWithSchema(ByteBinaryCodec, Schema[Byte])): _*, + ), ) } }