本文整理匯總了Java中com.google.api.client.googleapis.json.GoogleJsonResponseException.from方法的典型用法代碼示例。如果您正苦於以下問題:Java GoogleJsonResponseException.from方法的具體用法?Java GoogleJsonResponseException.from怎麽用?Java GoogleJsonResponseException.from使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.google.api.client.googleapis.json.GoogleJsonResponseException
的用法示例。
在下文中一共展示了GoogleJsonResponseException.from方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: makeResponseException
import com.google.api.client.googleapis.json.GoogleJsonResponseException; //導入方法依賴的package包/類
/** Returns a valid GoogleJsonResponseException for the given status code and error message. */
private GoogleJsonResponseException makeResponseException(
final int statusCode,
final String message) throws Exception {
HttpTransport transport = new MockHttpTransport() {
@Override
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
return new MockLowLevelHttpRequest() {
@Override
public LowLevelHttpResponse execute() throws IOException {
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
response.setStatusCode(statusCode);
response.setContentType(Json.MEDIA_TYPE);
response.setContent(String.format(
"{\"error\":{\"code\":%d,\"message\":\"%s\",\"domain\":\"global\","
+ "\"reason\":\"duplicate\"}}",
statusCode,
message));
return response;
}};
}};
HttpRequest request = transport.createRequestFactory()
.buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL)
.setThrowExceptionOnExecuteError(false);
return GoogleJsonResponseException.from(new JacksonFactory(), request.execute());
}
示例2: googleJsonResponseException
import com.google.api.client.googleapis.json.GoogleJsonResponseException; //導入方法依賴的package包/類
/**
* Builds a fake GoogleJsonResponseException for testing API error handling.
*/
private static GoogleJsonResponseException googleJsonResponseException(
final int status, final String reason, final String message) throws IOException {
final JsonFactory jsonFactory = new JacksonFactory();
HttpTransport transport = new MockHttpTransport() {
@Override
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
ErrorInfo errorInfo = new ErrorInfo();
errorInfo.setReason(reason);
errorInfo.setMessage(message);
errorInfo.setFactory(jsonFactory);
GenericJson error = new GenericJson();
error.set("code", status);
error.set("errors", Arrays.asList(errorInfo));
error.setFactory(jsonFactory);
GenericJson errorResponse = new GenericJson();
errorResponse.set("error", error);
errorResponse.setFactory(jsonFactory);
return new MockLowLevelHttpRequest().setResponse(
new MockLowLevelHttpResponse().setContent(errorResponse.toPrettyString())
.setContentType(Json.MEDIA_TYPE).setStatusCode(status));
}
};
HttpRequest request =
transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
request.setThrowExceptionOnExecuteError(false);
HttpResponse response = request.execute();
return GoogleJsonResponseException.from(jsonFactory, response);
}
示例3: googleJsonResponseException
import com.google.api.client.googleapis.json.GoogleJsonResponseException; //導入方法依賴的package包/類
private static GoogleJsonResponseException googleJsonResponseException(
final int status, final ErrorInfo errorInfo, final String httpStatusString)
throws IOException {
final JsonFactory jsonFactory = new JacksonFactory();
HttpTransport transport = new MockHttpTransport() {
@Override
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
errorInfo.setFactory(jsonFactory);
GoogleJsonError jsonError = new GoogleJsonError();
jsonError.setCode(status);
jsonError.setErrors(Arrays.asList(errorInfo));
jsonError.setMessage(httpStatusString);
jsonError.setFactory(jsonFactory);
GenericJson errorResponse = new GenericJson();
errorResponse.set("error", jsonError);
errorResponse.setFactory(jsonFactory);
return new MockLowLevelHttpRequest().setResponse(
new MockLowLevelHttpResponse().setContent(errorResponse.toPrettyString())
.setContentType(Json.MEDIA_TYPE).setStatusCode(status));
}
};
HttpRequest request =
transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
request.setThrowExceptionOnExecuteError(false);
HttpResponse response = request.execute();
return GoogleJsonResponseException.from(jsonFactory, response);
}
示例4: create
import com.google.api.client.googleapis.json.GoogleJsonResponseException; //導入方法依賴的package包/類
/**
* @param statusCode the status code that should be in the returned {@link
* GoogleJsonResponseException}
* @return a {@link GoogleJsonResponseException} with the status code {@code statusCode}
* @throws IOException shouldn't occur
*/
public static GoogleJsonResponseException create(int statusCode) throws IOException {
HttpResponse response = createHttpResponse(statusCode, null);
return GoogleJsonResponseException.from(new JacksonFactory(), response);
}
開發者ID:google,項目名稱:java-monitoring-client-library,代碼行數:11,代碼來源:GoogleJsonResponseExceptionHelper.java