Files
simapi-cj/src/helpers/SimApiError.cj
T
2026-08-16 12:46:15 +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 errorWhenNone(condition: ?Any, code!: Int64 = 404, message!: String = ""): Unit {
match (condition) {
case None => error(code: code, message: message)
case _ => ()
}
}
}