Search in sources :

Example 1 with ImageError

use of com.sx4.bot.entities.image.ImageError in project Sx4 by sx4-discord-bot.

the class ImageUtility method getErrorMessage.

public static MessageAction getErrorMessage(TextChannel channel, int status, String fullBody, BiFunction<Document, ImageError, MessageAction> badRequest) {
    if (status == 400) {
        Document body = Document.parse(fullBody);
        int code = body.getEmbedded(List.of("details", "code"), Integer.class);
        ImageError error = ImageError.fromCode(code);
        if (error != null && error.isUrlError()) {
            return channel.sendMessageFormat("That url could not be formed to a valid image %s", Config.get().getFailureEmote());
        }
        MessageAction messageAction = badRequest == null ? null : badRequest.apply(body, error);
        if (messageAction == null) {
            return channel.sendMessageEmbeds(ExceptionUtility.getSimpleErrorMessage(String.format("- Code: %d\n- %s", error.getCode(), body.getString("message")), "diff"));
        } else {
            return messageAction;
        }
    } else {
        return channel.sendMessageEmbeds(ExceptionUtility.getSimpleErrorMessage(String.format("- Status: %d\n- %s", status, fullBody), "diff"));
    }
}
Also used : MessageAction(net.dv8tion.jda.api.requests.restaction.MessageAction) Document(org.bson.Document) ImageError(com.sx4.bot.entities.image.ImageError)

Aggregations

ImageError (com.sx4.bot.entities.image.ImageError)1 MessageAction (net.dv8tion.jda.api.requests.restaction.MessageAction)1 Document (org.bson.Document)1