refactor(exception): establish new exception system, and move the legacy into deprecated package

This commit is contained in:
2026-04-10 18:41:13 +08:00
parent 4876d621b2
commit 7b963df991
19 changed files with 213 additions and 22 deletions

View File

@@ -3,7 +3,7 @@ package work.slhaf.partner.framework.agent;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import work.slhaf.partner.framework.agent.config.ConfigCenter;
import work.slhaf.partner.framework.agent.exception.AgentLaunchFailedException;
import work.slhaf.partner.framework.agent.exception.deprecated.AgentLaunchFailedException;
import work.slhaf.partner.framework.agent.factory.AgentRegisterFactory;
import work.slhaf.partner.framework.agent.factory.context.AgentContext;
import work.slhaf.partner.framework.agent.interaction.AgentGatewayRegistration;

View File

@@ -3,7 +3,7 @@ package work.slhaf.partner.framework.agent.config
import com.alibaba.fastjson2.JSON
import com.alibaba.fastjson2.JSONObject
import org.slf4j.LoggerFactory
import work.slhaf.partner.framework.agent.exception.AgentLaunchFailedException
import work.slhaf.partner.framework.agent.exception.deprecated.AgentLaunchFailedException
import work.slhaf.partner.framework.agent.support.DirectoryWatchSupport
import java.io.IOException
import java.lang.reflect.Field

View File

@@ -1,11 +1,53 @@
package work.slhaf.partner.framework.agent.exception;
package work.slhaf.partner.framework.agent.exception
public class AgentRuntimeException extends RuntimeException {
public AgentRuntimeException(String message) {
super("Agent 执行出错 " + message);
}
import work.slhaf.partner.framework.agent.factory.component.abstracts.AbstractAgentModule
public AgentRuntimeException(String message, Throwable cause) {
super("Agent 执行出错 " + message, cause);
open class AgentRuntimeException @JvmOverloads constructor(
message: String,
cause: Throwable? = null
) : AgentException(message, cause)
open class ModuleExecutionException @JvmOverloads constructor(
message: String,
val moduleType: Class<out AbstractAgentModule>,
val moduleName: String,
cause: Throwable? = null
) : AgentRuntimeException(message, cause) {
override fun toReport(): ExceptionReport = super.toReport().also {
it.extra["moduleType"] = moduleType
it.extra["moduleName"] = moduleName
}
}
open class InteractionException @JvmOverloads constructor(
message: String,
cause: Throwable? = null
) : AgentRuntimeException(message, cause)
open class GatewayException @JvmOverloads constructor(
message: String,
val gatewayName: String,
cause: Throwable? = null
) : InteractionException(message, cause) {
override fun toReport(): ExceptionReport = super.toReport().also {
it.extra["gatewayName"] = gatewayName
}
}
open class ModelInvokeException(
message: String,
val providerName: String,
val modelKey: String,
val baseUrl: String,
val model: String,
val override: Map<String, String> = emptyMap(),
cause: Throwable? = null
) : AgentRuntimeException(message, cause) {
override fun toReport(): ExceptionReport = super.toReport().also {
it.extra["providerName"] = providerName
it.extra["modelKey"] = modelKey
it.extra["baseUrl"] = baseUrl
it.extra["model"] = model
it.extra["override"] = override
}
}

View File

@@ -0,0 +1,25 @@
package work.slhaf.partner.framework.agent.exception
open class AgentStartupException @JvmOverloads constructor(
message: String,
val relatedComponent: String,
cause: Throwable? = null
) : AgentException(message, cause) {
override fun toReport(): ExceptionReport {
val report = super.toReport()
report.extra["relatedComponent"] = relatedComponent
return report
}
}
open class FactoryExecutionException @JvmOverloads constructor(
message: String,
val factoryName: String,
cause: Throwable? = null
) : AgentStartupException(message, "agent-register-factory", cause) {
override fun toReport(): ExceptionReport {
val report = super.toReport()
report.extra["factoryName"] = factoryName
return report
}
}

View File

@@ -1,5 +1,6 @@
package work.slhaf.partner.framework.agent.exception;
package work.slhaf.partner.framework.agent.exception.deprecated;
@Deprecated
public class AgentLaunchFailedException extends RuntimeException {
public AgentLaunchFailedException(String message, Throwable cause) {
super("Agent 启动失败 " + message, cause);

View File

@@ -1,5 +1,6 @@
package work.slhaf.partner.framework.agent.exception;
package work.slhaf.partner.framework.agent.exception.deprecated;
@Deprecated
public class AgentRunningFailedException extends AgentRuntimeException {
public AgentRunningFailedException(String message) {
super(message);

View File

@@ -0,0 +1,12 @@
package work.slhaf.partner.framework.agent.exception.deprecated;
@Deprecated
public class AgentRuntimeException extends RuntimeException {
public AgentRuntimeException(String message) {
super("Agent 执行出错 " + message);
}
public AgentRuntimeException(String message, Throwable cause) {
super("Agent 执行出错 " + message, cause);
}
}

View File

@@ -0,0 +1,110 @@
package work.slhaf.partner.framework.agent.exception
import com.alibaba.fastjson2.JSONObject
import org.slf4j.LoggerFactory
abstract class AgentException @JvmOverloads constructor(
message: String,
cause: Throwable? = null
) : RuntimeException(message, cause) {
open fun toReport(): ExceptionReport {
return ExceptionReport(
this::class.java.simpleName,
message ?: "",
cause
)
}
}
data class ExceptionReport @JvmOverloads constructor(
val type: String,
val message: String,
val cause: Throwable? = null,
val extra: MutableMap<String, Any?> = linkedMapOf()
) {
override fun toString(): String {
val causeType = cause?.javaClass?.simpleName ?: ""
val causeMessage = cause?.message ?: ""
return """type: $type,
|message: $message,
|causeType: $causeType,
|cause: $causeMessage,
|extra: ${JSONObject.toJSONString(extra)}
""".trimMargin()
}
fun toDetailedString(): String {
return buildString {
appendLine(toString())
val stackTrace = cause?.stackTraceToString()
if (!stackTrace.isNullOrBlank()) {
appendLine("stackTrace:")
appendLine(stackTrace)
}
}
}
}
object ExceptionReporterHandler {
private val registry = mutableMapOf<String, ExceptionReporter>()
private val log = LoggerFactory.getLogger(this::class.java)
fun register(reporter: ExceptionReporter) {
val previous = registry.putIfAbsent(reporter.reporterName(), reporter)
checkAgentStartup(previous == null || previous === reporter) {
AgentStartupException(
"Exception reporter already registered: ${reporter.reporterName()}",
"exception-reporter-handler"
)
}
}
fun report(exception: AgentException, vararg reporters: String) {
LoggerExceptionReporter.report(exception)
for (reporterName in reporters) {
val reporter = registry[reporterName]
if (reporter != null) {
reporter.report(exception)
} else {
log.warn("Exception reporter $reporterName not registered")
}
}
}
}
interface ExceptionReporter {
fun reporterName(): String
fun report(exception: AgentException)
fun register() {
ExceptionReporterHandler.register(this)
}
}
object LoggerExceptionReporter : ExceptionReporter {
private val log = LoggerFactory.getLogger(this::class.java)
override fun reporterName(): String = "logger-reporter"
override fun report(exception: AgentException) {
val exceptionReport = exception.toReport().toDetailedString()
log.error("exception occurred: $exceptionReport")
}
}
inline fun checkAgentStartup(
condition: Boolean,
exception: () -> AgentStartupException
) {
if (!condition) throw exception()
}

View File

@@ -1,6 +1,6 @@
package work.slhaf.partner.framework.agent.factory.capability.exception;
import work.slhaf.partner.framework.agent.exception.AgentLaunchFailedException;
import work.slhaf.partner.framework.agent.exception.deprecated.AgentLaunchFailedException;
public class CapabilityCheckFailedException extends AgentLaunchFailedException {
public CapabilityCheckFailedException(String message) {

View File

@@ -1,6 +1,6 @@
package work.slhaf.partner.framework.agent.factory.capability.exception;
import work.slhaf.partner.framework.agent.exception.AgentLaunchFailedException;
import work.slhaf.partner.framework.agent.exception.deprecated.AgentLaunchFailedException;
public class CapabilityFactoryExecuteFailedException extends AgentLaunchFailedException {
public CapabilityFactoryExecuteFailedException(String message) {

View File

@@ -1,6 +1,6 @@
package work.slhaf.partner.framework.agent.factory.component.exception;
import work.slhaf.partner.framework.agent.exception.AgentRuntimeException;
import work.slhaf.partner.framework.agent.exception.deprecated.AgentRuntimeException;
public class ProxiedModuleRunningException extends AgentRuntimeException {
public ProxiedModuleRunningException(String message) {

View File

@@ -1,6 +1,6 @@
package work.slhaf.partner.framework.agent.factory.config.exception;
import work.slhaf.partner.framework.agent.exception.AgentLaunchFailedException;
import work.slhaf.partner.framework.agent.exception.deprecated.AgentLaunchFailedException;
public class ConfigFactoryInitFailedException extends AgentLaunchFailedException {
public ConfigFactoryInitFailedException(String message, Throwable cause) {

View File

@@ -1,6 +1,6 @@
package work.slhaf.partner.framework.agent.factory.config.exception;
import work.slhaf.partner.framework.agent.exception.AgentRuntimeException;
import work.slhaf.partner.framework.agent.exception.deprecated.AgentRuntimeException;
public class ConfigFactoryRuntimeException extends AgentRuntimeException {
public ConfigFactoryRuntimeException(String message, Throwable cause) {

View File

@@ -3,7 +3,7 @@ package work.slhaf.partner.framework.agent.factory.context
import com.alibaba.fastjson2.JSONArray
import com.alibaba.fastjson2.JSONObject
import org.slf4j.LoggerFactory
import work.slhaf.partner.framework.agent.exception.AgentRunningFailedException
import work.slhaf.partner.framework.agent.exception.deprecated.AgentRunningFailedException
import work.slhaf.partner.framework.agent.factory.capability.annotation.CapabilityCore
import work.slhaf.partner.framework.agent.factory.component.abstracts.AbstractAgentModule
import work.slhaf.partner.framework.agent.factory.component.annotation.AgentComponent