下面列出了java.net.HttpURLConnection#HTTP_BAD_GATEWAY 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static Status.Code httpStatusToGrpcCode(int httpStatusCode) {
if (httpStatusCode >= 100 && httpStatusCode < 200) {
// 1xx. These headers should have been ignored.
return Status.Code.INTERNAL;
}
switch (httpStatusCode) {
case HttpURLConnection.HTTP_BAD_REQUEST: // 400
case 431: // Request Header Fields Too Large
// TODO(carl-mastrangelo): this should be added to the http-grpc-status-mapping.md doc.
return Status.Code.INTERNAL;
case HttpURLConnection.HTTP_UNAUTHORIZED: // 401
return Status.Code.UNAUTHENTICATED;
case HttpURLConnection.HTTP_FORBIDDEN: // 403
return Status.Code.PERMISSION_DENIED;
case HttpURLConnection.HTTP_NOT_FOUND: // 404
return Status.Code.UNIMPLEMENTED;
case 429: // Too Many Requests
case HttpURLConnection.HTTP_BAD_GATEWAY: // 502
case HttpURLConnection.HTTP_UNAVAILABLE: // 503
case HttpURLConnection.HTTP_GATEWAY_TIMEOUT: // 504
return Status.Code.UNAVAILABLE;
default:
return Status.Code.UNKNOWN;
}
}
public void testRequestWasSuccessful_unsuccessfulStatusCodes() {
// 204 and 205 are considered unsuccessful in this case, and 206 should never happen. Also
// test with some of the other common status codes (these are ones that our backend has
// been known to return).
final int[] statusCodes = {
HttpURLConnection.HTTP_NO_CONTENT,
HttpURLConnection.HTTP_RESET,
HttpURLConnection.HTTP_PARTIAL,
HttpURLConnection.HTTP_BAD_REQUEST,
HttpURLConnection.HTTP_UNAUTHORIZED,
HttpURLConnection.HTTP_FORBIDDEN,
HttpURLConnection.HTTP_NOT_FOUND,
HttpURLConnection.HTTP_NOT_ACCEPTABLE,
HttpURLConnection.HTTP_INTERNAL_ERROR,
HttpURLConnection.HTTP_BAD_GATEWAY,
HttpURLConnection.HTTP_UNAVAILABLE,
HttpURLConnection.HTTP_GATEWAY_TIMEOUT
};
for (int statusCode : statusCodes) {
assertFalse(defaultSettingsSpiCall.requestWasSuccessful(statusCode));
}
}
private static Status.Code httpStatusToGrpcCode(int httpStatusCode) {
if (httpStatusCode >= 100 && httpStatusCode < 200) {
// 1xx. These headers should have been ignored.
return Status.Code.INTERNAL;
}
switch (httpStatusCode) {
case HttpURLConnection.HTTP_BAD_REQUEST: // 400
case 431: // Request Header Fields Too Large
return Status.Code.INTERNAL;
case HttpURLConnection.HTTP_UNAUTHORIZED: // 401
return Status.Code.UNAUTHENTICATED;
case HttpURLConnection.HTTP_FORBIDDEN: // 403
return Status.Code.PERMISSION_DENIED;
case HttpURLConnection.HTTP_NOT_FOUND: // 404
return Status.Code.UNIMPLEMENTED;
case 429: // Too Many Requests
case HttpURLConnection.HTTP_BAD_GATEWAY: // 502
case HttpURLConnection.HTTP_UNAVAILABLE: // 503
case HttpURLConnection.HTTP_GATEWAY_TIMEOUT: // 504
return Status.Code.UNAVAILABLE;
default:
return Status.Code.UNKNOWN;
}
}
private static Status.Code httpStatusToGrpcCode(int httpStatusCode) {
if (httpStatusCode >= 100 && httpStatusCode < 200) {
// 1xx. These headers should have been ignored.
return Status.Code.INTERNAL;
}
switch (httpStatusCode) {
case HttpURLConnection.HTTP_BAD_REQUEST: // 400
case 431: // Request Header Fields Too Large
// TODO(carl-mastrangelo): this should be added to the http-grpc-status-mapping.md doc.
return Status.Code.INTERNAL;
case HttpURLConnection.HTTP_UNAUTHORIZED: // 401
return Status.Code.UNAUTHENTICATED;
case HttpURLConnection.HTTP_FORBIDDEN: // 403
return Status.Code.PERMISSION_DENIED;
case HttpURLConnection.HTTP_NOT_FOUND: // 404
return Status.Code.UNIMPLEMENTED;
case 429: // Too Many Requests
case HttpURLConnection.HTTP_BAD_GATEWAY: // 502
case HttpURLConnection.HTTP_UNAVAILABLE: // 503
case HttpURLConnection.HTTP_GATEWAY_TIMEOUT: // 504
return Status.Code.UNAVAILABLE;
default:
return Status.Code.UNKNOWN;
}
}
/**
* Returns true for server errors that are throttle-able.
*
* <p>The {@link HttpURLConnection#HTTP_GATEWAY_TIMEOUT} error is included here since it is
* similar to the other unavailable errors in the previously linked doc.
*/
private boolean isThrottleableServerError(int httpStatusCode) {
return httpStatusCode == HTTP_TOO_MANY_REQUESTS
|| httpStatusCode == HttpURLConnection.HTTP_BAD_GATEWAY
|| httpStatusCode == HttpURLConnection.HTTP_UNAVAILABLE
|| httpStatusCode == HttpURLConnection.HTTP_GATEWAY_TIMEOUT;
}
/**
* Translates the specified HTTP status code into a storage exception.
*
* @param statusCode
* The HTTP status code returned by the operation.
* @param statusDescription
* A <code>String</code> that represents the status description.
* @param inner
* An <code>Exception</code> object that represents a reference to the initial exception, if one exists.
*
* @return A <code>StorageException</code> object that represents translated exception.
**/
protected static StorageException translateFromHttpStatus(final int statusCode, final String statusDescription,
final Exception inner) {
String errorCode;
switch (statusCode) {
case HttpURLConnection.HTTP_FORBIDDEN:
errorCode = StorageErrorCode.ACCESS_DENIED.toString();
break;
case HttpURLConnection.HTTP_GONE:
case HttpURLConnection.HTTP_NOT_FOUND:
errorCode = StorageErrorCode.RESOURCE_NOT_FOUND.toString();
break;
case 416:
case HttpURLConnection.HTTP_BAD_REQUEST:
// 416: RequestedRangeNotSatisfiable - No corresponding enum in HttpURLConnection
errorCode = StorageErrorCode.BAD_REQUEST.toString();
break;
case HttpURLConnection.HTTP_PRECON_FAILED:
case HttpURLConnection.HTTP_NOT_MODIFIED:
errorCode = StorageErrorCode.CONDITION_FAILED.toString();
break;
case HttpURLConnection.HTTP_CONFLICT:
errorCode = StorageErrorCode.RESOURCE_ALREADY_EXISTS.toString();
break;
case HttpURLConnection.HTTP_UNAVAILABLE:
errorCode = StorageErrorCode.SERVER_BUSY.toString();
break;
case HttpURLConnection.HTTP_GATEWAY_TIMEOUT:
errorCode = StorageErrorCode.SERVICE_TIMEOUT.toString();
break;
case HttpURLConnection.HTTP_INTERNAL_ERROR:
errorCode = StorageErrorCode.SERVICE_INTERNAL_ERROR.toString();
break;
case HttpURLConnection.HTTP_NOT_IMPLEMENTED:
errorCode = StorageErrorCode.NOT_IMPLEMENTED.toString();
break;
case HttpURLConnection.HTTP_BAD_GATEWAY:
errorCode = StorageErrorCode.BAD_GATEWAY.toString();
break;
case HttpURLConnection.HTTP_VERSION:
errorCode = StorageErrorCode.HTTP_VERSION_NOT_SUPPORTED.toString();
break;
default:
errorCode = null;
}
if (errorCode == null) {
return null;
} else {
return new StorageException(errorCode, statusDescription, statusCode, null, inner);
}
}
/**
* Map from HTTP status code to reason description.
* Sentry HTTP breadcrumbs expect a text description of the HTTP status-code.
* This function implements a look-up table with a default value for unknown status-codes.
* @param statusCode an integer HTTP status code, expected to be in the range [200,505].
* @return a non-empty string in all cases.
*/
private static String httpReason(int statusCode) {
switch (statusCode) {
// 2xx
case HttpURLConnection.HTTP_OK: return "OK";
case HttpURLConnection.HTTP_CREATED: return "Created";
case HttpURLConnection.HTTP_ACCEPTED: return "Accepted";
case HttpURLConnection.HTTP_NOT_AUTHORITATIVE: return "Non-Authoritative Information";
case HttpURLConnection.HTTP_NO_CONTENT: return "No Content";
case HttpURLConnection.HTTP_RESET: return "Reset Content";
case HttpURLConnection.HTTP_PARTIAL: return "Partial Content";
// 3xx
case HttpURLConnection.HTTP_MULT_CHOICE: return "Multiple Choices";
case HttpURLConnection.HTTP_MOVED_PERM: return "Moved Permanently";
case HttpURLConnection.HTTP_MOVED_TEMP: return "Temporary Redirect";
case HttpURLConnection.HTTP_SEE_OTHER: return "See Other";
case HttpURLConnection.HTTP_NOT_MODIFIED: return "Not Modified";
case HttpURLConnection.HTTP_USE_PROXY: return "Use Proxy";
// 4xx
case HttpURLConnection.HTTP_BAD_REQUEST: return "Bad Request";
case HttpURLConnection.HTTP_UNAUTHORIZED: return "Unauthorized";
case HttpURLConnection.HTTP_PAYMENT_REQUIRED: return "Payment Required";
case HttpURLConnection.HTTP_FORBIDDEN: return "Forbidden";
case HttpURLConnection.HTTP_NOT_FOUND: return "Not Found";
case HttpURLConnection.HTTP_BAD_METHOD: return "Method Not Allowed";
case HttpURLConnection.HTTP_NOT_ACCEPTABLE: return "Not Acceptable";
case HttpURLConnection.HTTP_PROXY_AUTH: return "Proxy Authentication Required";
case HttpURLConnection.HTTP_CLIENT_TIMEOUT: return "Request Time-Out";
case HttpURLConnection.HTTP_CONFLICT: return "Conflict";
case HttpURLConnection.HTTP_GONE: return "Gone";
case HttpURLConnection.HTTP_LENGTH_REQUIRED: return "Length Required";
case HttpURLConnection.HTTP_PRECON_FAILED: return "Precondition Failed";
case HttpURLConnection.HTTP_ENTITY_TOO_LARGE: return "Request Entity Too Large";
case HttpURLConnection.HTTP_REQ_TOO_LONG: return "Request-URI Too Large";
case HttpURLConnection.HTTP_UNSUPPORTED_TYPE: return "Unsupported Media Type";
// 5xx
case HttpURLConnection.HTTP_INTERNAL_ERROR: return "Internal Server Error";
case HttpURLConnection.HTTP_NOT_IMPLEMENTED: return "Not Implemented";
case HttpURLConnection.HTTP_BAD_GATEWAY: return "Bad Gateway";
case HttpURLConnection.HTTP_UNAVAILABLE: return "Service Unavailable";
case HttpURLConnection.HTTP_GATEWAY_TIMEOUT: return "Gateway Timeout";
case HttpURLConnection.HTTP_VERSION: return "Version Not Supported";
default: return "unknown";
}
}
/**
* 判断http请求是否有效
* {@link HttpURLConnection#HTTP_BAD_GATEWAY} or {@link HttpURLConnection#HTTP_BAD_METHOD}
* or {@link HttpURLConnection#HTTP_BAD_REQUEST} 无法重试
*
* @param errorCode http 返回码
* @return {@code true} 无效请求;{@code false} 有效请求
*/
public static boolean httpIsBadRequest(int errorCode) {
return errorCode == HttpURLConnection.HTTP_BAD_GATEWAY
|| errorCode == HttpURLConnection.HTTP_BAD_METHOD
|| errorCode == HttpURLConnection.HTTP_BAD_REQUEST;
}