@@ -16,18 +16,28 @@
* ProjectName mingli-utils
* ModuleName mingli-utils.main
* CurrentFile HttpStatusException.kt
* LastUpdate 2026-10-11 17:02:06
* LastUpdate 2026-11-23 17:02:00
* UpdateUser MingLiPro
*/
package com.mingliqiye.utils.exception
import kotlin.reflect.KClass
/**
* 表示 HTTP 异常的 基类,继承自 [RuntimeException] 。
* HTTP 状态 异常基类,供所有携带状态码的异常继承 。
*
* @param statusCode HTTP 状态码
* @param message 异常信息,默认为 null
* @param cause 异常原因,默认为 null
* 子类在构造时固定 [statusCode];调用方可以据此统一生成 HTTP 响应,
* 而不必为每一种状态码单独判断异常类型。
*
* 构造函数标注了 `@JvmOverloads`, Java 侧可以按不同参数个数调用。
*
* @property statusCode HTTP 状态码
* @param message 异常描述;为 `null` 时沿用父类行为
* @param throwable 导致该异常的原始异常
* @param enableSuppression 是否启用异常抑制
* @param writableStackTrace 是否生成堆栈信息
* @see MingLiUtilsBaseException
*/
open class HttpStatusException @JvmOverloads constructor (
val statusCode : Int ,
@@ -36,3 +46,395 @@ open class HttpStatusException @JvmOverloads constructor(
enableSuppression : Boolean = false ,
writableStackTrace : Boolean = true ,
) : MingLiUtilsBaseException ( message , throwable , enableSuppression , writableStackTrace )
/**
* HTTP 状态异常类型枚举。
*
* 每个枚举项绑定一个 [HttpStatusException] 子类:可以用 [kClazz] / [clazz] 按类型查找,
* 也可以用 [init] 按消息创建对应的异常实例。
*
* @property kClazz 异常类型的 Kotlin 类
* @property init 使用给定消息创建异常的工厂函数
*/
enum class HttpStatusExceptionEnum (
val kClazz : KClass < out HttpStatusException > ,
val init : ( message : String ? ) -> HttpStatusException ,
) {
/** 502 Bad Gateway,对应 [BadGatewayException]。 */
BAD _GATEWAY _EXCEPTION ( BadGatewayException :: class , :: BadGatewayException ) ,
/** 400 Bad Request,对应 [BadRequestException]。 */
BAD _REQUEST _EXCEPTION ( BadRequestException :: class , :: BadRequestException ) ,
/** 409 Conflict,对应 [ConflictException]。 */
CONFLICT _EXCEPTION ( ConflictException :: class , :: ConflictException ) ,
/** 403 Forbidden,对应 [ForbiddenException]。 */
FORBIDDEN _EXCEPTION ( ForbiddenException :: class , :: ForbiddenException ) ,
/** 302 Found,对应 [FoundException]。 */
FOUND _EXCEPTION ( FoundException :: class , :: FoundException ) ,
/** 504 Gateway Timeout,对应 [GatewayTimeoutException]。 */
GATEWAY _TIMEOUT _EXCEPTION ( GatewayTimeoutException :: class , :: GatewayTimeoutException ) ,
/** 410 Gone,对应 [GoneException]。 */
GONE _EXCEPTION ( GoneException :: class , :: GoneException ) ,
/** 505 HTTP Version Not Supported,对应 [HttpVersionNotSupportedException]。 */
HTTP _VERSION _NOT _SUPPORTED _EXCEPTION ( HttpVersionNotSupportedException :: class , :: HttpVersionNotSupportedException ) ,
/** 500 Internal Server Error,对应 [InternalServerErrorException]。 */
INTERNAL _SERVER _ERROR _EXCEPTION ( InternalServerErrorException :: class , :: InternalServerErrorException ) ,
/** 411 Length Required,对应 [LengthRequiredException]。 */
LENGTH _REQUIRED _EXCEPTION ( LengthRequiredException :: class , :: LengthRequiredException ) ,
/** 405 Method Not Allowed,对应 [MethodNotAllowedException]。 */
METHOD _NOT _ALLOWED _EXCEPTION ( MethodNotAllowedException :: class , :: MethodNotAllowedException ) ,
/** 301 Moved Permanently,对应 [MovedPermanentlyException]。 */
MOVED _PERMANENTLY _EXCEPTION ( MovedPermanentlyException :: class , :: MovedPermanentlyException ) ,
/** 300 Multiple Choices,对应 [MultipleChoicesException]。 */
MULTIPLE _CHOICES _EXCEPTION ( MultipleChoicesException :: class , :: MultipleChoicesException ) ,
/** 406 Not Acceptable,对应 [NotAcceptableException]。 */
NOT _ACCEPTABLE _EXCEPTION ( NotAcceptableException :: class , :: NotAcceptableException ) ,
/** 404 Not Found,对应 [NotFoundException]。 */
NOT _FOUND _EXCEPTION ( NotFoundException :: class , :: NotFoundException ) ,
/** 501 Not Implemented,对应 [NotImplementedException]。 */
NOT _IMPLEMENTED _EXCEPTION ( NotImplementedException :: class , :: NotImplementedException ) ,
/** 304 Not Modified,对应 [NotModifiedException]。 */
NOT _MODIFIED _EXCEPTION ( NotModifiedException :: class , :: NotModifiedException ) ,
/** 402 Payment Required,对应 [PaymentRequiredException]。 */
PAYMENT _REQUIRED _EXCEPTION ( PaymentRequiredException :: class , :: PaymentRequiredException ) ,
/** 412 Precondition Failed,对应 [PreconditionFailedException]。 */
PRECONDITION _FAILED _EXCEPTION ( PreconditionFailedException :: class , :: PreconditionFailedException ) ,
/** 407 Proxy Authentication Required,对应 [ProxyAuthenticationRequiredException]。 */
PROXY _AUTHENTICATION _REQUIRED _EXCEPTION (
ProxyAuthenticationRequiredException :: class ,
:: ProxyAuthenticationRequiredException
) ,
/** 416 Requested Range Not Satisfiable,对应 [RequestedRangeNotSatisfiableException]。 */
REQUESTED _RANGE _NOT _SATISFIABLE _EXCEPTION (
RequestedRangeNotSatisfiableException :: class ,
:: RequestedRangeNotSatisfiableException
) ,
/** 413 Request Entity Too Large,对应 [RequestEntityTooLargeException]。 */
REQUEST _ENTITY _TOO _LARGE _EXCEPTION ( RequestEntityTooLargeException :: class , :: RequestEntityTooLargeException ) ,
/** 408 Request Timeout,对应 [RequestTimeoutException]。 */
REQUEST _TIMEOUT _EXCEPTION ( RequestTimeoutException :: class , :: RequestTimeoutException ) ,
/** 414 Request URI Too Large,对应 [RequestUriTooLargeException]。 */
REQUEST _URI _TOO _LARGE _EXCEPTION ( RequestUriTooLargeException :: class , :: RequestUriTooLargeException ) ,
/** 303 See Other,对应 [SeeOtherException]。 */
SEE _OTHER _EXCEPTION ( SeeOtherException :: class , :: SeeOtherException ) ,
/** 503 Service Unavailable,对应 [ServiceUnavailableException]。 */
SERVICE _UNAVAILABLE _EXCEPTION ( ServiceUnavailableException :: class , :: ServiceUnavailableException ) ,
/** 307 Temporary Redirect,对应 [TemporaryRedirectException]。 */
TEMPORARY _REDIRECT _EXCEPTION ( TemporaryRedirectException :: class , :: TemporaryRedirectException ) ,
/** 401 Unauthorized,对应 [UnauthorizedException]。 */
UNAUTHORIZED _EXCEPTION ( UnauthorizedException :: class , :: UnauthorizedException ) ,
/** 415 Unsupported Media Type,对应 [UnsupportedMediaTypeException]。 */
UNSUPPORTED _MEDIA _TYPE _EXCEPTION ( UnsupportedMediaTypeException :: class , :: UnsupportedMediaTypeException ) ,
/** 305 Use Proxy,对应 [UseProxyException]。 */
USE _PROXY _EXCEPTION ( UseProxyException :: class , :: UseProxyException ) ;
/** [kClazz] 对应的 Java 类,便于反射或按 `Class` 注册。 */
val clazz : Class < out HttpStatusException > = this . kClazz . java
}
/**
* 抛出 HTTP 502 Bad Gateway 异常。
*
* @param message 异常描述,默认为 `"Bad Gateway"`
* @throws BadGatewayException 总是抛出
*/
@Suppress ( " NOTHING_TO_INLINE " )
inline fun httpError502 ( message : String ? = " Bad Gateway " ) : Nothing = throw BadGatewayException ( message )
/**
* 抛出 HTTP 400 Bad Request 异常。
*
* @param message 异常描述,默认为 `"Bad Request"`
* @throws BadRequestException 总是抛出
*/
@Suppress ( " NOTHING_TO_INLINE " )
inline fun httpError400 ( message : String ? = " Bad Request " ) : Nothing = throw BadRequestException ( message )
/**
* 抛出 HTTP 409 Conflict 异常。
*
* @param message 异常描述,默认为 `"Conflict"`
* @throws ConflictException 总是抛出
*/
@Suppress ( " NOTHING_TO_INLINE " )
inline fun httpError409 ( message : String ? = " Conflict " ) : Nothing = throw ConflictException ( message )
/**
* 抛出 HTTP 403 Forbidden 异常。
*
* @param message 异常描述,默认为 `"Forbidden"`
* @throws ForbiddenException 总是抛出
*/
@Suppress ( " NOTHING_TO_INLINE " )
inline fun httpError403 ( message : String ? = " Forbidden " ) : Nothing = throw ForbiddenException ( message )
/**
* 抛出 HTTP 302 Found 异常。
*
* @param message 异常描述,默认为 `"Found"`
* @throws FoundException 总是抛出
*/
@Suppress ( " NOTHING_TO_INLINE " )
inline fun httpError302 ( message : String ? = " Found " ) : Nothing = throw FoundException ( message )
/**
* 抛出 HTTP 504 Gateway Timeout 异常。
*
* @param message 异常描述,默认为 `"Gateway Timeout"`
* @throws GatewayTimeoutException 总是抛出
*/
@Suppress ( " NOTHING_TO_INLINE " )
inline fun httpError504 ( message : String ? = " Gateway Timeout " ) : Nothing = throw GatewayTimeoutException ( message )
/**
* 抛出 HTTP 410 Gone 异常。
*
* @param message 异常描述,默认为 `"Gone"`
* @throws GoneException 总是抛出
*/
@Suppress ( " NOTHING_TO_INLINE " )
inline fun httpError410 ( message : String ? = " Gone " ) : Nothing = throw GoneException ( message )
/**
* 抛出 HTTP 505 HTTP Version Not Supported 异常。
*
* @param message 异常描述,默认为 `"HTTP Version Not Supported"`
* @throws HttpVersionNotSupportedException 总是抛出
*/
@Suppress ( " NOTHING_TO_INLINE " )
inline fun httpError505 ( message : String ? = " HTTP Version Not Supported " ) : Nothing =
throw HttpVersionNotSupportedException ( message )
/**
* 抛出 HTTP 500 Internal Server Error 异常。
*
* @param message 异常描述,默认为 `"Internal Server Error"`
* @throws InternalServerErrorException 总是抛出
*/
@Suppress ( " NOTHING_TO_INLINE " )
inline fun httpError500 ( message : String ? = " Internal Server Error " ) : Nothing =
throw InternalServerErrorException ( message )
/**
* 抛出 HTTP 411 Length Required 异常。
*
* @param message 异常描述,默认为 `"Length Required"`
* @throws LengthRequiredException 总是抛出
*/
@Suppress ( " NOTHING_TO_INLINE " )
inline fun httpError411 ( message : String ? = " Length Required " ) : Nothing = throw LengthRequiredException ( message )
/**
* 抛出 HTTP 405 Method Not Allowed 异常。
*
* @param message 异常描述,默认为 `"Method Not Allowed"`
* @throws MethodNotAllowedException 总是抛出
*/
@Suppress ( " NOTHING_TO_INLINE " )
inline fun httpError405 ( message : String ? = " Method Not Allowed " ) : Nothing = throw MethodNotAllowedException ( message )
/**
* 抛出 HTTP 301 Moved Permanently 异常。
*
* @param message 异常描述,默认为 `"Moved Permanently"`
* @throws MovedPermanentlyException 总是抛出
*/
@Suppress ( " NOTHING_TO_INLINE " )
inline fun httpError301 ( message : String ? = " Moved Permanently " ) : Nothing = throw MovedPermanentlyException ( message )
/**
* 抛出 HTTP 300 Multiple Choices 异常。
*
* @param message 异常描述,默认为 `"Multiple Choices"`
* @throws MultipleChoicesException 总是抛出
*/
@Suppress ( " NOTHING_TO_INLINE " )
inline fun httpError300 ( message : String ? = " Multiple Choices " ) : Nothing = throw MultipleChoicesException ( message )
/**
* 抛出 HTTP 406 Not Acceptable 异常。
*
* @param message 异常描述,默认为 `"Not Acceptable"`
* @throws NotAcceptableException 总是抛出
*/
@Suppress ( " NOTHING_TO_INLINE " )
inline fun httpError406 ( message : String ? = " Not Acceptable " ) : Nothing = throw NotAcceptableException ( message )
/**
* 抛出 HTTP 404 Not Found 异常。
*
* @param message 异常描述,默认为 `"Not Found"`
* @throws NotFoundException 总是抛出
*/
@Suppress ( " NOTHING_TO_INLINE " )
inline fun httpError404 ( message : String ? = " Not Found " ) : Nothing = throw NotFoundException ( message )
/**
* 抛出 HTTP 501 Not Implemented 异常。
*
* @param message 异常描述,默认为 `"Not Implemented"`
* @throws NotImplementedException 总是抛出
*/
@Suppress ( " NOTHING_TO_INLINE " )
inline fun httpError501 ( message : String ? = " Not Implemented " ) : Nothing = throw NotImplementedException ( message )
/**
* 抛出 HTTP 304 Not Modified 异常。
*
* @param message 异常描述,默认为 `"Not Modified"`
* @throws NotModifiedException 总是抛出
*/
@Suppress ( " NOTHING_TO_INLINE " )
inline fun httpError304 ( message : String ? = " Not Modified " ) : Nothing = throw NotModifiedException ( message )
/**
* 抛出 HTTP 402 Payment Required 异常。
*
* @param message 异常描述,默认为 `"Payment Required"`
* @throws PaymentRequiredException 总是抛出
*/
@Suppress ( " NOTHING_TO_INLINE " )
inline fun httpError402 ( message : String ? = " Payment Required " ) : Nothing = throw PaymentRequiredException ( message )
/**
* 抛出 HTTP 412 Precondition Failed 异常。
*
* @param message 异常描述,默认为 `"Precondition Failed"`
* @throws PreconditionFailedException 总是抛出
*/
@Suppress ( " NOTHING_TO_INLINE " )
inline fun httpError412 ( message : String ? = " Precondition Failed " ) : Nothing = throw PreconditionFailedException ( message )
/**
* 抛出 HTTP 407 Proxy Authentication Required 异常。
*
* @param message 异常描述,默认为 `"Proxy Authentication Required"`
* @throws ProxyAuthenticationRequiredException 总是抛出
*/
@Suppress ( " NOTHING_TO_INLINE " )
inline fun httpError407 ( message : String ? = " Proxy Authentication Required " ) : Nothing =
throw ProxyAuthenticationRequiredException ( message )
/**
* 抛出 HTTP 416 Requested Range Not Satisfiable 异常。
*
* @param message 异常描述,默认为 `"Requested Range Not Satisfiable"`
* @throws RequestedRangeNotSatisfiableException 总是抛出
*/
@Suppress ( " NOTHING_TO_INLINE " )
inline fun httpError416 ( message : String ? = " Requested Range Not Satisfiable " ) : Nothing =
throw RequestedRangeNotSatisfiableException ( message )
/**
* 抛出 HTTP 413 Request Entity Too Large 异常。
*
* @param message 异常描述,默认为 `"Request Entity Too Large"`
* @throws RequestEntityTooLargeException 总是抛出
*/
@Suppress ( " NOTHING_TO_INLINE " )
inline fun httpError413 ( message : String ? = " Request Entity Too Large " ) : Nothing =
throw RequestEntityTooLargeException ( message )
/**
* 抛出 HTTP 408 Request Timeout 异常。
*
* @param message 异常描述,默认为 `"Request Timeout"`
* @throws RequestTimeoutException 总是抛出
*/
@Suppress ( " NOTHING_TO_INLINE " )
inline fun httpError408 ( message : String ? = " Request Timeout " ) : Nothing = throw RequestTimeoutException ( message )
/**
* 抛出 HTTP 414 Request URI Too Large 异常。
*
* @param message 异常描述,默认为 `"Request URI Too Large"`
* @throws RequestUriTooLargeException 总是抛出
*/
@Suppress ( " NOTHING_TO_INLINE " )
inline fun httpError414 ( message : String ? = " Request URI Too Large " ) : Nothing =
throw RequestUriTooLargeException ( message )
/**
* 抛出 HTTP 303 See Other 异常。
*
* @param message 异常描述,默认为 `"See Other"`
* @throws SeeOtherException 总是抛出
*/
@Suppress ( " NOTHING_TO_INLINE " )
inline fun httpError303 ( message : String ? = " See Other " ) : Nothing = throw SeeOtherException ( message )
/**
* 抛出 HTTP 503 Service Unavailable 异常。
*
* @param message 异常描述,默认为 `"Service Unavailable"`
* @throws ServiceUnavailableException 总是抛出
*/
@Suppress ( " NOTHING_TO_INLINE " )
inline fun httpError503 ( message : String ? = " Service Unavailable " ) : Nothing = throw ServiceUnavailableException ( message )
/**
* 抛出 HTTP 307 Temporary Redirect 异常。
*
* @param message 异常描述,默认为 `"Temporary Redirect"`
* @throws TemporaryRedirectException 总是抛出
*/
@Suppress ( " NOTHING_TO_INLINE " )
inline fun httpError307 ( message : String ? = " Temporary Redirect " ) : Nothing = throw TemporaryRedirectException ( message )
/**
* 抛出 HTTP 401 Unauthorized 异常。
*
* @param message 异常描述,默认为 `"Unauthorized"`
* @throws UnauthorizedException 总是抛出
*/
@Suppress ( " NOTHING_TO_INLINE " )
inline fun httpError401 ( message : String ? = " Unauthorized " ) : Nothing = throw UnauthorizedException ( message )
/**
* 抛出 HTTP 415 Unsupported Media Type 异常。
*
* @param message 异常描述,默认为 `"Unsupported Media Type"`
* @throws UnsupportedMediaTypeException 总是抛出
*/
@Suppress ( " NOTHING_TO_INLINE " )
inline fun httpError415 ( message : String ? = " Unsupported Media Type " ) : Nothing =
throw UnsupportedMediaTypeException ( message )
/**
* 抛出 HTTP 305 Use Proxy 异常。
*
* @param message 异常描述,默认为 `"Use Proxy"`
* @throws UseProxyException 总是抛出
*/
@Suppress ( " NOTHING_TO_INLINE " )
inline fun httpError305 ( message : String ? = " Use Proxy " ) : Nothing = throw UseProxyException ( message )