com.urbanairship.api.client.RequestError Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-client Show documentation
Show all versions of java-client Show documentation
The Urban Airship Java client library
/*
* Copyright (c) 2013-2016. Urban Airship and Contributors
*/
package com.urbanairship.api.client;
import com.google.common.base.Optional;
import com.urbanairship.api.client.parse.RequestErrorObjectMapper;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import java.io.IOException;
import java.util.Map;
/**
* Error object for API requests.
* This is populated the as many details as are possible at the time of the
* error. Optional values from Google Guava are used in place of values
* that may not be present.
*/
public final class RequestError {
/* Header keys, values */
private final static String CONTENT_TYPE_TEXT_HTML = "text/html";
private final static String CONTENT_TYPE_JSON = "application/json";
private final static String UA_APPLICATION_JSON = "application/vnd.urbanairship+json";
private final boolean ok;
private final Optional operationId;
private final String error;
private final Optional errorCode;
private final Optional details;
private RequestError(boolean ok, Optional operationId, String error, Optional errorCode,
Optional details) {
this.ok = ok;
this.operationId = operationId;
if (error == null || error.isEmpty()) {
throw new IllegalArgumentException("Error cannot be null or empty");
}
this.error = error;
this.errorCode = errorCode;
this.details = details;
}
/**
* Create an APIError from the response if it conforms to the API v3
* Currently, three types of error bodies are returned, text/html strings,
* basic JSON errors {"error":"message"} and the v3 error spec. This method
* parses between three, and returns a best effort response.
*
* @param body Response body for the request that caused the exception
* @return APIError
* @throws IOException
*/
public static RequestError errorFromResponse(String body, String contentType) throws IOException {
// Text/html
if (contentType.equalsIgnoreCase(CONTENT_TYPE_TEXT_HTML)) {
return nonJSONError(body);
}
// JSON but not v3
else if (contentType.equalsIgnoreCase(CONTENT_TYPE_JSON)) {
return nonV3JSONError(body);
}
// v3 JSON parsing
else if (contentType.equalsIgnoreCase(UA_APPLICATION_JSON)) {
ObjectMapper mapper = RequestErrorObjectMapper.getInstance();
return mapper.readValue(body, RequestError.class);
}
// wut?
else {
return RequestError.newBuilder()
.setError("Unknown response parsing error")
.build();
}
}
/*
Currently sending text/plain errors for some requests, currently 404's
do this. API-291, 12JUL13
*/
@Deprecated
private static RequestError nonJSONError(String body) throws
IOException {
return RequestError.newBuilder()
.setError(body)
.build();
}
/*
Currently returning JSON, but not API v3 JSON for some requests.
API-281, 12JUL13
*/
@Deprecated
private static RequestError nonV3JSONError(String body) throws IOException {
ObjectMapper mapper = RequestErrorObjectMapper.getInstance();
Map errorMsg =
mapper.readValue(body,
new TypeReference
© 2015 - 2025 Weber Informatics LLC | Privacy Policy