|
| 1 | +package com.tencent.devops.api.http |
| 2 | + |
| 3 | +/** |
| 4 | + * HTTP 状态码枚举类 |
| 5 | + */ |
| 6 | +enum class HttpStatus( |
| 7 | + val code: Int, |
| 8 | + val reasonPhrase: String |
| 9 | +) { |
| 10 | + CONTINUE(100, "Continue"), |
| 11 | + SWITCHING_PROTOCOLS(101, "Switching Protocols"), |
| 12 | + PROCESSING(102, "Processing"), |
| 13 | + CHECKPOINT(103, "Checkpoint"), |
| 14 | + OK(200, "OK"), |
| 15 | + CREATED(201, "Created"), |
| 16 | + ACCEPTED(202, "Accepted"), |
| 17 | + NON_AUTHORITATIVE_INFORMATION(203, "Non-Authoritative Information"), |
| 18 | + NO_CONTENT(204, "No Content"), |
| 19 | + RESET_CONTENT(205, "Reset Content"), |
| 20 | + PARTIAL_CONTENT(206, "Partial Content"), |
| 21 | + MULTI_STATUS(207, "Multi-Status"), |
| 22 | + ALREADY_REPORTED(208, "Already Reported"), |
| 23 | + IM_USED(226, "IM Used"), // 3xx Redirection |
| 24 | + MULTIPLE_CHOICES(300, "Multiple Choices"), |
| 25 | + MOVED_PERMANENTLY(301, "Moved Permanently"), |
| 26 | + FOUND(302, "Found"), |
| 27 | + MOVED_TEMPORARILY(302, "Moved Temporarily"), |
| 28 | + SEE_OTHER(303, "See Other"), |
| 29 | + NOT_MODIFIED(304, "Not Modified"), |
| 30 | + USE_PROXY(305, "Use Proxy"), |
| 31 | + TEMPORARY_REDIRECT(307, "Temporary Redirect"), |
| 32 | + PERMANENT_REDIRECT(308, "Permanent Redirect"), // --- 4xx Client Error --- |
| 33 | + BAD_REQUEST(400, "Bad Request"), |
| 34 | + UNAUTHORIZED(401, "Unauthorized"), |
| 35 | + PAYMENT_REQUIRED(402, "Payment Required"), |
| 36 | + FORBIDDEN(403, "Forbidden"), |
| 37 | + NOT_FOUND(404, "Not Found"), |
| 38 | + METHOD_NOT_ALLOWED(405, "Method Not Allowed"), |
| 39 | + NOT_ACCEPTABLE(406, "Not Acceptable"), |
| 40 | + PROXY_AUTHENTICATION_REQUIRED(407, "Proxy Authentication Required"), |
| 41 | + REQUEST_TIMEOUT(408, "Request Timeout"), |
| 42 | + CONFLICT(409, "Conflict"), |
| 43 | + GONE(410, "Gone"), |
| 44 | + LENGTH_REQUIRED(411, "Length Required"), |
| 45 | + PRECONDITION_FAILED(412, "Precondition Failed"), |
| 46 | + PAYLOAD_TOO_LARGE(413, "Payload Too Large"), |
| 47 | + REQUEST_ENTITY_TOO_LARGE(413, "Request Entity Too Large"), |
| 48 | + URI_TOO_LONG(414, "URI Too Long"), |
| 49 | + REQUEST_URI_TOO_LONG(414, "Request-URI Too Long"), |
| 50 | + UNSUPPORTED_MEDIA_TYPE(415, "Unsupported Media Type"), |
| 51 | + REQUESTED_RANGE_NOT_SATISFIABLE(416, "Requested range not satisfiable"), |
| 52 | + EXPECTATION_FAILED(417, "Expectation Failed"), |
| 53 | + I_AM_A_TEAPOT(418, "I'm a teapot"), |
| 54 | + INSUFFICIENT_SPACE_ON_RESOURCE(419, "Insufficient Space On Resource"), |
| 55 | + METHOD_FAILURE(420, "Method Failure"), |
| 56 | + DESTINATION_LOCKED(421, "Destination Locked"), |
| 57 | + UNPROCESSABLE_ENTITY(422, "Unprocessable Entity"), |
| 58 | + LOCKED(423, "Locked"), |
| 59 | + FAILED_DEPENDENCY(424, "Failed Dependency"), |
| 60 | + TOO_EARLY(425, "Too Early"), |
| 61 | + UPGRADE_REQUIRED(426, "Upgrade Required"), |
| 62 | + PRECONDITION_REQUIRED(428, "Precondition Required"), |
| 63 | + TOO_MANY_REQUESTS(429, "Too Many Requests"), |
| 64 | + REQUEST_HEADER_FIELDS_TOO_LARGE(431, "Request Header Fields Too Large"), |
| 65 | + UNAVAILABLE_FOR_LEGAL_REASONS(451, "Unavailable For Legal Reasons"), // --- 5xx Server Error --- |
| 66 | + INTERNAL_SERVER_ERROR(500, "Internal Server Error"), |
| 67 | + NOT_IMPLEMENTED(501, "Not Implemented"), |
| 68 | + BAD_GATEWAY(502, "Bad Gateway"), |
| 69 | + SERVICE_UNAVAILABLE(503, "Service Unavailable"), |
| 70 | + GATEWAY_TIMEOUT(504, "Gateway Timeout"), |
| 71 | + HTTP_VERSION_NOT_SUPPORTED(505, "HTTP Versio n not supported"), |
| 72 | + VARIANT_ALSO_NEGOTIATES(506, "Variant Also Negotiates"), |
| 73 | + INSUFFICIENT_STORAGE(507, "Insufficient Storage"), |
| 74 | + LOOP_DETECTED(508, "Loop Detected"), |
| 75 | + BANDWIDTH_LIMIT_EXCEEDED(509, "Bandwidth Limit Exceeded"), |
| 76 | + NOT_EXTENDED(510, "Not Extended"), |
| 77 | + NETWORK_AUTHENTICATION_REQUIRED(511, "Network Authentication Required"); |
| 78 | + |
| 79 | + companion object { |
| 80 | + |
| 81 | + /** |
| 82 | + * 根据[code]查询HttpStatus |
| 83 | + * 当[code]对应的HttpStatus不存在时抛出[IllegalArgumentException]异常 |
| 84 | + */ |
| 85 | + fun ofCode(code: Int): HttpStatus { |
| 86 | + return ofCodeOrNull(code) ?: throw IllegalArgumentException("No matching constant for [$code]") |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * 根据[code]查询HttpStatus |
| 91 | + * 当[code]对应的HttpStatus不存在时返回null |
| 92 | + */ |
| 93 | + fun ofCodeOrNull(code: Int): HttpStatus? { |
| 94 | + for (status in values()) { |
| 95 | + if (status.code == code) { |
| 96 | + return status |
| 97 | + } |
| 98 | + } |
| 99 | + return null |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments