Skip to content
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

Standardise the Engine JSON-RPC error codes #8694

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright Consensys Software Inc., 2022
*
* 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 tech.pegasys.teku.ethereum.executionclient.web3j;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public final class JsonRpcErrorCodes {
public static final int PARSE_ERROR = -32700;
public static final int INVALID_REQUEST = -32600;
public static final int METHOD_NOT_FOUND = -32601;
public static final int INVALID_PARAMS = -32602;
public static final int INTERNAL_ERROR = -32603;
public static final int SERVER_ERROR_RANGE_START = -32000;
public static final int SERVER_ERROR_RANGE_END = -32099;

private static final Map<Integer, String> ERROR_MESSAGES;

static {
Map<Integer, String> messages = new HashMap<>();
messages.put(PARSE_ERROR, "Parse error");
messages.put(INVALID_REQUEST, "Invalid request");
messages.put(METHOD_NOT_FOUND, "Method not found");
messages.put(INVALID_PARAMS, "Invalid params");
messages.put(INTERNAL_ERROR, "Internal error");
messages.put(SERVER_ERROR_RANGE_START, "Server error");
ERROR_MESSAGES = Collections.unmodifiableMap(messages);
}

private JsonRpcErrorCodes() {
// Utility class, do not instantiate
}

public static String getErrorMessage(final int errorCode) {
if (isServerError(errorCode)) {
return ERROR_MESSAGES.get(SERVER_ERROR_RANGE_START);
}
return ERROR_MESSAGES.getOrDefault(errorCode, "Unknown error");
}

public static boolean isServerError(final int errorCode) {
return errorCode >= SERVER_ERROR_RANGE_END && errorCode <= SERVER_ERROR_RANGE_START;
}

public static boolean isStandardError(final int errorCode) {
return ERROR_MESSAGES.containsKey(errorCode) || isServerError(errorCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import static tech.pegasys.teku.infrastructure.exceptions.ExceptionUtil.getMessageOrSimpleName;

import java.io.IOException;
import java.net.ConnectException;
import java.time.Duration;
import java.util.Collection;
Expand Down Expand Up @@ -86,21 +85,39 @@ public <T> SafeFuture<Response<T>> doRequest(
(response, exception) -> {
final boolean isCriticalRequest = isCriticalRequest(web3jRequest);
if (exception != null) {
final boolean couldBeAuthError = isAuthenticationException(exception);
handleError(isCriticalRequest, exception, couldBeAuthError);
return Response.withErrorMessage(getMessageOrSimpleName(exception));
return handleException(exception, isCriticalRequest);
} else if (response.hasError()) {
final String errorMessage =
response.getError().getCode() + ": " + response.getError().getMessage();
handleError(isCriticalRequest, new IOException(errorMessage), false);
return Response.withErrorMessage(errorMessage);
return handleJsonRpcError(response.getError(), isCriticalRequest);
} else {
handleSuccess(isCriticalRequest);
return new Response<>(response.getResult());
}
});
}

private <T> Response<T> handleException(final Throwable exception, final boolean isCriticalRequest) {
final boolean couldBeAuthError = isAuthenticationException(exception);
handleError(isCriticalRequest, exception, couldBeAuthError);
return Response.withErrorMessage(getMessageOrSimpleName(exception));
}

private <T> Response<T> handleJsonRpcError(
final org.web3j.protocol.core.Response.Error error, final boolean isCriticalRequest) {
int errorCode = error.getCode();
String errorType = JsonRpcErrorCodes.getErrorMessage(errorCode);
String formattedError = String.format("%s (%d): %s", errorType, errorCode, error.getMessage());

if (isCriticalRequest) {
logError(formattedError);
}

return Response.withErrorMessage(formattedError);
}

private void logError(final String errorMessage) {
eventLog.executionClientRequestFailed(new Exception(errorMessage), false);
}

private boolean isCriticalRequest(final Request<?, ?> request) {
return !nonCriticalMethods.contains(request.getMethod());
}
Expand Down