mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 16:53:04 +08:00
refactor(exception): establish new exception system, and move the legacy into deprecated package
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
package work.slhaf.partner.common.vector.exception;
|
package work.slhaf.partner.common.vector.exception;
|
||||||
|
|
||||||
import work.slhaf.partner.framework.agent.exception.AgentRuntimeException;
|
import work.slhaf.partner.framework.agent.exception.deprecated.AgentRuntimeException;
|
||||||
|
|
||||||
public class VectorClientExecuteException extends AgentRuntimeException {
|
public class VectorClientExecuteException extends AgentRuntimeException {
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package work.slhaf.partner.common.vector.exception;
|
package work.slhaf.partner.common.vector.exception;
|
||||||
|
|
||||||
import work.slhaf.partner.framework.agent.exception.AgentRuntimeException;
|
import work.slhaf.partner.framework.agent.exception.deprecated.AgentRuntimeException;
|
||||||
|
|
||||||
public class VectorClientLoadFailedException extends AgentRuntimeException {
|
public class VectorClientLoadFailedException extends AgentRuntimeException {
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package work.slhaf.partner.core.action.exception;
|
package work.slhaf.partner.core.action.exception;
|
||||||
|
|
||||||
import work.slhaf.partner.framework.agent.exception.AgentLaunchFailedException;
|
import work.slhaf.partner.framework.agent.exception.deprecated.AgentLaunchFailedException;
|
||||||
|
|
||||||
public class ActionInitFailedException extends AgentLaunchFailedException {
|
public class ActionInitFailedException extends AgentLaunchFailedException {
|
||||||
public ActionInitFailedException(String message, Throwable cause) {
|
public ActionInitFailedException(String message, Throwable cause) {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package work.slhaf.partner.core.action.exception;
|
package work.slhaf.partner.core.action.exception;
|
||||||
|
|
||||||
import work.slhaf.partner.framework.agent.exception.AgentRuntimeException;
|
import work.slhaf.partner.framework.agent.exception.deprecated.AgentRuntimeException;
|
||||||
|
|
||||||
public class ActionSerializeFailedException extends AgentRuntimeException {
|
public class ActionSerializeFailedException extends AgentRuntimeException {
|
||||||
public ActionSerializeFailedException(String message) {
|
public ActionSerializeFailedException(String message) {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package work.slhaf.partner.core.action.exception;
|
package work.slhaf.partner.core.action.exception;
|
||||||
|
|
||||||
import work.slhaf.partner.framework.agent.exception.AgentRuntimeException;
|
import work.slhaf.partner.framework.agent.exception.deprecated.AgentRuntimeException;
|
||||||
|
|
||||||
public class MetaActionNotFoundException extends AgentRuntimeException {
|
public class MetaActionNotFoundException extends AgentRuntimeException {
|
||||||
public MetaActionNotFoundException(String message) {
|
public MetaActionNotFoundException(String message) {
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package work.slhaf.partner.framework.agent;
|
|||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import work.slhaf.partner.framework.agent.config.ConfigCenter;
|
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.AgentRegisterFactory;
|
||||||
import work.slhaf.partner.framework.agent.factory.context.AgentContext;
|
import work.slhaf.partner.framework.agent.factory.context.AgentContext;
|
||||||
import work.slhaf.partner.framework.agent.interaction.AgentGatewayRegistration;
|
import work.slhaf.partner.framework.agent.interaction.AgentGatewayRegistration;
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package work.slhaf.partner.framework.agent.config
|
|||||||
import com.alibaba.fastjson2.JSON
|
import com.alibaba.fastjson2.JSON
|
||||||
import com.alibaba.fastjson2.JSONObject
|
import com.alibaba.fastjson2.JSONObject
|
||||||
import org.slf4j.LoggerFactory
|
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 work.slhaf.partner.framework.agent.support.DirectoryWatchSupport
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import java.lang.reflect.Field
|
import java.lang.reflect.Field
|
||||||
|
|||||||
@@ -1,11 +1,53 @@
|
|||||||
package work.slhaf.partner.framework.agent.exception;
|
package work.slhaf.partner.framework.agent.exception
|
||||||
|
|
||||||
public class AgentRuntimeException extends RuntimeException {
|
import work.slhaf.partner.framework.agent.factory.component.abstracts.AbstractAgentModule
|
||||||
public AgentRuntimeException(String message) {
|
|
||||||
super("Agent 执行出错 " + message);
|
|
||||||
}
|
|
||||||
|
|
||||||
public AgentRuntimeException(String message, Throwable cause) {
|
open class AgentRuntimeException @JvmOverloads constructor(
|
||||||
super("Agent 执行出错 " + message, cause);
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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 class AgentLaunchFailedException extends RuntimeException {
|
||||||
public AgentLaunchFailedException(String message, Throwable cause) {
|
public AgentLaunchFailedException(String message, Throwable cause) {
|
||||||
super("Agent 启动失败 " + message, cause);
|
super("Agent 启动失败 " + message, cause);
|
||||||
@@ -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 class AgentRunningFailedException extends AgentRuntimeException {
|
||||||
public AgentRunningFailedException(String message) {
|
public AgentRunningFailedException(String message) {
|
||||||
super(message);
|
super(message);
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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()
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package work.slhaf.partner.framework.agent.factory.capability.exception;
|
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 class CapabilityCheckFailedException extends AgentLaunchFailedException {
|
||||||
public CapabilityCheckFailedException(String message) {
|
public CapabilityCheckFailedException(String message) {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package work.slhaf.partner.framework.agent.factory.capability.exception;
|
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 class CapabilityFactoryExecuteFailedException extends AgentLaunchFailedException {
|
||||||
public CapabilityFactoryExecuteFailedException(String message) {
|
public CapabilityFactoryExecuteFailedException(String message) {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package work.slhaf.partner.framework.agent.factory.component.exception;
|
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 class ProxiedModuleRunningException extends AgentRuntimeException {
|
||||||
public ProxiedModuleRunningException(String message) {
|
public ProxiedModuleRunningException(String message) {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package work.slhaf.partner.framework.agent.factory.config.exception;
|
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 class ConfigFactoryInitFailedException extends AgentLaunchFailedException {
|
||||||
public ConfigFactoryInitFailedException(String message, Throwable cause) {
|
public ConfigFactoryInitFailedException(String message, Throwable cause) {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package work.slhaf.partner.framework.agent.factory.config.exception;
|
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 class ConfigFactoryRuntimeException extends AgentRuntimeException {
|
||||||
public ConfigFactoryRuntimeException(String message, Throwable cause) {
|
public ConfigFactoryRuntimeException(String message, Throwable cause) {
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package work.slhaf.partner.framework.agent.factory.context
|
|||||||
import com.alibaba.fastjson2.JSONArray
|
import com.alibaba.fastjson2.JSONArray
|
||||||
import com.alibaba.fastjson2.JSONObject
|
import com.alibaba.fastjson2.JSONObject
|
||||||
import org.slf4j.LoggerFactory
|
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.capability.annotation.CapabilityCore
|
||||||
import work.slhaf.partner.framework.agent.factory.component.abstracts.AbstractAgentModule
|
import work.slhaf.partner.framework.agent.factory.component.abstracts.AbstractAgentModule
|
||||||
import work.slhaf.partner.framework.agent.factory.component.annotation.AgentComponent
|
import work.slhaf.partner.framework.agent.factory.component.annotation.AgentComponent
|
||||||
|
|||||||
Reference in New Issue
Block a user