-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat(jsonrpc): jsonrpc set error resolver #6369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
0xbigapple
wants to merge
8
commits into
tronprotocol:release_v4.8.1
Choose a base branch
from
0xbigapple:feature/jsonrpc_setTronErrorResolver
base: release_v4.8.1
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
739467f
JsonRpc:setTronErrorResolver
0xbigapple d9200bc
fix: add resData.length check while throw JsonRpcInternalException
0xbigapple 0d924f6
add data field in TronException
0xbigapple 63bd97c
feat(api): rename JsonRpcErrorResolver, add unit test
0xbigapple 1c27a8b
feat(api): add note and test
0xbigapple d575241
feat(api): add JsonRpcException.java
0xbigapple af80922
feat(api): remove logs and unnecessary test case
0xbigapple 694445b
feat(api): enhance estimateGas error handling to include data field
0xbigapple File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.tron.core.exception.jsonrpc; | ||
|
||
import lombok.Getter; | ||
import org.tron.core.exception.TronException; | ||
|
||
@Getter | ||
public class JsonRpcException extends TronException { | ||
private Object data = null; | ||
|
||
public JsonRpcException() { | ||
super(); | ||
report(); | ||
} | ||
|
||
public JsonRpcException(String message, Object data) { | ||
super(message); | ||
this.data = data; | ||
report(); | ||
} | ||
|
||
public JsonRpcException(String message) { | ||
super(message); | ||
report(); | ||
} | ||
|
||
public JsonRpcException(String message, Throwable cause) { | ||
super(message, cause); | ||
report(); | ||
} | ||
|
||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...eption/JsonRpcInvalidParamsException.java → ...sonrpc/JsonRpcInvalidParamsException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...ption/JsonRpcInvalidRequestException.java → ...onrpc/JsonRpcInvalidRequestException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...ption/JsonRpcMethodNotFoundException.java → ...onrpc/JsonRpcMethodNotFoundException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...eption/JsonRpcTooManyResultException.java → ...sonrpc/JsonRpcTooManyResultException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolver.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package org.tron.core.services.jsonrpc; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.googlecode.jsonrpc4j.ErrorData; | ||
import com.googlecode.jsonrpc4j.ErrorResolver; | ||
import com.googlecode.jsonrpc4j.JsonRpcError; | ||
import com.googlecode.jsonrpc4j.JsonRpcErrors; | ||
import com.googlecode.jsonrpc4j.ReflectionUtil; | ||
import java.lang.reflect.Method; | ||
import java.util.List; | ||
import org.tron.core.exception.jsonrpc.JsonRpcException; | ||
|
||
/** | ||
* {@link ErrorResolver} that uses annotations. | ||
*/ | ||
public enum JsonRpcErrorResolver implements ErrorResolver { | ||
INSTANCE; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public JsonError resolveError( | ||
Throwable thrownException, Method method, List<JsonNode> arguments) { | ||
JsonRpcError resolver = getResolverForException(thrownException, method); | ||
if (notFoundResolver(resolver)) { | ||
return null; | ||
} | ||
|
||
String message = hasErrorMessage(resolver) ? resolver.message() : thrownException.getMessage(); | ||
|
||
// data priority: exception > annotation > default ErrorData | ||
Object data = hasErrorData(resolver) | ||
? resolver.data() | ||
: new ErrorData(resolver.exception().getName(), message); | ||
|
||
// Use data from JsonRpcException if present | ||
if (thrownException instanceof JsonRpcException | ||
&& ((JsonRpcException)thrownException).getData() != null) { | ||
data = ((JsonRpcException)thrownException).getData(); | ||
} | ||
return new JsonError(resolver.code(), message, data); | ||
} | ||
|
||
private JsonRpcError getResolverForException(Throwable thrownException, Method method) { | ||
JsonRpcErrors errors = ReflectionUtil.getAnnotation(method, JsonRpcErrors.class); | ||
if (hasAnnotations(errors)) { | ||
for (JsonRpcError errorDefined : errors.value()) { | ||
if (isExceptionInstanceOfError(thrownException, errorDefined)) { | ||
return errorDefined; | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private boolean notFoundResolver(JsonRpcError resolver) { | ||
return resolver == null; | ||
} | ||
|
||
private boolean hasErrorMessage(JsonRpcError em) { | ||
// noinspection ConstantConditions | ||
return em.message() != null && !em.message().trim().isEmpty(); | ||
} | ||
|
||
private boolean hasErrorData(JsonRpcError em) { | ||
// noinspection ConstantConditions | ||
return em.data() != null && !em.data().trim().isEmpty(); | ||
} | ||
|
||
private boolean hasAnnotations(JsonRpcErrors errors) { | ||
return errors != null; | ||
} | ||
|
||
private boolean isExceptionInstanceOfError(Throwable target, JsonRpcError em) { | ||
return em.exception().isInstance(target); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not define data's type as byte[] ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Object is more general than byte[], I think
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, use the Object type because it is more flexible and can support various kinds of data.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
get it.