feat: SimApiError 增加顶层函数,支持无前缀 error(400);精简 simapi.cj 为包锚点

- error/errorWhen/errorWhenTrue/errorWhenFalse/errorWhenNull 提升为 simapi.helpers 顶层函数,
  import simapi.helpers.* 后可直接调用(对齐 C# using static SimApi.Helpers.SimApiError)
- SimApiError 类保留为兼容门面(旧写法仍可用)
- simapi.cj 精简为仅 package 声明:实测 cjpm 要求 src 根目录必须有 .cj 锚点,
  否则不扫描子目录导致整包编译为空;SimApi 类/version 常量无任何引用,已移除
This commit is contained in:
2026-08-17 09:06:17 +08:00
parent 67f7c16a3f
commit 2b8d2c47be
2 changed files with 53 additions and 27 deletions
+48 -3
View File
@@ -1,12 +1,60 @@
/* /*
* Copyright (c) 2025 SimcuTeam. All rights reserved. * Copyright (c) 2025 SimcuTeam. All rights reserved.
* 移植自 C# 项目 SimApiE:\simcu\simapi-net),遵循 MIT 许可证。 * 移植自 C# 项目 SimApiE:\simcu\simapi-net),遵循 MIT 许可证。
*
* 错误抛出:提供「顶层函数」+「SimApiError 类」两种写法(对齐 C# using static SimApi.Helpers.SimApiError)。
* - 顶层函数:import simapi.helpers.* 后可直接 error(400) / errorWhen(...),无需前缀
* - SimApiError.error(...):旧写法,保留兼容
*/ */
package simapi.helpers package simapi.helpers
import simapi.exceptions.* 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。 * 错误抛出辅助类:所有业务错误统一通过这里抛出 SimApiException。
* 对应 C# 的 SimApi.Helpers.SimApiError。 * 对应 C# 的 SimApi.Helpers.SimApiError。
@@ -51,9 +99,6 @@ public class SimApiError {
/** /**
* 给定的可选值为 None 时抛错。 * 给定的可选值为 None 时抛错。
* @param condition 检测的可选值。
* @param code 错误代码,默认 404。
* @param message 错误描述。
*/ */
public static func errorWhenNull(condition: ?Any, code!: Int64 = 404, message!: String = ""): Unit { public static func errorWhenNull(condition: ?Any, code!: Int64 = 404, message!: String = ""): Unit {
match (condition) { match (condition) {
+4 -23
View File
@@ -1,29 +1,10 @@
/* /*
* Copyright (c) 2025 SimcuTeam. All rights reserved. * Copyright (c) 2025 SimcuTeam. All rights reserved.
* 移植自 C# 项目 SimApiE:\simcu\simapi-net),遵循 MIT 许可证。 * 移植自 C# 项目 SimApiE:\simcu\simapi-net),遵循 MIT 许可证。
*
* 说明:本文件是 simapi 根包的入口锚点。cjpm 要求 src 根目录至少有一个 .cj 文件,
* 否则不会扫描 src 子目录(helpers/extensions/...),整个包将编译为空。
* 实际功能全部在子包中(simapi.helpers / simapi.extensions / ...)。
*/ */
package simapi 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# 项目 SimApiE:\simcu\simapi-net)。
*/
public class SimApi {
private init() {}
/**
* 包版本号(编译期从 simapi-cj/cjpm.toml 读取)。
*/
@ReadTomlVersion[path: "../simapi-cj/cjpm.toml"]
public static let version: String = ""
}