64 lines
1.9 KiB
Plaintext
64 lines
1.9 KiB
Plaintext
/*
|
||
* Copyright (c) 2025 SimcuTeam. All rights reserved.
|
||
* 移植自 C# 项目 SimApi(E:\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 errorWhenNone(condition: ?Any, code!: Int64 = 404, message!: String = ""): Unit {
|
||
match (condition) {
|
||
case None => error(code: code, message: message)
|
||
case _ => ()
|
||
}
|
||
}
|
||
} |