You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It is recommended to add a custom resolver configuration for the data dictionary and error codes.
The original configuration method is as follows:
{
"errorCodeDictionaries": [
{
"title": "title",
"enumClassName": "com.power.common.enums.HttpCodeEnum", // Error code enumeration class
"codeField": "code", // Field name for the error code's code
"descField": "message" // Field name corresponding to the description information of the error code
}
]
}
A new custom resolver configuration parameter valuesResolverClass is suggested, as shown below:
{
"errorCodeDictionaries": [
{
"title": "title",
"enumClassName": "com.power.common.notenums.EException", // Error code class (not an Enum)
"valuesResolverClass": "com.power.common.notenums.EExceptionValuesResolver", // Error code resolution class
}
]
}
Additionally, it is hoped that smart-doc will provide an interface SmartValuesResolver, as follows:
public interface SmartValuesResolver {
<T extends EnumDictionary> Collection<T> resolve();
}
The functionality of the newly added valuesResolverClass parameter's corresponding class EExceptionValuesResolver implementing the SmartValuesResolver interface is as follows:
public class EExceptionValuesResolver implements SmartValuesResolver {
@Override
public <T extends EnumDictionary> Collection<T> resolve() {
List<EnumDictionary> list = new ArrayList<>();
// Technical processing
return list;
}
}
Then, in the com.power.doc.utils.DocUtil#errorCodeDictToList method, add a judgment and handling for the valuesResolverClass parameter. The original key code location is as follows:
At the beginning, add an extended branch and modify as follows:
Class<?> valuesResolverClass = null;
if (StringUtil.isNotEmpty(dictionary.getValuesResolverClass())) {
valuesResolverClass = classLoader.loadClass(dictionary.getValuesResolverClass());
}
if (null != valuesResolverClass && SmartValuesResolver.class.isAssignableFrom(valuesResolverClass)) {
List<ApiErrorCode> enumDictionaryList = valuesResolverClass.getConstructor().newInstance().resolve();
errorCodeList.addAll(enumDictionaryList);
}
else if (clzz.isInterface()) {
// Original code
} else {
// Original code
}
Basic Example (PR Use Case)
If the proposal involves a new or changed API, include a basic code example. Omit this section if it's not applicable.
Motivation (Purpose of this PR)
Company A has unified coding standards, and all exceptions (including error codes) inherit from an abstract base class (parent class) EException (not an interface, not an enum).
@Getter
public class EException {
private final int code; // Unique within the inheritance system, throw an exception if constraints are violated upon initialization
private final String name; // Unique within the inheritance system, throw an exception if constraints are violated upon initialization
private String description;
private String solution;
// Some technical processing, this class cannot be instantiated with new, allowing it to be inherited, only through the following of method to create
protected static EException of(String name, int code, String description, String solution) {
// Technical processing
}
public static EException valueOf(int code) {
// Technical processing
}
public static EException valueOf(String name) {
// Technical processing
}
public static Set<EException> values() {
// Technical processing, this method returns all defined exceptions (including parent and child classes (as long as the ancestor is this class, its defined exceptions will be returned))
}
}
Basic public exception definition (inheriting from EException)
@NoArgsConstructor(access = AccessLevel.PROTECTED) // Can continue to be inherited
public class BaseEException extend EException {
public static final EException BAD_REQUEST = of("BAD_REQUEST", 400000, "Request syntax error: {0}", "Syntax format: {1}");
public static final EException UNAUTHORIZED = of("UNAUTHORIZED", 401000, "The request requires user authentication", "Please log in first");
public static final EException SYSTEM_ERROR = of("SYSTEM_ERROR", 500000, "System exception:{0}", "Please try again later or contact the administrator");
// And so on
}
Custom exception definition for microservices (inheriting from BaseEException)
@NoArgsConstructor(access = AccessLevel.PRIVATE) // Cannot continue to be inherited
public class BizEException extend BaseEException {
public static final EException PARAM_NAME_EXIST = of("PARAM_NAME_EXIST", 400100, "Parameter name already exists!", "Please use another name or contact the administrator");
// And so on
}
Through subclasses, all defined static exceptions in the inheritance relationship can be referenced, and all exceptions within a microservice are gathered together through BizEException. Moreover, this technology also achieves the aggregation of all exceptions through the values method for global maintenance. However, the traditional use of enum to implement the interface method results in each enum being independent and not interoperable, preventing inheritance, resulting in scattered definitions of exceptions within the same application and hindering global maintenance.
The text was updated successfully, but these errors were encountered:
Summary (Description of this PR)
It is recommended to add a custom resolver configuration for the data dictionary and error codes.
The original configuration method is as follows:
A new custom resolver configuration parameter
valuesResolverClass
is suggested, as shown below:Additionally, it is hoped that smart-doc will provide an interface
SmartValuesResolver
, as follows:The functionality of the newly added
valuesResolverClass
parameter's corresponding classEExceptionValuesResolver
implementing theSmartValuesResolver
interface is as follows:Then, in the
com.power.doc.utils.DocUtil#errorCodeDictToList
method, add a judgment and handling for thevaluesResolverClass
parameter. The original key code location is as follows:At the beginning, add an extended branch and modify as follows:
Basic Example (PR Use Case)
If the proposal involves a new or changed API, include a basic code example. Omit this section if it's not applicable.
Motivation (Purpose of this PR)
Company A has unified coding standards, and all exceptions (including error codes) inherit from an abstract base class (parent class) EException (not an interface, not an enum).
Basic public exception definition (inheriting from
EException
)Custom exception definition for microservices (inheriting from
BaseEException
)Through subclasses, all defined
static
exceptions in the inheritance relationship can be referenced, and all exceptions within a microservice are gathered together throughBizEException
. Moreover, this technology also achieves the aggregation of all exceptions through thevalues
method for global maintenance. However, the traditional use ofenum
to implement the interface method results in eachenum
being independent and not interoperable, preventing inheritance, resulting in scattered definitions of exceptions within the same application and hindering global maintenance.The text was updated successfully, but these errors were encountered: