![JAR search and dependency download from the Maven repository](/logo.png)
com.gitee.cn9750wang.webtools.error.P21Exception Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of web-tools Show documentation
Show all versions of web-tools Show documentation
web tools for spring-boot web project
The newest version!
/*
* Copyright 2021 wwy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gitee.cn9750wang.webtools.error;
import com.gitee.cn9750wang.webtools.error.defines.ServerErr;
import com.gitee.cn9750wang.webtools.vo.result.error.ErrorMessage;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
* 异常类
* @author wwy
*/
public class P21Exception extends RuntimeException{
private List errors = null;
public P21Exception(){
super();
}
public P21Exception(Throwable cause) {
super(cause);
addToList(ServerErr.SERVER_ERR.getMessage().setMsg(cause.getMessage()));
}
public P21Exception(String message) {
super(message);
addToList(ServerErr.SERVER_ERR.getMessage().setMsg(message));
}
/**
* 添加一条错误信息到错误列表中
* @param code 错误代码
* @param msg 错误信息
* @return 异常对象
*/
public P21Exception error(ErrorCode code,String msg){
return this.error(new ErrorMessage(code,msg));
}
/**
* 添加一条错误信息到错误列表中
* @param message 错误信息对象
* @return 异常对象
*/
public P21Exception error(ErrorMessage message){
if(message == null){
return this;
}
addToList(message);
return this;
}
private void addToList(ErrorMessage message){
if(errors == null){
errors = Collections.singletonList(message);
}else if(errors.size() == 1){
var newList = new ArrayList();
newList.add(errors.get(0));
newList.add(message);
errors = newList;
}else{
errors.add(message);
}
}
/**
* 获取所有错误信息
* @return 错误信息列表,永远也不会返回null
*/
public Collection getErrors() {
if(errors == null){
return Collections.emptyList();
}
return errors;
}
/**
* 将错误列表的容量修剪为错误列表的当前大小
*/
public void trimErrors(){
if(errors instanceof ArrayList){
((ArrayList) errors).trimToSize();
}
}
@Override
public String getMessage() {
if(errors == null){
return super.getMessage();
}else if(errors.size() == 1){
return errors.get(0).getMsg();
}
var sb = new StringBuilder();
final int len = errors.size();
for(int i = 0; i < len; i++){
sb.append(i).append(". ").append(errors.get(i).getMsg()).append('\n');
}
return sb.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy