use of org.javacord.api.util.rest.RestRequestResponseInformation in project Javacord by BtoBastian.
the class DiscordExceptionValidatorImpl method validateException.
@Override
public void validateException(DiscordException exception) throws AssertionError {
Optional<RestRequestHttpResponseCode> expectedResponseCodeOptional = RestRequestHttpResponseCode.fromDiscordExceptionClass(exception.getClass());
Optional<RestRequestResponseInformation> restRequestResponseInformationOptional = exception.getResponse();
if (expectedResponseCodeOptional.isPresent()) {
// if this class or a superclass of it is the expected exception class for an HTTP response code
RestRequestHttpResponseCode expectedResponseCode = expectedResponseCodeOptional.get();
if (restRequestResponseInformationOptional.isPresent()) {
// if there is an actual result but the response code does not match
if (restRequestResponseInformationOptional.get().getCode() != expectedResponseCode.getCode()) {
throw new AssertionError(exception.getClass().getSimpleName() + " should only be thrown on " + expectedResponseCode.getCode() + " HTTP response code (was " + restRequestResponseInformationOptional.get().getCode() + ") or it should not be a subclass of " + expectedResponseCode.getDiscordExceptionClass().orElseThrow(AssertionError::new).getSimpleName() + ". Please contact the developer!");
}
} else {
// if there is no actual result
throw new AssertionError(exception.getClass().getSimpleName() + " should only be thrown on " + expectedResponseCode.getCode() + " HTTP response code but there is no result. " + "Please contact the developer!");
}
} else {
// if this class or a superclass of it is not the expected exception class for an HTTP response code
Optional<Integer> responseCodeOptional = restRequestResponseInformationOptional.map(RestRequestResponseInformation::getCode);
Optional<? extends Class<? extends DiscordException>> discordExceptionClassOptional = responseCodeOptional.flatMap(RestRequestHttpResponseCode::fromCode).flatMap(RestRequestHttpResponseCode::getDiscordExceptionClass);
// if there is a result present and for its HTTP response code exists an expected exception
if (discordExceptionClassOptional.isPresent()) {
throw new AssertionError("For " + responseCodeOptional.orElseThrow(AssertionError::new) + " HTTP response code an exception of type " + discordExceptionClassOptional.get().getSimpleName() + " or a sub-class thereof should be thrown. " + "Please contact the developer!");
}
}
}
use of org.javacord.api.util.rest.RestRequestResponseInformation in project Javacord by BtoBastian.
the class RestRequest method executeBlocking.
/**
* Executes the request blocking.
*
* @return The result of the request.
* @throws Exception If something went wrong while executing the request.
*/
public RestRequestResult executeBlocking() throws Exception {
api.getGlobalRatelimiter().ifPresent(ratelimiter -> {
try {
ratelimiter.requestQuota();
} catch (InterruptedException e) {
logger.warn("Encountered unexpected ratelimiter interrupt", e);
}
});
Request.Builder requestBuilder = new Request.Builder();
HttpUrl.Builder httpUrlBuilder = endpoint.getOkHttpUrl(urlParameters).newBuilder();
queryParameters.forEach(httpUrlBuilder::addQueryParameter);
requestBuilder.url(httpUrlBuilder.build());
RequestBody requestBody;
if (multipartBody != null) {
requestBody = multipartBody;
} else if (body != null) {
requestBody = RequestBody.create(MediaType.parse("application/json"), body);
} else {
requestBody = RequestBody.create(null, new byte[0]);
}
switch(method) {
case GET:
requestBuilder.get();
break;
case POST:
requestBuilder.post(requestBody);
break;
case PUT:
requestBuilder.put(requestBody);
break;
case DELETE:
requestBuilder.delete(requestBody);
break;
case PATCH:
requestBuilder.patch(requestBody);
break;
default:
throw new IllegalArgumentException("Unsupported http method!");
}
if (includeAuthorizationHeader) {
requestBuilder.addHeader("authorization", api.getPrefixedToken());
}
headers.forEach(requestBuilder::addHeader);
logger.debug("Trying to send {} request to {}{}", method::name, () -> endpoint.getFullUrl(urlParameters), () -> body != null ? " with body " + body : "");
try (Response response = getApi().getHttpClient().newCall(requestBuilder.build()).execute()) {
RestRequestResult result = new RestRequestResult(this, response);
logger.debug("Sent {} request to {} and received status code {} with{} body{}", method::name, () -> endpoint.getFullUrl(urlParameters), response::code, () -> result.getBody().map(b -> "").orElse(" empty"), () -> result.getStringBody().map(s -> " " + s).orElse(""));
if (response.code() >= 300 || response.code() < 200) {
RestRequestInformation requestInformation = asRestRequestInformation();
RestRequestResponseInformation responseInformation = new RestRequestResponseInformationImpl(requestInformation, result);
Optional<RestRequestHttpResponseCode> responseCode = RestRequestHttpResponseCode.fromCode(response.code());
// Check if the response body contained a know error code
if (!result.getJsonBody().isNull() && result.getJsonBody().has("code")) {
int code = result.getJsonBody().get("code").asInt();
String message = result.getJsonBody().has("message") ? result.getJsonBody().get("message").asText() : null;
Optional<? extends DiscordException> discordException = RestRequestResultErrorCode.fromCode(code, responseCode.orElse(null)).flatMap(restRequestResultCode -> restRequestResultCode.getDiscordException(origin, (message == null) ? restRequestResultCode.getMeaning() : message, requestInformation, responseInformation));
// There's an exception for this specific response code
if (discordException.isPresent()) {
throw discordException.get();
}
}
switch(response.code()) {
case 429:
// A 429 will be handled in the RatelimitManager class
return result;
default:
// There are specific exceptions for specific response codes (e.g. NotFoundException for 404)
Optional<? extends DiscordException> discordException = responseCode.flatMap(restRequestHttpResponseCode -> restRequestHttpResponseCode.getDiscordException(origin, "Received a " + response.code() + " response from Discord with" + (result.getBody().isPresent() ? "" : " empty") + " body" + result.getStringBody().map(s -> " " + s).orElse("") + "!", requestInformation, responseInformation));
if (discordException.isPresent()) {
throw discordException.get();
} else {
// No specific exception was defined for the response code, so throw a "normal"
throw new DiscordException(origin, "Received a " + response.code() + " response from Discord with" + (result.getBody().isPresent() ? "" : " empty") + " body" + result.getStringBody().map(s -> " " + s).orElse("") + "!", requestInformation, responseInformation);
}
}
}
return result;
}
}
Aggregations