Files
simapi-cj/src/helpers/SimApiError.cj
T
xrain 92eea3108a refactor: 对齐 simapi-net 的 20 项差异
- SimApiLoginItem._types 默认 [user]
- 新增 SimApiIdOnlyRequest
- errorWhenNone 改名 errorWhenNull
- SimApiAuth.update 保留 TTL(Redis ttl/expire)
- SimApiCache: set 接受 Any + 新增 get<T>/getString
- SimApiHttpClient 签名字段移出 Options
- SimApiUtil: md5/sha1 支持 mode + 新增 paginate
- @SimApiAuth 支持逗号分隔多类型
- Logger: 4 位毫秒 + IsEnabled 恒 true
- 删除 /versions 端点;exceptionHandler 改为抛错
- UseSimApi 中间件顺序对齐 .NET
- README 同步更新
2026-08-16 23:44:52 +08:00

64 lines
1.9 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* Copyright (c) 2025 SimcuTeam. All rights reserved.
* 移植自 C# 项目 SimApiE:\simcu\simapi-net),遵循 MIT 许可证。
*/
package simapi.helpers
import simapi.exceptions.*
/**
* 错误抛出辅助类:所有业务错误统一通过这里抛出 SimApiException。
* 对应 C# 的 SimApi.Helpers.SimApiError。
*/
public class SimApiError {
private init() {}
/**
* 直接抛错。
* @param code 错误代码,默认 500。
* @param message 错误描述,默认空(由 code 自动带取描述)。
*/
public static func error(code!: Int64 = 500, message!: String = ""): Unit {
throw SimApiException(code, message: message)
}
/**
* 条件为 true 时抛错。
* @param condition 检测条件。
* @param code 错误代码,默认 400。
* @param message 错误描述。
*/
public static func errorWhen(condition: Bool, code!: Int64 = 400, message!: String = ""): Unit {
if (condition) {
error(code: code, message: message)
}
}
/**
* 条件为 true 时抛错(别名)。
*/
public static func errorWhenTrue(condition: Bool, code!: Int64 = 400, message!: String = ""): Unit {
errorWhen(condition, code: code, message: message)
}
/**
* 条件为 false 时抛错。
*/
public static func errorWhenFalse(condition: Bool, code!: Int64 = 400, message!: String = ""): Unit {
errorWhen(!condition, code: code, message: message)
}
/**
* 给定的可选值为 None 时抛错。
* @param condition 检测的可选值。
* @param code 错误代码,默认 404。
* @param message 错误描述。
*/
public static func errorWhenNull(condition: ?Any, code!: Int64 = 404, message!: String = ""): Unit {
match (condition) {
case None => error(code: code, message: message)
case _ => ()
}
}
}