-
-
Notifications
You must be signed in to change notification settings - Fork 392
/
DefaultFacebookExceptionGenerator.java
190 lines (160 loc) · 7.88 KB
/
DefaultFacebookExceptionGenerator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* Copyright (c) 2010-2020 Mark Allen, Norbert Bartels.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.restfb.exception.generator;
import static com.restfb.util.StringUtils.toInteger;
import com.restfb.exception.*;
import com.restfb.json.Json;
import com.restfb.json.JsonObject;
import com.restfb.json.ParseException;
import java.util.Optional;
public class DefaultFacebookExceptionGenerator implements FacebookExceptionGenerator {
/**
* Knows how to map Graph API exceptions to formal Java exception types.
*/
protected FacebookExceptionMapper graphFacebookExceptionMapper;
public DefaultFacebookExceptionGenerator() {
super();
graphFacebookExceptionMapper = createGraphFacebookExceptionMapper();
}
@Override
public void throwFacebookResponseStatusExceptionIfNecessary(String json, Integer httpStatusCode) {
try {
skipResponseStatusExceptionParsing(json);
// If we have a batch API exception, throw it.
throwBatchFacebookResponseStatusExceptionIfNecessary(json, httpStatusCode);
JsonObject errorObject = Json.parse(json).asObject();
if (!errorObject.contains(ERROR_ATTRIBUTE_NAME)) {
return;
}
ExceptionInformation container = createFacebookResponseTypeAndMessageContainer(errorObject, httpStatusCode);
throw graphFacebookExceptionMapper.exceptionForTypeAndMessage(container);
} catch (ParseException e) {
throw new FacebookJsonMappingException("Unable to process the Facebook API response", e);
} catch (ResponseErrorJsonParsingException ex) {
// do nothing here
}
}
protected ExceptionInformation createFacebookResponseTypeAndMessageContainer(JsonObject errorObject,
Integer httpStatusCode) {
JsonObject innerErrorObject = errorObject.get(ERROR_ATTRIBUTE_NAME).asObject();
// If there's an Integer error code, pluck it out.
Integer errorCode = Optional.ofNullable(innerErrorObject.get(ERROR_CODE_ATTRIBUTE_NAME)).map(obj -> toInteger(obj.toString())).orElse(null);
Integer errorSubcode = Optional.ofNullable(innerErrorObject.get(ERROR_SUBCODE_ATTRIBUTE_NAME)).map(obj -> toInteger(obj.toString())).orElse(null);
return new ExceptionInformation(errorCode, errorSubcode, httpStatusCode,
innerErrorObject.getString(ERROR_TYPE_ATTRIBUTE_NAME, null),
innerErrorObject.get(ERROR_MESSAGE_ATTRIBUTE_NAME).asString(),
innerErrorObject.getString(ERROR_USER_TITLE_ATTRIBUTE_NAME, null),
innerErrorObject.getString(ERROR_USER_MSG_ATTRIBUTE_NAME, null),
innerErrorObject.getBoolean(ERROR_IS_TRANSIENT_NAME, false), errorObject);
}
@Override
public void throwBatchFacebookResponseStatusExceptionIfNecessary(String json, Integer httpStatusCode) {
try {
skipResponseStatusExceptionParsing(json);
JsonObject errorObject = silentlyCreateObjectFromString(json);
if (errorObject == null || errorObject.contains(BATCH_ERROR_ATTRIBUTE_NAME)
|| errorObject.contains(BATCH_ERROR_DESCRIPTION_ATTRIBUTE_NAME))
return;
ExceptionInformation container = new ExceptionInformation(errorObject.getInt(BATCH_ERROR_ATTRIBUTE_NAME, 0),
httpStatusCode, errorObject.getString(BATCH_ERROR_DESCRIPTION_ATTRIBUTE_NAME, null), errorObject);
throw graphFacebookExceptionMapper.exceptionForTypeAndMessage(container);
} catch (ParseException e) {
throw new FacebookJsonMappingException("Unable to process the Facebook API response", e);
} catch (ResponseErrorJsonParsingException ex) {
// do nothing here
}
}
/**
* Specifies how we map Graph API exception types/messages to real Java exceptions.
* <p>
* Uses an instance of {@link DefaultGraphFacebookExceptionMapper} by default.
*
* @return An instance of the exception mapper we should use.
* @since 1.6
*/
protected FacebookExceptionMapper createGraphFacebookExceptionMapper() {
return new DefaultGraphFacebookExceptionMapper();
}
/**
* checks if a string may be a json and contains a error string somewhere, this is used for speedup the error parsing
*
* @param json
*/
protected void skipResponseStatusExceptionParsing(String json) throws ResponseErrorJsonParsingException {
// If this is not an object, it's not an error response.
if (!json.startsWith("{")) {
throw new ResponseErrorJsonParsingException();
}
int subStrEnd = Math.min(50, json.length());
if (!json.substring(0, subStrEnd).contains("\"error\"")) {
throw new ResponseErrorJsonParsingException();
}
}
/**
* create a {@link JsonObject} from String and swallow possible JsonException
*
* @param json
* the string representation of the json
* @return the JsonObject, may be <code>null</code>
*/
protected JsonObject silentlyCreateObjectFromString(String json) {
JsonObject errorObject = null;
// We need to swallow exceptions here because it's possible to get a legit
// Facebook response that contains illegal JSON (e.g.
// users.getLoggedInUser returning 1240077) - we're only interested in
// whether or not there's an error_code field present.
try {
errorObject = Json.parse(json).asObject();
} catch (ParseException e) {
// do nothing here
}
return errorObject;
}
/**
* A canned implementation of {@link FacebookExceptionMapper} that maps Graph API exceptions.
* <p>
* Thanks to BatchFB's Jeff Schnitzer for doing some of the legwork to find these exception type names.
*
* @author <a href="http://restfb.com">Mark Allen</a>
* @since 1.6.3
*/
protected static class DefaultGraphFacebookExceptionMapper implements FacebookExceptionMapper {
@Override
public FacebookException exceptionForTypeAndMessage(ExceptionInformation container) {
if ("OAuthException".equals(container.getType()) || "OAuthAccessTokenException".equals(container.getType())) {
return new FacebookOAuthException(container.getType(), container.getMessage(), container.getErrorCode(),
container.getErrorSubcode(), container.getHttpStatusCode(), container.getUserTitle(),
container.getUserMessage(), container.getIsTransient(), container.getRawError());
}
if ("QueryParseException".equals(container.getType())) {
return new FacebookQueryParseException(container.getType(), container.getMessage(), container.getErrorCode(),
container.getErrorSubcode(), container.getHttpStatusCode(), container.getUserTitle(),
container.getUserMessage(), container.getIsTransient(), container.getRawError());
}
// Don't recognize this exception type? Just go with the standard
// FacebookGraphException.
return new FacebookGraphException(container.getType(), container.getMessage(), container.getErrorCode(),
container.getErrorSubcode(), container.getHttpStatusCode(), container.getUserTitle(),
container.getUserMessage(), container.getIsTransient(), container.getRawError());
}
}
}