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 e317ad3a3a..eac55e3f5d 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")) } } @@ -77,11 +77,10 @@ sealed trait HttpContentCodec[A] { self => } def only(mediaType: MediaType): HttpContentCodec[A] = - Filtered(this , mediaType) + Filtered(this, mediaType) def only(mediaType: Option[MediaType]): HttpContentCodec[A] = mediaType.map(only).getOrElse(self) - } private[http] def chooseFirst(mediaTypes: Chunk[MediaTypeWithQFactor]): (MediaType, BinaryCodecWithSchema[A]) = if (mediaTypes.isEmpty) { @@ -103,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) @@ -158,7 +157,7 @@ object HttpContentCodec { def from[A]( codec: (MediaType, BinaryCodecWithSchema[A]), - codecs: (MediaType, BinaryCodecWithSchema[A])*, + codecs: (MediaType, BinaryCodecWithSchema[A])* ): HttpContentCodec[A] = Default(ListMap((codec +: codecs): _*)) @@ -168,7 +167,7 @@ object HttpContentCodec { .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] { @@ -177,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") + ) ) } @@ -216,49 +215,47 @@ 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] = @@ -266,46 +263,43 @@ object HttpContentCodec { object json { - def only[A](implicit schema: Schema[A]): HttpContentCodec[A] = { + def only[A](implicit schema: Schema[A]): HttpContentCodec[A] = Default( ListMap( MediaType.application.`json` -> BinaryCodecWithSchema( config => JsonCodec.schemaBasedBinaryCodec[A]( - JsonCodec.Config(ignoreEmptyCollections = config.ignoreEmptyCollections), + JsonCodec.Config(ignoreEmptyCollections = config.ignoreEmptyCollections) )(schema), - schema, - ), - ), + schema + ) + ) ) - } } object protobuf { - def only[A](implicit schema: Schema[A]): HttpContentCodec[A] = { + def only[A](implicit schema: Schema[A]): HttpContentCodec[A] = Default( ListMap( MediaType.parseCustomMediaType("application/protobuf").get -> - BinaryCodecWithSchema(ProtobufCodec.protobufCodec[A], schema), - ), + BinaryCodecWithSchema(ProtobufCodec.protobufCodec[A], schema) + ) ) - } } object text { - def only[A](implicit schema: Schema[A]): HttpContentCodec[A] = { + 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) + ) ) - } } private val ByteChunkBinaryCodec: BinaryCodec[Chunk[Byte]] = new BinaryCodec[Chunk[Byte]] { @@ -327,8 +321,8 @@ object HttpContentCodec { ListMap( MediaType.allMediaTypes .filter(_.binary) - .map(mt => mt -> BinaryCodecWithSchema(ByteChunkBinaryCodec, Schema.chunk[Byte])): _*, - ), + .map(mt => mt -> BinaryCodecWithSchema(ByteChunkBinaryCodec, Schema.chunk[Byte])): _* + ) ) } @@ -351,8 +345,8 @@ object HttpContentCodec { ListMap( MediaType.allMediaTypes .filter(_.binary) - .map(mt => mt -> BinaryCodecWithSchema(ByteBinaryCodec, Schema[Byte])): _*, - ), + .map(mt => mt -> BinaryCodecWithSchema(ByteBinaryCodec, Schema[Byte])): _* + ) ) } }