Skip to content

Instantly share code, notes, and snippets.

@vlio20
Last active June 19, 2023 23:56
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save vlio20/27023c55fd8df349a4487b505ba72c3b to your computer and use it in GitHub Desktop.
Save vlio20/27023c55fd8df349a4487b505ba72c3b to your computer and use it in GitHub Desktop.
Kotlin Http Status Codes enum
package oogaday.commons.enums
enum class StatusCode(val code: Int) {
Continue(100),
SwitchingProtocols(101),
Processing(102),
OK(200),
Created(201),
Accepted(202),
NonAuthoritativeInformation(203),
NoContent(204),
ResetContent(205),
PartialContent(206),
MultiStatus(207),
AlreadyReported(208),
IMUsed(226),
MultipleChoices(300),
MovedPermanently(301),
Found(302),
SeeOther(303),
NotModified(304),
UseProxy(305),
TemporaryRedirect(307),
PermanentRedirect(308),
BadRequest(400),
Unauthorized(401),
PaymentRequired(402),
Forbidden(403),
NotFound(404),
MethodNotAllowed(405),
NotAcceptable(406),
ProxyAuthenticationRequired(407),
RequestTimeout(408),
Conflict(409),
Gone(410),
LengthRequired(411),
PreconditionFailed(412),
PayloadTooLarge(413),
UriTooLong(414),
UnsupportedMediaType(415),
RangeNotSatisfiable(416),
ExpectationFailed(417),
IAmATeapot(418),
MisdirectedRequest(421),
UnprocessableEntity(422),
Locked(423),
FailedDependency(424),
UpgradeRequired(426),
PreconditionRequired(428),
TooManyRequests(429),
RequestHeaderFieldsTooLarge(431),
UnavailableForLegalReasons(451),
InternalServerError(500),
NotImplemented(501),
BadGateway(502),
ServiceUnavailable(503),
GatewayTimeout(504),
HttpVersionNotSupported(505),
VariantAlsoNegotiates(506),
InsufficientStorage(507),
LoopDetected(508),
NotExtended(510),
NetworkAuthenticationRequired(511),
Unknown(0)
}
@jef
Copy link

jef commented Mar 4, 2020

Nice

@sapirgolan
Copy link

@vlio20 this definitely some peace of work :-)

@FabioCZ
Copy link

FabioCZ commented Nov 15, 2021

Might I suggest a small edit? Acronyms with 3 or more letters shouldn't have all the letters capitalized (https://kotlinlang.org/docs/coding-conventions.html#choose-good-names), so, for example, 414 should be UriTooLong or 505 should be HttpVersionNotSupported

@vlio20
Copy link
Author

vlio20 commented Nov 16, 2021

@FabioCZ - updated!

@FabioCZ
Copy link

FabioCZ commented Nov 16, 2021

Thanks, and thanks for this helpful enum!

@vovahost
Copy link

vovahost commented Feb 23, 2022

@vlio20 You can also add the parsing from status code:


  companion object {
    fun from(code: Int): StatusCode {
      return when (code) {
        100 -> Continue
        101 -> SwitchingProtocols
        102 -> Processing

        200 -> OK
        201 -> Created
        202 -> Accepted
        203 -> NonAuthoritativeInformation
        204 -> NoContent
        205 -> ResetContent
        206 -> PartialContent
        207 -> MultiStatus
        208 -> AlreadyReported
        226 -> IMUsed

        300 -> MultipleChoices
        301 -> MovedPermanently
        302 -> Found
        303 -> SeeOther
        304 -> NotModified
        305 -> UseProxy
        307 -> TemporaryRedirect
        308 -> PermanentRedirect

        400 -> BadRequest
        401 -> Unauthorized
        402 -> PaymentRequired
        403 -> Forbidden
        404 -> NotFound
        405 -> MethodNotAllowed
        406 -> NotAcceptable
        407 -> ProxyAuthenticationRequired
        408 -> RequestTimeout
        409 -> Conflict
        410 -> Gone
        411 -> LengthRequired
        412 -> PreconditionFailed
        413 -> PayloadTooLarge
        414 -> UriTooLong
        415 -> UnsupportedMediaType
        416 -> RangeNotSatisfiable
        417 -> ExpectationFailed
        418 -> IAmATeapot
        421 -> MisdirectedRequest
        422 -> UnprocessableEntity
        423 -> Locked
        424 -> FailedDependency
        426 -> UpgradeRequired
        428 -> PreconditionRequired
        429 -> TooManyRequests
        431 -> RequestHeaderFieldsTooLarge
        451 -> UnavailableForLegalReasons

        500 -> InternalServerError
        501 -> NotImplemented
        502 -> BadGateway
        503 -> ServiceUnavailable
        504 -> GatewayTimeout
        505 -> HttpVersionNotSupported
        506 -> VariantAlsoNegotiates
        507 -> InsufficientStorage
        508 -> LoopDetected
        510 -> NotExtended
        511 -> NetworkAuthenticationRequired

        0 -> Unknown
        else -> Unknown
      }
    }
  }

@simgesengun
Copy link

companion object {
fun from(code: Int): StatusCode {

You can also do it as:

companion object {
    fun from(code: Int): StatusCode {
        return StatusCode.values().find {
            it.code == code
        } ?: Unknown
    }
} 

or

companion object {
    fun Int.toStatusCode(): StatusCode {
        return StatusCode.values().find {
            it.code == this
        } ?: Unknown
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment