API refactors to avoid generic unhandled states.

This commit is contained in:
CalDescent 2021-07-04 13:38:20 +01:00
parent 60415b9222
commit a742fecf9c
2 changed files with 86 additions and 86 deletions

View File

@ -275,11 +275,17 @@ public class ArbitraryResource {
LOGGER.info("Whole file digest: {}", dataFile.base58Digest());
int chunkCount = dataFile.split(DataFile.CHUNK_SIZE);
if (chunkCount > 0) {
if (chunkCount == 0) {
LOGGER.error("No chunks created");
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_DATA);
}
LOGGER.info(String.format("Successfully split into %d chunk%s", chunkCount, (chunkCount == 1 ? "" : "s")));
String base58Digest = dataFile.base58Digest();
if (base58Digest != null) {
if (base58Digest == null) {
LOGGER.error("Unable to calculate digest");
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_DATA);
}
AccountData accountData = repository.getAccountRepository().getAccount(creatorAddress);
if (accountData == null || accountData.getPublicKey() == null) {
@ -312,14 +318,6 @@ public class ArbitraryResource {
byte[] bytes = ArbitraryTransactionTransformer.toBytes(transactionData);
return Base58.encode(bytes);
}
// Something went wrong, so delete our copies of the data and chunks
dataFile.deleteAll();
}
return "false";
} catch (DataException e) {
LOGGER.error("Repository issue when uploading data", e);
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.REPOSITORY_ISSUE, e);

View File

@ -95,9 +95,16 @@ public class WebsiteResource {
}
DataFile dataFile = this.hostWebsite(path);
if (dataFile != null) {
if (dataFile == null) {
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_DATA);
}
String base58Digest = dataFile.base58Digest();
if (base58Digest != null) {
LOGGER.error("Unable to calculate digest");
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_DATA);
}
try (final Repository repository = RepositoryManager.getRepository()) {
AccountData accountData = repository.getAccountRepository().getAccount(creatorAddress);
@ -137,11 +144,6 @@ public class WebsiteResource {
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.REPOSITORY_ISSUE, e);
}
}
// Something went wrong, so delete our copies of the data and chunks
dataFile.deleteAll();
}
return "false";
}
@POST
@Path("/preview")