Skip to content

Commit

Permalink
Support custom reason phrase for Status (#3009) (#3116)
Browse files Browse the repository at this point in the history
  • Loading branch information
987Nabil authored Sep 9, 2024
1 parent 2def6dd commit 8731a44
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,13 @@ private[netty] object Conversions {
}

def statusToNetty(status: Status): HttpResponseStatus =
HttpResponseStatus.valueOf(status.code)
HttpResponseStatus.valueOf(status.code, status.reasonPhrase)

def statusFromNetty(status: HttpResponseStatus): Status =
Status.fromInt(status.code)
Status.fromInt(status.code) match {
case Status.Custom(code, _) => Status.Custom(code, status.reasonPhrase)
case status => status
}

def schemeToNetty(scheme: Scheme): Option[HttpScheme] = scheme match {
case Scheme.HTTP => Option(HttpScheme.HTTP)
Expand Down
9 changes: 6 additions & 3 deletions zio-http/shared/src/main/scala/zio/http/Status.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ sealed trait Status extends Product with Serializable { self =>
*/
val code: Int

val reasonPhrase: String =
self.productPrefix.flatMap { c => if (c.isUpper) s" $c" else s"$c" }.drop(1)

lazy val text: String = code.toString

/**
Expand All @@ -53,7 +56,7 @@ object Status {
sealed trait Informational extends Status // 100 – 199
sealed trait Success extends Status // 200 – 299
sealed trait Redirection extends Status // 300 – 399
sealed trait Error extends Status // 400 – 499
sealed trait Error extends Status // 400 – 599
sealed trait ClientError extends Error // 400 – 499
sealed trait ServerError extends Error // 500 – 599

Expand Down Expand Up @@ -169,7 +172,7 @@ object Status {

case object NetworkAuthenticationRequired extends ServerError { override val code: Int = 511 }

final case class Custom(override val code: Int) extends Status
final case class Custom(override val code: Int, override val reasonPhrase: String = "") extends Status

def fromString(code: String): Option[Status] =
Try(code.toInt).toOption.map(fromInt)
Expand Down Expand Up @@ -232,7 +235,7 @@ object Status {
case 507 => Status.InsufficientStorage
case 510 => Status.NotExtended
case 511 => Status.NetworkAuthenticationRequired
case _ => Status.Custom(code)
case _ => Status.Custom(code, "")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,8 @@ private[codec] trait StatusCodecs {
val NotExtended: StatusCodec[Unit] = status(Status.NotExtended)
val NetworkAuthenticationRequired: StatusCodec[Unit] =
status(Status.NetworkAuthenticationRequired)
def CustomStatus(code: Int): StatusCodec[Unit] = status(Status.Custom(code))
def CustomStatus(code: Int): StatusCodec[Unit] = status(Status.Custom(code, ""))

def CustomStatus(code: Int, reasonPhrase: String): StatusCodec[Unit] =
status(Status.Custom(code, reasonPhrase))
}

0 comments on commit 8731a44

Please sign in to comment.