1 package de.juplo.facebook.errors;
4 import com.fasterxml.jackson.core.JsonProcessingException;
5 import com.fasterxml.jackson.databind.DeserializationFeature;
6 import com.fasterxml.jackson.databind.ObjectMapper;
7 import com.fasterxml.jackson.databind.SerializationFeature;
8 import java.io.ByteArrayInputStream;
9 import java.io.IOException;
10 import java.io.InputStream;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13 import org.springframework.core.io.buffer.DataBufferUtils;
14 import org.springframework.http.HttpHeaders;
15 import org.springframework.http.HttpStatus;
16 import org.springframework.web.reactive.function.BodyExtractors;
17 import org.springframework.web.reactive.function.client.ClientResponse;
18 import reactor.core.publisher.Mono;
23 * Base exception for Facebook Graph-Api exceptions.
27 public class GraphApiException extends RuntimeException
29 public enum Type { OAuthException, GraphMethodException }
32 final static Logger LOG = LoggerFactory.getLogger(GraphApiException.class);
33 final static ObjectMapper OBJECT_MAPPER;
35 public final HttpStatus status;
36 public final HttpHeaders headers;
37 public final FacebookErrorMessage error;
42 OBJECT_MAPPER = new ObjectMapper();
43 OBJECT_MAPPER.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
44 OBJECT_MAPPER.configure(DeserializationFeature.ACCEPT_FLOAT_AS_INT, false);
45 OBJECT_MAPPER.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
50 public static Mono<GraphApiException> create(ClientResponse response)
55 .map(we -> create(response.statusCode(), response.headers().asHttpHeaders(), we.getResponseBodyAsByteArray()));
58 public static GraphApiException create(
66 return create(status, headers, OBJECT_MAPPER.readValue(in, FacebookErrorMessage.class));
68 catch (IOException | RuntimeException e)
70 return new ErrorResponseParsingErrorException(status, headers, e);
74 public static GraphApiException create(
80 return create(status, headers, new ByteArrayInputStream(message));
83 public static GraphApiException create(
86 FacebookErrorMessage error
89 // see: http://fbdevwiki.com/wiki/Error_codes
92 // 1..99: general errors
93 case 1: return new UnknownErrorException(status, headers, error);
94 case 2: return new UnexpectedErrorException(status, headers, error);
95 case 4: return new ApplicationRequestLimitReachedException(status, headers, error);
96 case 10: return new AuthorizationMissingException(status, headers, error);
97 case 12: return new DeprecatedException(status, headers, error);
98 case 17: return new AccountRequestLimitReachedException(status, headers, error);
99 case 21: return new PageMigratedException(status, headers, error);
100 case 32: return new PageRequestLimitReachedException(status, headers, error);
101 // 100..199: graph method errors
102 case 100: return new UnsupportedGetRequestException(status, headers, error);
103 case 102: return new UserAccessTokenRequiredException(status, headers, error);
104 case 104: return new AccessTokenRequiredException(status, headers, error);
105 case 190: return new AccessTokenExpiredException(status, headers, error);
106 // 200..299: permission errors
107 case 200: return new ApplicationNotAuthorizedByUserException(status, headers, error);
206 case 299: return new AuthorizationMissingException(status, headers, error);
207 // 200..299: permission errors
208 // 300..399: data editing errors ?
209 case 341: return new TemporaryRateLimitExceededException(status, headers, error);
210 // 400..449: authentication error
211 // 450..499: session errors
212 // 500..599: application messaging errors ?
213 case 506: return new MultipleConcurrentPostsException(status, headers, error);
214 // 600..699: FQL errors
215 case 613: return new RateLimitExceededException(status, headers, error);
216 // 700..749: ref errors
217 // 750..799: application integration errors
218 // 900..949: application information errors
219 // 950..999: batch api errors
220 // 1000..1099: event api errors
221 // 1100..1199: live-message errors
222 case 1609005: return new LinkPostFailureException(status, headers, error);
223 case 2200: return new CallbackVerificationFailedException(status, headers, error);
226 GraphApiException e = new UnmappedErrorException(status, headers, error);
227 LOG.info("unmapped error: {}", e.toString());
233 protected GraphApiException(
236 FacebookErrorMessage error
239 super(error.message);
240 this.status = status;
241 this.headers = headers;
246 public HttpStatus getStatus()
251 public HttpHeaders getHeaders()
256 public Type getType()
258 return error.type == null ? null : Type.valueOf(error.type);
261 public Integer getCode()
266 public Integer getSubCode()
268 return error.subCode;
271 public String getUserTitle()
273 return error.userTitle;
276 public String getUserMessage()
278 return error.userMessage;
281 public String getTraceId()
283 return error.traceId;
288 public String toString()
292 return OBJECT_MAPPER.writeValueAsString(error);
294 catch(JsonProcessingException e)
296 // This should never happen. But in case of a mistake: be verbose!
297 LOG.error("could not convert message into JSON: {}", e);
298 return e.getMessage();