|
1 | 1 | package io.github.kloping.qqbot;
|
2 | 2 |
|
| 3 | +import com.alibaba.fastjson.JSON; |
| 4 | +import com.alibaba.fastjson.JSONObject; |
3 | 5 | import io.github.kloping.common.Public;
|
4 | 6 | import io.github.kloping.qqbot.api.BotContent;
|
| 7 | +import io.github.kloping.qqbot.api.exc.RequestException; |
5 | 8 | import io.github.kloping.qqbot.entities.Bot;
|
| 9 | +import io.github.kloping.qqbot.entities.exc.QBotError; |
6 | 10 | import io.github.kloping.qqbot.entities.qqpd.message.RawMessage;
|
7 | 11 | import io.github.kloping.qqbot.http.data.ActionResult;
|
8 |
| -import io.github.kloping.qqbot.utils.RequestException; |
9 | 12 | import io.github.kloping.spt.annotations.AutoStand;
|
10 | 13 | import io.github.kloping.spt.annotations.AutoStandAfter;
|
11 | 14 | import io.github.kloping.spt.annotations.Entity;
|
|
18 | 21 | import org.jsoup.nodes.Document;
|
19 | 22 |
|
20 | 23 | import java.awt.*;
|
| 24 | +import java.lang.reflect.Constructor; |
21 | 25 | import java.lang.reflect.Method;
|
22 | 26 |
|
23 | 27 | /**
|
@@ -45,7 +49,6 @@ public void after(HttpClientManager manager) {
|
45 | 49 | public void receive(HttpClientManager manager, String url, Integer code, Class<?> interface0, Method method,
|
46 | 50 | Connection.Method reqMethod, Class<?> cla, Object o, Document metadata) {
|
47 | 51 | if (o == null || code == null || metadata == null) return;
|
48 |
| - |
49 | 52 | logger.log(String.format("Use the (%s) method through the (%s) interface to request " +
|
50 | 53 | "the data obtained by the response code of the (%s) URL is (%s), " +
|
51 | 54 | "and (%s) may be converted to (%s) type Will be processed and filtered",
|
@@ -78,10 +81,29 @@ public void receive(HttpClientManager manager, String url, Integer code, Class<?
|
78 | 81 | }
|
79 | 82 | });
|
80 | 83 | if (code >= 400 || code < 200) {
|
81 |
| - throw new RequestException(code, metadata.body().wholeText(), url, method.getName()); |
| 84 | + RequestException requestException = null; |
| 85 | + String bodyJson = metadata.body().wholeText(); |
| 86 | + JSONObject exjo = JSON.parseObject(bodyJson); |
| 87 | + QBotError error = exjo.toJavaObject(QBotError.class); |
| 88 | + //加入exc对象 |
| 89 | + int eccode = exjo.getInteger("code"); |
| 90 | + if (Resource.CODE2EXCEPTION.containsKey(eccode)) { |
| 91 | + try { |
| 92 | + Class<? extends RequestException> exceptionClass = Resource.CODE2EXCEPTION.get(eccode); |
| 93 | + Constructor constructor = exceptionClass.getConstructor(int.class, String.class, String.class, String.class); |
| 94 | + requestException = (RequestException) constructor.newInstance(eccode, bodyJson, url, method.getName()); |
| 95 | + } catch (Exception e) { |
| 96 | + logger.error(e.getMessage()); |
| 97 | + } |
| 98 | + } else { |
| 99 | + requestException = new RequestException(eccode, bodyJson, url, method.getName()); |
| 100 | + } |
| 101 | + if (requestException != null) requestException.setData(error); |
| 102 | + throw requestException; |
82 | 103 | }
|
83 | 104 | }
|
84 | 105 |
|
| 106 | + |
85 | 107 | public void fillAll(Class<?> cla, Object o) {
|
86 | 108 | if (o instanceof BotContent) {
|
87 | 109 | BotContent content = (BotContent) o;
|
|
0 commit comments