1package im.conversations.android.xmpp;
 2
 3import com.google.common.base.Strings;
 4import im.conversations.android.xmpp.model.error.Condition;
 5import im.conversations.android.xmpp.model.error.Error;
 6import im.conversations.android.xmpp.model.stanza.Iq;
 7
 8public class IqErrorException extends Exception {
 9
10    private final Iq response;
11
12    public IqErrorException(Iq response) {
13        super(getErrorText(response));
14        this.response = response;
15    }
16
17    public Error getError() {
18        return this.response.getError();
19    }
20
21    public Condition getErrorCondition() {
22        final var error = getError();
23        if (error == null) {
24            return null;
25        }
26        return error.getCondition();
27    }
28
29    private static String getErrorText(final Iq response) {
30        final var error = response.getError();
31        final var text = error == null ? null : error.getText();
32        final var textContent = text == null ? null : text.getContent();
33        if (Strings.isNullOrEmpty(textContent)) {
34            final var condition = error == null ? null : error.getExtension(Condition.class);
35            return condition == null ? null : condition.getName();
36        }
37        return textContent;
38    }
39
40    public Iq getResponse() {
41        return this.response;
42    }
43}