Search in sources :

Example 66 with NulsException

use of io.nuls.core.exception.NulsException in project nuls by nuls-io.

the class MsgLog method error.

public static void error(Throwable throwable) {
    String logContent = isStringBlank(getId()) ? (getLogTrace() + ":") : (getLogTrace() + "[" + getId() + "]" + ":");
    if (!(throwable instanceof NulsException) || !(throwable instanceof NulsRuntimeException)) {
        throwable = new NulsException(ErrorCode.FAILED, throwable);
    }
    LOG.error(logContent, throwable);
}
Also used : NulsException(io.nuls.core.exception.NulsException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

Example 67 with NulsException

use of io.nuls.core.exception.NulsException in project nuls by nuls-io.

the class MsgLog method trace.

/**
 * 提供trace级别基本的日志输出
 *
 * @param msg       需要显示的消息
 * @param throwable 异常信息
 */
public static void trace(String msg, Throwable throwable) {
    String logContent = isStringBlank(getId()) ? (getLogTrace() + ":" + msg) : (getLogTrace() + "[" + getId() + "]" + ":" + msg);
    if (!(throwable instanceof NulsException) || !(throwable instanceof NulsRuntimeException)) {
        throwable = new NulsException(ErrorCode.FAILED, throwable);
    }
    LOG.trace(logContent, throwable);
}
Also used : NulsException(io.nuls.core.exception.NulsException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

Example 68 with NulsException

use of io.nuls.core.exception.NulsException in project nuls by nuls-io.

the class VersionManager method updateFile.

private static void updateFile(String oldVersion, String newVersion, List<NulsVersion> versionList, URL libsUrl) throws NulsException, IOException {
    String jsonStr = null;
    try {
        jsonStr = new String(HttpDownloadUtils.download(VERDION_JSON_URL), NulsContext.DEFAULT_ENCODING);
    } catch (IOException e) {
        Log.error(e);
        throw new NulsException(ErrorCode.FAILED, "Download version json faild!");
    }
    Map<String, Object> map = null;
    try {
        map = JSONUtils.json2map(jsonStr);
    } catch (Exception e) {
        Log.error(e);
        throw new NulsException(ErrorCode.FAILED, "Parse version json faild!");
    }
    String version = (String) map.get("version");
    String sign = (String) map.get("sign");
    // when the json change at the moment,restart;
    if (!newVersion.equals(version)) {
        start();
        return;
    }
    Map<String, String> jarMap = new HashMap<>();
    fillJarMap(libsUrl, jarMap);
    // jarMap is empty means it's development environment
    if (jarMap.isEmpty()) {
        return;
    }
    List<Map<String, Object>> libList = (List<Map<String, Object>>) map.get("libs");
    // check the temp folder exist,and delete
    URL rootUrl = VersionManager.class.getResource("");
    File tempFolder = new File(rootUrl.getPath() + "/temp");
    if (tempFolder.exists()) {
        deleteFile(tempFolder);
    }
    tempFolder.mkdir();
    List<String> newVersionJarList = new ArrayList<>();
    for (Map<String, Object> lib : libList) {
        String file = (String) lib.get("file");
        newVersionJarList.add(file);
        String libSign = (String) lib.get("sign");
        // todo check sign
        if (jarMap.get(file) == null) {
            downloadLib(tempFolder.getPath(), file, sign);
        }
    }
    File bakFolder = new File(rootUrl.getPath() + "/bak");
    if (bakFolder.exists()) {
        deleteFile(bakFolder);
    }
    bakFolder.mkdir();
    List<String> removeList = new ArrayList<>();
    try {
        for (String key : jarMap.keySet()) {
            if (newVersionJarList.contains(key)) {
                continue;
            }
            File jar = new File(jarMap.get(key));
            boolean b = jar.renameTo(new File(bakFolder.getPath() + "/" + key));
            if (!b) {
                throw new NulsException(ErrorCode.FAILED, "move the file fiald:" + key);
            }
            removeList.add(jar.getName());
        }
    } catch (NulsException e) {
        List<String> failedList = new ArrayList<>();
        for (String fileName : removeList) {
            File file = new File(bakFolder.getPath() + "/" + fileName);
            boolean b = file.renameTo(new File(libsUrl.getPath() + "/" + fileName));
            if (!b) {
                failedList.add(bakFolder.getPath() + "/" + fileName);
            }
        }
        failedOpration(failedList);
    }
    File[] files = tempFolder.listFiles();
    if (files == null || files.length == 0) {
        return;
    }
    List<String> moved = new ArrayList<>();
    try {
        for (File file : files) {
            boolean b = file.renameTo(new File(libsUrl.getPath() + "/" + file.getName()));
            if (!b) {
                throw new NulsException(ErrorCode.FAILED, "move the file fiald:" + file.getPath());
            }
            moved.add(file.getName());
        }
    } catch (NulsException e) {
        for (String fileName : moved) {
            File newFile = new File(libsUrl.getPath() + "/" + fileName);
            if (newFile.exists()) {
                newFile.delete();
            }
        }
        File[] bakFiles = bakFolder.listFiles();
        List<String> failedList = new ArrayList<>();
        for (File file : bakFiles) {
            boolean b = file.renameTo(new File(libsUrl.getPath() + "/" + file.getName()));
            if (!b) {
                failedList.add(file.getPath());
            }
        }
        failedOpration(failedList);
    }
// todo 重启动,或者发送通知
}
Also used : NulsException(io.nuls.core.exception.NulsException) URL(java.net.URL) NulsException(io.nuls.core.exception.NulsException)

Example 69 with NulsException

use of io.nuls.core.exception.NulsException in project nuls by nuls-io.

the class EventManager method getInstance.

public static BaseEvent getInstance(byte[] bytes) throws NulsException {
    EventHeader header = new EventHeader();
    header.parse(new NulsByteBuffer(bytes));
    Class<? extends BaseEvent> clazz = EVENT_MAP.get(header.getModuleId() + "_" + header.getEventType());
    if (null == clazz) {
        return null;
    }
    BaseEvent event;
    try {
        event = clazz.newInstance();
    } catch (Exception e) {
        Log.error(e);
        throw new NulsException(ErrorCode.DATA_PARSE_ERROR);
    }
    try {
        event.parse(new NulsByteBuffer(bytes));
    } catch (Exception e) {
        Log.error(e);
        throw e;
    }
    return event;
}
Also used : NulsException(io.nuls.core.exception.NulsException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) NulsException(io.nuls.core.exception.NulsException) NulsByteBuffer(io.nuls.core.utils.io.NulsByteBuffer)

Aggregations

NulsException (io.nuls.core.exception.NulsException)69 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)32 IOException (java.io.IOException)17 ValidateResult (io.nuls.core.validate.ValidateResult)12 Account (io.nuls.account.entity.Account)11 Transaction (io.nuls.core.chain.entity.Transaction)8 BlockRoundData (io.nuls.consensus.entity.block.BlockRoundData)7 Block (io.nuls.core.chain.entity.Block)6 DbSession (io.nuls.db.transactional.annotation.DbSession)6 Coin (io.nuls.ledger.entity.params.Coin)6 CoinTransferData (io.nuls.ledger.entity.params.CoinTransferData)6 Result (io.nuls.core.chain.entity.Result)5 TransactionEvent (io.nuls.ledger.event.TransactionEvent)5 BlockHeader (io.nuls.core.chain.entity.BlockHeader)4 NulsDigestData (io.nuls.core.chain.entity.NulsDigestData)4 Address (io.nuls.account.entity.Address)3 PocMeetingRound (io.nuls.consensus.entity.meeting.PocMeetingRound)3 P2PKHScriptSig (io.nuls.core.script.P2PKHScriptSig)3 NulsByteBuffer (io.nuls.core.utils.io.NulsByteBuffer)3 AccountPo (io.nuls.db.entity.AccountPo)3