package de.juplo.facebook.errors;
-import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
-import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
+import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.springframework.core.io.buffer.DataBufferUtils;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+import org.springframework.web.reactive.function.BodyExtractors;
+import org.springframework.web.reactive.function.client.ClientResponse;
+import reactor.core.publisher.Mono;
final static Logger LOG = LoggerFactory.getLogger(GraphApiException.class);
final static ObjectMapper OBJECT_MAPPER;
- private final FacebookErrorMessage error;
+ public final HttpStatus status;
+ public final HttpHeaders headers;
+ public final FacebookErrorMessage error;
static
}
- public static GraphApiException create(InputStream in)
- throws
- IOException,
- JsonParseException,
- JsonMappingException
+
+ public static Mono<GraphApiException> create(ClientResponse response)
{
- return create(OBJECT_MAPPER.readValue(in, FacebookErrorMessage.class));
+ return
+ response
+ .createException()
+ .map(we -> create(response.statusCode(), response.headers().asHttpHeaders(), we.getResponseBodyAsByteArray()));
}
- public static GraphApiException create(byte[] message)
- throws
- IOException,
- JsonParseException,
- JsonMappingException
+ public static GraphApiException create(
+ HttpStatus status,
+ HttpHeaders headers,
+ InputStream in
+ )
{
- return create(OBJECT_MAPPER.readValue(message, FacebookErrorMessage.class));
+ try
+ {
+ return create(status, headers, OBJECT_MAPPER.readValue(in, FacebookErrorMessage.class));
+ }
+ catch (IOException | RuntimeException e)
+ {
+ return new ErrorResponseParsingErrorException(status, headers, e);
+ }
}
- public static GraphApiException create(FacebookErrorMessage error)
+ public static GraphApiException create(
+ HttpStatus status,
+ HttpHeaders headers,
+ byte[] message
+ )
+ {
+ return create(status, headers, new ByteArrayInputStream(message));
+ }
+
+ public static GraphApiException create(
+ HttpStatus status,
+ HttpHeaders headers,
+ FacebookErrorMessage error
+ )
{
// see: http://fbdevwiki.com/wiki/Error_codes
switch(error.code)
{
// 1..99: general errors
- case 1: return new UnknownErrorException(error);
- case 2: return new UnexpectedErrorException(error);
- case 4: return new ApplicationRequestLimitReachedException(error);
- case 10: return new AuthorizationMissingException(error);
- case 12: return new DeprecatedException(error);
- case 17: return new AccountRequestLimitReachedException(error);
- case 21: return new PageMigratedException(error);
- case 32: return new PageRequestLimitReachedException(error);
+ case 1: return new UnknownErrorException(status, headers, error);
+ case 2: return new UnexpectedErrorException(status, headers, error);
+ case 4: return new ApplicationRequestLimitReachedException(status, headers, error);
+ case 10: return new AuthorizationMissingException(status, headers, error);
+ case 12: return new DeprecatedException(status, headers, error);
+ case 17: return new AccountRequestLimitReachedException(status, headers, error);
+ case 21: return new PageMigratedException(status, headers, error);
+ case 32: return new PageRequestLimitReachedException(status, headers, error);
// 100..199: graph method errors
- case 100: return new UnsupportedGetRequestException(error);
- case 102: return new UserAccessTokenRequiredException(error);
- case 104: return new AccessTokenRequiredException(error);
+ case 100: return new UnsupportedGetRequestException(status, headers, error);
+ case 102: return new UserAccessTokenRequiredException(status, headers, error);
+ case 104: return new AccessTokenRequiredException(status, headers, error);
+ case 190: return new AccessTokenExpiredException(status, headers, error);
// 200..299: permission errors
- case 200:
+ case 200: return new ApplicationNotAuthorizedByUserException(status, headers, error);
case 201:
case 202:
case 203:
case 296:
case 297:
case 298:
- case 299: return new AuthorizationMissingException(error);
+ case 299: return new AuthorizationMissingException(status, headers, error);
// 200..299: permission errors
// 300..399: data editing errors ?
- case 341: return new TemporaryRateLimitExceededException(error);
+ case 341: return new TemporaryRateLimitExceededException(status, headers, error);
// 400..449: authentication error
// 450..499: session errors
// 500..599: application messaging errors ?
- case 506: return new MultipleConcurrentPostsException(error);
+ case 506: return new MultipleConcurrentPostsException(status, headers, error);
// 600..699: FQL errors
- case 613: return new CustomRequestLimitReachedException(error);
+ case 613: return new RateLimitExceededException(status, headers, error);
// 700..749: ref errors
// 750..799: application integration errors
// 900..949: application information errors
// 950..999: batch api errors
// 1000..1099: event api errors
// 1100..1199: live-message errors
- case 1609005: return new LinkPostFailureException(error);
- case 2200: return new CallbackVerificationFailedException(error);
+ case 1609005: return new LinkPostFailureException(status, headers, error);
+ case 2200: return new CallbackVerificationFailedException(status, headers, error);
default:
- LOG.info("unmapped error: {}", error);
- return new UnmappedErrorException(error);
+ GraphApiException e = new UnmappedErrorException(status, headers, error);
+ LOG.info("unmapped error: {}", e.toString());
+ return e;
}
}
- protected GraphApiException(FacebookErrorMessage error)
+ protected GraphApiException(
+ HttpStatus status,
+ HttpHeaders headers,
+ FacebookErrorMessage error
+ )
{
super(error.message);
+ this.status = status;
+ this.headers = headers;
this.error = error;
}
+ public HttpStatus getStatus()
+ {
+ return status;
+ }
+
+ public HttpHeaders getHeaders()
+ {
+ return headers;
+ }
+
public Type getType()
{
return error.type == null ? null : Type.valueOf(error.type);