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.http.HttpHeaders;
14 import org.springframework.http.HttpStatus;
15 import org.springframework.web.reactive.function.client.ClientResponse;
16 import reactor.core.publisher.Mono;
21 * Base exception for Facebook Graph-Api exceptions.
25 public class GraphApiException extends RuntimeException
27 public enum Type { OAuthException, GraphMethodException }
30 final static Logger LOG = LoggerFactory.getLogger(GraphApiException.class);
31 final static ObjectMapper OBJECT_MAPPER;
33 public final HttpStatus status;
34 public final HttpHeaders headers;
35 public final FacebookErrorMessage error;
40 OBJECT_MAPPER = new ObjectMapper();
41 OBJECT_MAPPER.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
42 OBJECT_MAPPER.configure(DeserializationFeature.ACCEPT_FLOAT_AS_INT, false);
43 OBJECT_MAPPER.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
48 public static GraphApiException create(ClientResponse response)
52 .bodyToMono(String.class);
53 Mono<GraphApiException> e = body
54 .map(str -> create(response.statusCode(), null, str.getBytes()));
57 public static GraphApiException create(
65 return create(status, headers, OBJECT_MAPPER.readValue(in, FacebookErrorMessage.class));
67 catch (IOException | RuntimeException e)
69 return new ErrorResponseParsingErrorException(status, headers, e);
73 public static GraphApiException create(
79 return create(status, headers, new ByteArrayInputStream(message));
82 public static GraphApiException create(
85 FacebookErrorMessage error
88 // see: http://fbdevwiki.com/wiki/Error_codes
91 // 1..99: general errors
92 case 1: return new UnknownErrorException(status, headers, error);
93 case 2: return new UnexpectedErrorException(status, headers, error);
94 case 4: return new ApplicationRequestLimitReachedException(status, headers, error);
95 case 10: return new AuthorizationMissingException(status, headers, error);
96 case 12: return new DeprecatedException(status, headers, error);
97 case 17: return new AccountRequestLimitReachedException(status, headers, error);
98 case 21: return new PageMigratedException(status, headers, error);
99 case 32: return new PageRequestLimitReachedException(status, headers, error);
100 // 100..199: graph method errors
101 case 100: return new UnsupportedGetRequestException(status, headers, error);
102 case 102: return new UserAccessTokenRequiredException(status, headers, error);
103 case 104: return new AccessTokenRequiredException(status, headers, error);
104 case 190: return new AccessTokenExpiredException(status, headers, error);
105 // 200..299: permission errors
106 case 200: return new ApplicationNotAuthorizedByUserException(status, headers, error);
205 case 299: return new AuthorizationMissingException(status, headers, error);
206 // 200..299: permission errors
207 // 300..399: data editing errors ?
208 case 341: return new TemporaryRateLimitExceededException(status, headers, error);
209 // 400..449: authentication error
210 // 450..499: session errors
211 // 500..599: application messaging errors ?
212 case 506: return new MultipleConcurrentPostsException(status, headers, error);
213 // 600..699: FQL errors
214 case 613: return new RateLimitExceededException(status, headers, error);
215 // 700..749: ref errors
216 // 750..799: application integration errors
217 // 900..949: application information errors
218 // 950..999: batch api errors
219 // 1000..1099: event api errors
220 // 1100..1199: live-message errors
221 case 1609005: return new LinkPostFailureException(status, headers, error);
222 case 2200: return new CallbackVerificationFailedException(status, headers, error);
225 GraphApiException e = new UnmappedErrorException(status, headers, error);
226 LOG.info("unmapped error: {}", e.toString());
232 protected GraphApiException(
235 FacebookErrorMessage error
238 super(error.message);
239 this.status = status;
240 this.headers = headers;
245 public HttpStatus getStatus()
250 public HttpHeaders getHeaders()
255 public Type getType()
257 return error.type == null ? null : Type.valueOf(error.type);
260 public Integer getCode()
265 public Integer getSubCode()
267 return error.subCode;
270 public String getUserTitle()
272 return error.userTitle;
275 public String getUserMessage()
277 return error.userMessage;
280 public String getTraceId()
282 return error.traceId;
287 public String toString()
291 return OBJECT_MAPPER.writeValueAsString(error);
293 catch(JsonProcessingException e)
295 // This should never happen. But in case of a mistake: be verbose!
296 LOG.error("could not convert message into JSON: {}", e);
297 return e.getMessage();