Skip to content

Commit

Permalink
refactor(SCALE): extract SCALE en/decoding exceptions as custom Runti…
Browse files Browse the repository at this point in the history
…meExceptions

also, move the existing ScaleEncodingException into `utils`
  • Loading branch information
David-Petrov committed Nov 24, 2023
1 parent a43548c commit c033f32
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.limechain.network.protocol.warp.dto.WarpSyncRequest;
import com.limechain.network.protocol.warp.dto.WarpSyncResponse;
import com.limechain.network.protocol.warp.encoding.WarpSyncResponseDecoder;
import com.limechain.network.protocol.warp.exception.ScaleEncodingException;
import com.limechain.utils.scale.exceptions.ScaleEncodingException;
import com.limechain.network.protocol.warp.scale.writer.WarpSyncRequestWriter;
import io.emeraldpay.polkaj.scale.ScaleCodecWriter;
import io.libp2p.core.Stream;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.limechain.network.protocol.warp.dto;

import com.limechain.network.protocol.warp.exception.ScaleEncodingException;
import com.limechain.utils.scale.exceptions.ScaleEncodingException;
import com.limechain.network.protocol.warp.scale.reader.BlockBodyReader;
import com.limechain.network.protocol.warp.scale.writer.BlockBodyWriter;
import com.limechain.utils.HashUtils;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.limechain.network.protocol.warp.dto;

import com.limechain.network.protocol.blockannounce.scale.BlockHeaderScaleWriter;
import com.limechain.network.protocol.warp.exception.ScaleEncodingException;
import com.limechain.utils.scale.exceptions.ScaleEncodingException;
import com.limechain.utils.HashUtils;
import io.emeraldpay.polkaj.scale.ScaleCodecWriter;
import io.emeraldpay.polkaj.types.Hash256;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.limechain.network.protocol.warp.dto;

import com.limechain.network.protocol.warp.exception.ScaleEncodingException;
import com.limechain.utils.scale.exceptions.ScaleEncodingException;
import com.limechain.utils.HashUtils;
import io.emeraldpay.polkaj.scale.ScaleCodecWriter;
import lombok.Data;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.limechain.runtime.hostapi;

import com.limechain.network.protocol.warp.exception.ScaleEncodingException;
import com.limechain.utils.scale.exceptions.ScaleEncodingException;
import com.limechain.rpc.server.AppBean;
import com.limechain.runtime.hostapi.dto.Key;
import com.limechain.runtime.hostapi.dto.RuntimePointerSize;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.limechain.runtime.hostapi;

import com.limechain.network.protocol.warp.exception.ScaleEncodingException;
import com.limechain.utils.scale.exceptions.ScaleEncodingException;
import com.limechain.runtime.hostapi.dto.RuntimePointerSize;
import com.limechain.storage.DBConstants;
import com.limechain.storage.DeleteByPrefixResult;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.limechain.storage;

import com.limechain.network.protocol.warp.exception.ScaleEncodingException;
import com.limechain.utils.scale.exceptions.ScaleEncodingException;
import io.emeraldpay.polkaj.scale.ScaleCodecWriter;

import java.io.ByteArrayOutputStream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.limechain.network.protocol.blockannounce.scale.BlockHeaderScaleWriter;
import com.limechain.network.protocol.warp.dto.BlockHeader;
import com.limechain.network.protocol.warp.exception.ScaleEncodingException;
import com.limechain.utils.scale.exceptions.ScaleEncodingException;
import com.limechain.network.protocol.warp.scale.reader.BlockHeaderReader;
import com.limechain.storage.DBConstants;
import io.emeraldpay.polkaj.scale.ScaleCodecReader;
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/limechain/utils/scale/ScaleUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.limechain.utils.scale;

import com.limechain.utils.scale.exceptions.ScaleDecodingException;
import com.limechain.utils.scale.writers.PairWriter;
import io.emeraldpay.polkaj.scale.ScaleCodecReader;
import io.emeraldpay.polkaj.scale.ScaleCodecWriter;
Expand All @@ -15,7 +16,8 @@
import java.util.List;
import java.util.function.Function;

// TODO: This class is currently just a helper utility class,
// TODO:
// This is currently a helper utility class
// planned to grow into a unified scale encode/decode util class with whatever methods are useful
// WIP
// Currently trying out different approaches to spare some of the boilerplate around SCALE en/decoding
Expand All @@ -28,7 +30,7 @@ public <T> T decode(byte[] encodedData, ScaleReader<T> reader) {
try {
return new ScaleCodecReader(encodedData).read(reader);
} catch (RuntimeException e) {
throw new RuntimeException("Error while SCALE decoding.", e); // TODO: is this a code smell?
throw new ScaleDecodingException("Error while SCALE decoding.", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.limechain.utils.scale.exceptions;

public class ScaleDecodingException extends RuntimeException {
public ScaleDecodingException(Throwable cause) {
super(cause);
}

public ScaleDecodingException(String message) {
super(message);
}

public ScaleDecodingException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.limechain.network.protocol.warp.exception;
package com.limechain.utils.scale.exceptions;

public class ScaleEncodingException extends RuntimeException{
public ScaleEncodingException(Throwable cause) {
Expand Down

0 comments on commit c033f32

Please sign in to comment.