Search in sources :

Example 1 with ParsingException

use of net.dv8tion.jda.api.exceptions.ParsingException in project JDA by DV8FromTheWorld.

the class DataArray method toPrettyString.

@Nonnull
public String toPrettyString() {
    DefaultPrettyPrinter.Indenter indent = new DefaultIndenter("    ", DefaultIndenter.SYS_LF);
    DefaultPrettyPrinter printer = new DefaultPrettyPrinter();
    printer.withObjectIndenter(indent).withArrayIndenter(indent);
    try {
        return mapper.writer(printer).writeValueAsString(data);
    } catch (JsonProcessingException e) {
        throw new ParsingException(e);
    }
}
Also used : DefaultPrettyPrinter(com.fasterxml.jackson.core.util.DefaultPrettyPrinter) DefaultIndenter(com.fasterxml.jackson.core.util.DefaultIndenter) ParsingException(net.dv8tion.jda.api.exceptions.ParsingException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Nonnull(javax.annotation.Nonnull)

Example 2 with ParsingException

use of net.dv8tion.jda.api.exceptions.ParsingException in project JDA by DV8FromTheWorld.

the class DataObject method toPrettyString.

@Nonnull
public String toPrettyString() {
    DefaultPrettyPrinter.Indenter indent = new DefaultIndenter("    ", DefaultIndenter.SYS_LF);
    DefaultPrettyPrinter printer = new DefaultPrettyPrinter();
    printer.withObjectIndenter(indent).withArrayIndenter(indent);
    try {
        return mapper.writer(printer).writeValueAsString(data);
    } catch (JsonProcessingException e) {
        throw new ParsingException(e);
    }
}
Also used : DefaultPrettyPrinter(com.fasterxml.jackson.core.util.DefaultPrettyPrinter) DefaultIndenter(com.fasterxml.jackson.core.util.DefaultIndenter) ParsingException(net.dv8tion.jda.api.exceptions.ParsingException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Nonnull(javax.annotation.Nonnull)

Example 3 with ParsingException

use of net.dv8tion.jda.api.exceptions.ParsingException in project JDA by DV8FromTheWorld.

the class MessagePaginationActionImpl method handleSuccess.

@Override
protected void handleSuccess(Response response, Request<List<Message>> request) {
    DataArray array = response.getArray();
    List<Message> messages = new ArrayList<>(array.length());
    EntityBuilder builder = api.getEntityBuilder();
    for (int i = 0; i < array.length(); i++) {
        try {
            Message msg = builder.createMessageWithChannel(array.getObject(i), channel, false);
            messages.add(msg);
        } catch (ParsingException | NullPointerException e) {
            LOG.warn("Encountered an exception in MessagePagination", e);
        } catch (IllegalArgumentException e) {
            if (EntityBuilder.UNKNOWN_MESSAGE_TYPE.equals(e.getMessage()))
                LOG.warn("Skipping unknown message type during pagination", e);
            else
                LOG.warn("Unexpected issue trying to parse message during pagination", e);
        }
    }
    if (order == PaginationOrder.FORWARD)
        Collections.reverse(messages);
    if (useCache)
        cached.addAll(messages);
    if (!messages.isEmpty()) {
        last = messages.get(messages.size() - 1);
        lastKey = last.getIdLong();
    }
    request.onSuccess(messages);
}
Also used : ParsingException(net.dv8tion.jda.api.exceptions.ParsingException) ArrayList(java.util.ArrayList) EntityBuilder(net.dv8tion.jda.internal.entities.EntityBuilder) DataArray(net.dv8tion.jda.api.utils.data.DataArray)

Example 4 with ParsingException

use of net.dv8tion.jda.api.exceptions.ParsingException in project JDA by DV8FromTheWorld.

the class ReactionPaginationActionImpl method handleSuccess.

@Override
protected void handleSuccess(Response response, Request<List<User>> request) {
    final EntityBuilder builder = api.getEntityBuilder();
    final DataArray array = response.getArray();
    final List<User> users = new LinkedList<>();
    for (int i = 0; i < array.length(); i++) {
        try {
            final User user = builder.createUser(array.getObject(i));
            users.add(user);
            if (useCache)
                cached.add(user);
            last = user;
            lastKey = last.getIdLong();
        } catch (ParsingException | NullPointerException e) {
            LOG.warn("Encountered exception in ReactionPagination", e);
        }
    }
    request.onSuccess(users);
}
Also used : User(net.dv8tion.jda.api.entities.User) ParsingException(net.dv8tion.jda.api.exceptions.ParsingException) EntityBuilder(net.dv8tion.jda.internal.entities.EntityBuilder) DataArray(net.dv8tion.jda.api.utils.data.DataArray) LinkedList(java.util.LinkedList)

Example 5 with ParsingException

use of net.dv8tion.jda.api.exceptions.ParsingException in project JDA by DV8FromTheWorld.

the class AuditLogPaginationActionImpl method handleSuccess.

@Override
protected void handleSuccess(Response response, Request<List<AuditLogEntry>> request) {
    DataObject obj = response.getObject();
    DataArray users = obj.getArray("users");
    DataArray webhooks = obj.getArray("webhooks");
    DataArray entries = obj.getArray("audit_log_entries");
    List<AuditLogEntry> list = new ArrayList<>(entries.length());
    EntityBuilder builder = api.getEntityBuilder();
    TLongObjectMap<DataObject> userMap = new TLongObjectHashMap<>();
    for (int i = 0; i < users.length(); i++) {
        DataObject user = users.getObject(i);
        userMap.put(user.getLong("id"), user);
    }
    TLongObjectMap<DataObject> webhookMap = new TLongObjectHashMap<>();
    for (int i = 0; i < webhooks.length(); i++) {
        DataObject webhook = webhooks.getObject(i);
        webhookMap.put(webhook.getLong("id"), webhook);
    }
    for (int i = 0; i < entries.length(); i++) {
        try {
            DataObject entry = entries.getObject(i);
            DataObject user = userMap.get(entry.getLong("user_id", 0));
            DataObject webhook = webhookMap.get(entry.getLong("target_id", 0));
            AuditLogEntry result = builder.createAuditLogEntry((GuildImpl) guild, entry, user, webhook);
            list.add(result);
            if (this.useCache)
                this.cached.add(result);
            this.last = result;
            this.lastKey = last.getIdLong();
        } catch (ParsingException | NullPointerException e) {
            LOG.warn("Encountered exception in AuditLogPagination", e);
        }
    }
    request.onSuccess(list);
}
Also used : DataObject(net.dv8tion.jda.api.utils.data.DataObject) AuditLogEntry(net.dv8tion.jda.api.audit.AuditLogEntry) ParsingException(net.dv8tion.jda.api.exceptions.ParsingException) ArrayList(java.util.ArrayList) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) EntityBuilder(net.dv8tion.jda.internal.entities.EntityBuilder) DataArray(net.dv8tion.jda.api.utils.data.DataArray)

Aggregations

ParsingException (net.dv8tion.jda.api.exceptions.ParsingException)7 DataArray (net.dv8tion.jda.api.utils.data.DataArray)5 EntityBuilder (net.dv8tion.jda.internal.entities.EntityBuilder)4 ArrayList (java.util.ArrayList)3 DataObject (net.dv8tion.jda.api.utils.data.DataObject)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 DefaultIndenter (com.fasterxml.jackson.core.util.DefaultIndenter)2 DefaultPrettyPrinter (com.fasterxml.jackson.core.util.DefaultPrettyPrinter)2 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)2 Nonnull (javax.annotation.Nonnull)2 IOException (java.io.IOException)1 SocketException (java.net.SocketException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 LinkedList (java.util.LinkedList)1 DataFormatException (java.util.zip.DataFormatException)1 AuditLogEntry (net.dv8tion.jda.api.audit.AuditLogEntry)1 ThreadChannel (net.dv8tion.jda.api.entities.ThreadChannel)1 User (net.dv8tion.jda.api.entities.User)1 JDAImpl (net.dv8tion.jda.internal.JDAImpl)1