diff --git a/src/helpers/SimApiError.cj b/src/helpers/SimApiError.cj index 9f563b4..c272f23 100644 --- a/src/helpers/SimApiError.cj +++ b/src/helpers/SimApiError.cj @@ -1,12 +1,60 @@ /* * Copyright (c) 2025 SimcuTeam. All rights reserved. * 移植自 C# 项目 SimApi(E:\simcu\simapi-net),遵循 MIT 许可证。 + * + * 错误抛出:提供「顶层函数」+「SimApiError 类」两种写法(对齐 C# using static SimApi.Helpers.SimApiError)。 + * - 顶层函数:import simapi.helpers.* 后可直接 error(400) / errorWhen(...),无需前缀 + * - SimApiError.error(...):旧写法,保留兼容 */ package simapi.helpers import simapi.exceptions.* +// ===== 顶层函数(推荐用法:直接 error(400),对齐 C# using static) ===== + +/** + * 直接抛错。 + * @param code 错误代码,默认 500。 + * @param message 错误描述,默认空(由 code 自动带取描述)。 + */ +public func error(code!: Int64 = 500, message!: String = ""): Unit { + SimApiError.error(code: code, message: message) +} + +/** + * 条件为 true 时抛错。 + * @param condition 检测条件。 + * @param code 错误代码,默认 400。 + * @param message 错误描述。 + */ +public func errorWhen(condition: Bool, code!: Int64 = 400, message!: String = ""): Unit { + SimApiError.errorWhen(condition, code: code, message: message) +} + +/** + * 条件为 true 时抛错(别名)。 + */ +public func errorWhenTrue(condition: Bool, code!: Int64 = 400, message!: String = ""): Unit { + SimApiError.errorWhenTrue(condition, code: code, message: message) +} + +/** + * 条件为 false 时抛错。 + */ +public func errorWhenFalse(condition: Bool, code!: Int64 = 400, message!: String = ""): Unit { + SimApiError.errorWhenFalse(condition, code: code, message: message) +} + +/** + * 给定的可选值为 None 时抛错。 + */ +public func errorWhenNull(condition: ?Any, code!: Int64 = 404, message!: String = ""): Unit { + SimApiError.errorWhenNull(condition, code: code, message: message) +} + +// ===== 兼容门面(旧写法 SimApiError.error(...) 仍可用,内部为真实实现) ===== + /** * 错误抛出辅助类:所有业务错误统一通过这里抛出 SimApiException。 * 对应 C# 的 SimApi.Helpers.SimApiError。 @@ -51,9 +99,6 @@ public class SimApiError { /** * 给定的可选值为 None 时抛错。 - * @param condition 检测的可选值。 - * @param code 错误代码,默认 404。 - * @param message 错误描述。 */ public static func errorWhenNull(condition: ?Any, code!: Int64 = 404, message!: String = ""): Unit { match (condition) { @@ -61,4 +106,4 @@ public class SimApiError { case _ => () } } -} \ No newline at end of file +} diff --git a/src/simapi.cj b/src/simapi.cj index bf929b7..632c119 100644 --- a/src/simapi.cj +++ b/src/simapi.cj @@ -1,29 +1,10 @@ /* * Copyright (c) 2025 SimcuTeam. All rights reserved. * 移植自 C# 项目 SimApi(E:\simcu\simapi-net),遵循 MIT 许可证。 + * + * 说明:本文件是 simapi 根包的入口锚点。cjpm 要求 src 根目录至少有一个 .cj 文件, + * 否则不会扫描 src 子目录(helpers/extensions/...),整个包将编译为空。 + * 实际功能全部在子包中(simapi.helpers / simapi.extensions / ...)。 */ package simapi - -import simapi.communications.* -import simapi.configurations.* -import simapi.exceptions.* -import simapi.extensions.* -import simapi.helpers.* -import simapi.interfaces.* -import simapi.macros.* -import simapi.middlewares.* - -/** - * SimApi 仓颉版:ASP.NET Core 风格 API 基础框架。 - * 移植自 C# 项目 SimApi(E:\simcu\simapi-net)。 - */ -public class SimApi { - private init() {} - - /** - * 包版本号(编译期从 simapi-cj/cjpm.toml 读取)。 - */ - @ReadTomlVersion[path: "../simapi-cj/cjpm.toml"] - public static let version: String = "" -}