create error code

This commit is contained in:
chuntaojun 2020-07-27 13:03:45 +08:00
parent e90ac7e137
commit 018ce1441f
5 changed files with 119 additions and 54 deletions

View File

@ -24,6 +24,7 @@ package com.alibaba.nacos.api.common;
* <li> Global and common code starts with 10001. * <li> Global and common code starts with 10001.
* <li> Naming module code starts with 20001. * <li> Naming module code starts with 20001.
* <li> Config module code starts with 30001. * <li> Config module code starts with 30001.
* <li> Core module code starts with 40001.
* *
* @author nkorange * @author nkorange
* @since 1.2.0 * @since 1.2.0

View File

@ -0,0 +1,62 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* 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.alibaba.nacos.core.exception;
/**
* Core module code starts with 40001.
*
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
*/
public enum ErrorCode {
UnKnowError(40001),
RocksDBWriteError(40100),
RocksDBReadError(40101),
RocksDBDeleteError(40102),
RocksDBSnapshotSaveError(40103),
RocksDBSnapshotLoadError(40104),
RocksDBResetError(40105),
RocksDBCreateError(40106),
// disk error
IOMakeDirError(40201),
// consistency protocol
ProtoSubmitError(40301),
ProtoReadError(40302),
;
private final int code;
ErrorCode(int code) {
this.code = code;
}
public int getCode() {
return code;
}
}

View File

@ -17,10 +17,10 @@
package com.alibaba.nacos.core.storage; package com.alibaba.nacos.core.storage;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.exception.runtime.NacosRuntimeException; import com.alibaba.nacos.api.exception.runtime.NacosRuntimeException;
import com.alibaba.nacos.common.utils.JacksonUtils; import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.common.utils.StringUtils; import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.core.exception.ErrorCode;
import com.alibaba.nacos.core.exception.RocksStorageException; import com.alibaba.nacos.core.exception.RocksStorageException;
import com.alibaba.nacos.core.utils.DiskUtils; import com.alibaba.nacos.core.utils.DiskUtils;
import org.rocksdb.BackupEngine; import org.rocksdb.BackupEngine;
@ -90,7 +90,7 @@ public final class RocksStorage {
try { try {
DiskUtils.forceMkdir(baseDir); DiskUtils.forceMkdir(baseDir);
} catch (IOException e) { } catch (IOException e) {
throw new NacosRuntimeException(NacosException.SERVER_ERROR, e); throw new NacosRuntimeException(ErrorCode.IOMakeDirError.getCode(), e);
} }
createRocksDB(baseDir, group, writeOptions, readOptions, storage); createRocksDB(baseDir, group, writeOptions, readOptions, storage);
return storage; return storage;
@ -102,7 +102,7 @@ public final class RocksStorage {
createRocksDB(dbPath, group, writeOptions, readOptions, this); createRocksDB(dbPath, group, writeOptions, readOptions, this);
} catch (RocksDBException ex) { } catch (RocksDBException ex) {
Status status = ex.getStatus(); Status status = ex.getStatus();
throw createRocksStorageException(status); throw createRocksStorageException(ErrorCode.RocksDBResetError, status);
} }
} }
@ -126,7 +126,7 @@ public final class RocksStorage {
storage.db = db; storage.db = db;
storage.defaultHandle = columnFamilyHandles.get(0); storage.defaultHandle = columnFamilyHandles.get(0);
} catch (RocksDBException e) { } catch (RocksDBException e) {
throw new NacosRuntimeException(NacosException.SERVER_ERROR, e); throw new NacosRuntimeException(ErrorCode.RocksDBCreateError.getCode(), e);
} }
} }
@ -135,7 +135,7 @@ public final class RocksStorage {
this.db.put(defaultHandle, writeOptions, key, value); this.db.put(defaultHandle, writeOptions, key, value);
} catch (RocksDBException e) { } catch (RocksDBException e) {
Status status = e.getStatus(); Status status = e.getStatus();
throw createRocksStorageException(status); throw createRocksStorageException(ErrorCode.RocksDBWriteError, status);
} }
} }
@ -150,7 +150,7 @@ public final class RocksStorage {
db.write(writeOptions, batch); db.write(writeOptions, batch);
} catch (RocksDBException e) { } catch (RocksDBException e) {
Status status = e.getStatus(); Status status = e.getStatus();
throw createRocksStorageException(status); throw createRocksStorageException(ErrorCode.RocksDBWriteError, status);
} }
} }
@ -159,7 +159,7 @@ public final class RocksStorage {
return db.get(defaultHandle, readOptions, key); return db.get(defaultHandle, readOptions, key);
} catch (RocksDBException e) { } catch (RocksDBException e) {
Status status = e.getStatus(); Status status = e.getStatus();
throw createRocksStorageException(status); throw createRocksStorageException(ErrorCode.RocksDBReadError, status);
} }
} }
@ -168,7 +168,7 @@ public final class RocksStorage {
return db.multiGet(readOptions, key); return db.multiGet(readOptions, key);
} catch (RocksDBException e) { } catch (RocksDBException e) {
Status status = e.getStatus(); Status status = e.getStatus();
throw createRocksStorageException(status); throw createRocksStorageException(ErrorCode.RocksDBReadError, status);
} }
} }
@ -177,7 +177,7 @@ public final class RocksStorage {
db.delete(defaultHandle, writeOptions, key); db.delete(defaultHandle, writeOptions, key);
} catch (RocksDBException e) { } catch (RocksDBException e) {
Status status = e.getStatus(); Status status = e.getStatus();
throw createRocksStorageException(status); throw createRocksStorageException(ErrorCode.RocksDBDeleteError, status);
} }
} }
@ -188,7 +188,7 @@ public final class RocksStorage {
} }
} catch (RocksDBException e) { } catch (RocksDBException e) {
Status status = e.getStatus(); Status status = e.getStatus();
throw createRocksStorageException(status); throw createRocksStorageException(ErrorCode.RocksDBDeleteError, status);
} }
} }
@ -208,12 +208,13 @@ public final class RocksStorage {
return null; return null;
} catch (RocksDBException e) { } catch (RocksDBException e) {
Status status = e.getStatus(); Status status = e.getStatus();
return createRocksStorageException(status); return createRocksStorageException(ErrorCode.RocksDBSnapshotSaveError, status);
} catch (Throwable throwable) { } catch (Throwable throwable) {
return throwable; return throwable;
} }
}); if (ex != null) { });
throw new RocksStorageException(NacosException.SERVER_ERROR, ex); if (ex != null) {
throw new RocksStorageException(ErrorCode.UnKnowError.getCode(), ex);
} }
} }
@ -230,14 +231,12 @@ public final class RocksStorage {
final BackupEngine backupEngine = BackupEngine.open(RocksStorage.this.options.getEnv(), backupOpt); final BackupEngine backupEngine = BackupEngine.open(RocksStorage.this.options.getEnv(), backupOpt);
final RestoreOptions options = new RestoreOptions(true); final RestoreOptions options = new RestoreOptions(true);
final DBOptions dbOptions = RocksStorage.this.options; final DBOptions dbOptions = RocksStorage.this.options;
backupEngine backupEngine.restoreDbFromBackup(info.getBackupId(), dbPath, dbOptions.walDir(), options);
.restoreDbFromBackup(info.getBackupId(), dbPath, dbOptions.walDir(),
options);
} catch (RocksDBException ex) { } catch (RocksDBException ex) {
Status status = ex.getStatus(); Status status = ex.getStatus();
throw createRocksStorageException(status); throw createRocksStorageException(ErrorCode.RocksDBSnapshotLoadError, status);
} catch (Throwable ex) { } catch (Throwable ex) {
throw new RocksStorageException(NacosException.SERVER_ERROR, ex); throw new RocksStorageException(ErrorCode.UnKnowError.getCode(), ex);
} }
} }
@ -252,9 +251,9 @@ public final class RocksStorage {
this.readOptions.close(); this.readOptions.close();
} }
private static RocksStorageException createRocksStorageException(Status status) { private static RocksStorageException createRocksStorageException(ErrorCode code, Status status) {
RocksStorageException exception = new RocksStorageException(); RocksStorageException exception = new RocksStorageException();
exception.setErrCode(status.getCode().getValue()); exception.setErrCode(code.getCode());
exception.setErrMsg(String.format("RocksDB error msg : code=%s, subCode=%s, state=%s", status.getCode(), exception.setErrMsg(String.format("RocksDB error msg : code=%s, subCode=%s, state=%s", status.getCode(),
status.getSubCode(), status.getState())); status.getSubCode(), status.getState()));
return exception; return exception;

View File

@ -32,6 +32,7 @@ import com.alibaba.nacos.consistency.entity.Log;
import com.alibaba.nacos.consistency.entity.Response; import com.alibaba.nacos.consistency.entity.Response;
import com.alibaba.nacos.consistency.snapshot.SnapshotOperation; import com.alibaba.nacos.consistency.snapshot.SnapshotOperation;
import com.alibaba.nacos.core.distributed.raft.RaftConfig; import com.alibaba.nacos.core.distributed.raft.RaftConfig;
import com.alibaba.nacos.core.exception.ErrorCode;
import com.alibaba.nacos.core.exception.RocksStorageException; import com.alibaba.nacos.core.exception.RocksStorageException;
import com.alibaba.nacos.core.storage.RocksStorage; import com.alibaba.nacos.core.storage.RocksStorage;
import com.alibaba.nacos.naming.consistency.Datum; import com.alibaba.nacos.naming.consistency.Datum;
@ -109,7 +110,7 @@ public class PersistentServiceProcessor extends LogProcessor4CP implements Persi
byte[] data = rocksStorage.get(ByteUtils.toBytes(key)); byte[] data = rocksStorage.get(ByteUtils.toBytes(key));
return serializer.deserialize(data); return serializer.deserialize(data);
} catch (RocksStorageException ex) { } catch (RocksStorageException ex) {
throw new NacosRuntimeException(NacosException.SERVER_ERROR, ex.getErrMsg()); throw new NacosRuntimeException(ex.getErrCode(), ex.getErrMsg());
} }
}); });
init(); init();
@ -229,7 +230,7 @@ public class PersistentServiceProcessor extends LogProcessor4CP implements Persi
try { try {
protocol.submit(log); protocol.submit(log);
} catch (Exception e) { } catch (Exception e) {
throw new NacosException(NacosException.SERVER_ERROR, e.getMessage()); throw new NacosException(ErrorCode.ProtoSubmitError.getCode(), e.getMessage());
} }
} }
@ -242,7 +243,7 @@ public class PersistentServiceProcessor extends LogProcessor4CP implements Persi
try { try {
protocol.submit(log); protocol.submit(log);
} catch (Exception e) { } catch (Exception e) {
throw new NacosException(NacosException.SERVER_ERROR, e.getMessage()); throw new NacosException(ErrorCode.ProtoSubmitError.getCode(), e.getMessage());
} }
} }
@ -257,9 +258,9 @@ public class PersistentServiceProcessor extends LogProcessor4CP implements Persi
if (resp.getSuccess()) { if (resp.getSuccess()) {
return serializer.deserialize(resp.getData().toByteArray(), Datum.class); return serializer.deserialize(resp.getData().toByteArray(), Datum.class);
} }
throw new NacosException(NacosException.SERVER_ERROR, resp.getErrMsg()); throw new NacosException(ErrorCode.ProtoReadError.getCode(), resp.getErrMsg());
} catch (Exception e) { } catch (Exception e) {
throw new NacosException(NacosException.SERVER_ERROR, e.getMessage()); throw new NacosException(ErrorCode.ProtoReadError.getCode(), e.getMessage());
} }
} }

View File

@ -18,6 +18,8 @@
package com.alibaba.nacos.naming.utils; package com.alibaba.nacos.naming.utils;
/** /**
* Naming module code starts with 20001.
*
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a> * @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
*/ */
public final class Constants { public final class Constants {