This repository has been archived by the owner on Dec 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JSON-RPC error/exception resolvers that support POJOs (#362)
This adds Jackson-based (de)serialization for JSON-RPC servers and clients that will serialize/deserialize Throwable instances that have custom fields correctly.
- Loading branch information
Showing
29 changed files
with
864 additions
and
58 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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 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,18 @@ | ||
plugins { | ||
id "java" | ||
} | ||
|
||
group 'com.amazonaws.blox' | ||
version '0.1-SNAPSHOT' | ||
|
||
sourceCompatibility = 1.8 | ||
|
||
dependencies { | ||
compile "com.github.briandilley.jsonrpc4j:jsonrpc4j: " | ||
compile "org.projectlombok:lombok" | ||
|
||
testCompile "org.slf4j:slf4j-simple" | ||
testCompile "junit:junit:4.12" | ||
testCompile "org.assertj:assertj-core:3.8 " | ||
testCompileOnly "org.projectlombok:lombok" | ||
} |
62 changes: 62 additions & 0 deletions
62
json-rpc-core/src/main/java/com/amazonaws/blox/jsonrpc/PojoErrorResolver.java
This file contains 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,62 @@ | ||
/* | ||
* Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
* not use this file except in compliance with the License. A copy of the | ||
* License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0/ | ||
* | ||
* or in the "LICENSE" file accompanying this file. This file 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.amazonaws.blox.jsonrpc; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.googlecode.jsonrpc4j.ErrorResolver; | ||
import java.lang.reflect.Method; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
public class PojoErrorResolver implements ErrorResolver { | ||
// Since we encode the type name, just use one error code for all errors that were encoded by this | ||
// resolver. The number is arbitrary, and just chosen to be high enough to not accidentally | ||
// conflict with another error code. | ||
public static final int ERROR_CODE = 9001; | ||
|
||
private final ObjectMapper mapper; | ||
|
||
public PojoErrorResolver(final ObjectMapper mapper) { | ||
this.mapper = mapper.copy().addMixIn(Throwable.class, ThrowableSerializationMixin.class); | ||
} | ||
|
||
@Override | ||
public JsonError resolveError( | ||
final Throwable t, final Method method, final List<JsonNode> arguments) { | ||
|
||
final boolean isModeledException = | ||
Arrays.stream(method.getExceptionTypes()).anyMatch(aClass -> aClass.isInstance(t)); | ||
|
||
if (isModeledException) { | ||
// We have to explicitly serialize the exception type here using the mapper, rather than | ||
// relying on the mapper serializing the entire JsonError object. This is needed because | ||
// Jackson looks up the mixins based on the type of the *variable* not the object instance. | ||
// Since the type of the data field on JsonError is just Object, it then doesn't correctly | ||
// apply the annotations from ThrowableSerializationMixin. | ||
final JsonNode node = mapper.valueToTree(t); | ||
return new JsonError(ERROR_CODE, t.getMessage(), node); | ||
} | ||
|
||
log.warn( | ||
"Exception type {} is not modeled in signature of {}, returning generic error", | ||
t.getClass(), | ||
method.getName()); | ||
|
||
return new JsonError(JsonError.INTERNAL_ERROR.code, t.getMessage(), null); | ||
} | ||
} |
Oops, something went wrong.