X-Git-Url: https://juplo.de/gitweb/?p=facebook-utils;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Ffacebook%2Fexceptions%2FGraphApiException.java;h=bd0256570772cf9165d58241ef47104750f65c82;hp=1055b2091c2013befe7ce3c693131da74224b351;hb=5f2b31dc7f443ab9eb088d3150e2d4903cb40159;hpb=d0accf7044000e7f20e8bd586c3c48ce8c8cef7d diff --git a/src/main/java/de/juplo/facebook/exceptions/GraphApiException.java b/src/main/java/de/juplo/facebook/exceptions/GraphApiException.java index 1055b20..bd02565 100644 --- a/src/main/java/de/juplo/facebook/exceptions/GraphApiException.java +++ b/src/main/java/de/juplo/facebook/exceptions/GraphApiException.java @@ -3,6 +3,14 @@ package de.juplo.facebook.exceptions; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonPropertyOrder; import com.fasterxml.jackson.annotation.JsonRootName; +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.IOException; +import java.io.InputStream; import org.springframework.security.oauth2.common.exceptions.OAuth2Exception; /** @@ -10,64 +18,126 @@ import org.springframework.security.oauth2.common.exceptions.OAuth2Exception; * * @author Kai Moritz */ -@org.codehaus.jackson.map.annotate.JsonDeserialize(using = GraphApiExceptionJackson1Deserializer.class) -@com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = GraphApiExceptionJackson2Deserializer.class) public class GraphApiException extends OAuth2Exception { - private final String type; - private final int code; + final static ObjectMapper OBJECT_MAPPER; - private int httpErrorCode; + private final FacebookErrorMessage error; - public GraphApiException(String message, String type, int code) + static { - super(message); - this.type = type; - this.code = code; + OBJECT_MAPPER = new ObjectMapper(); + OBJECT_MAPPER.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true); + OBJECT_MAPPER.configure(DeserializationFeature.ACCEPT_FLOAT_AS_INT, false); + OBJECT_MAPPER.configure(SerializationFeature.WRAP_ROOT_VALUE, true); + } + + + public static GraphApiException create(InputStream in) + throws + IOException, + JsonParseException, + JsonMappingException + { + return create(OBJECT_MAPPER.readValue(in, FacebookErrorMessage.class)); + } + + public static GraphApiException create(byte[] message) + throws + IOException, + JsonParseException, + JsonMappingException + { + return create(OBJECT_MAPPER.readValue(message, FacebookErrorMessage.class)); + } + + public static GraphApiException create(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 21: return new PageMigratedException(error); + // 100..199: graph method errors + case 100: return new UnsupportedGetRequestException(error); + // 200..299: permission errors + // 300..399: data editing errors + // 400..449: authentication error + // 450..499: session errors + // 500..599: application messaging errors + // 600..699: FQL errors + case 613: return new RateExceededException(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 + + default: return new UnmappedErrorException(error); + } + } + + + protected GraphApiException(FacebookErrorMessage error) + { + super(error.message); + this.error = error; } public String getType() { - return type; + return error.type; } - public int getCode() + public Integer getCode() { - return code; + return error.code; } - @Override - public int getHttpErrorCode() + public Integer getSubCode() { - return httpErrorCode == 0 ? super.getHttpErrorCode() : httpErrorCode; + return error.subCode; } - public void setHttpErrorCode(int httpErrorCode) + public String getUserTitle() { - this.httpErrorCode = httpErrorCode; + return error.userTitle; } + public String getUserMessage() + { + return error.userMessage; + } + + public String getTraceId() + { + return error.traceId; + } + + @Override public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("{error:{\"message\":\""); - builder.append(getMessage().replaceAll("\"", "\\\"")); - builder.append("\",\"type\":"); - builder.append(type.replaceAll("\"", "\\\"")); - builder.append("\",\"code\":"); - builder.append(code); - builder.append("}}"); - return builder.toString(); + try + { + return OBJECT_MAPPER.writeValueAsString(error); + } + catch(JsonProcessingException e) + { + return "Could not convert error in JSON-representation: " + e.getMessage(); + } } /** * This class represents an error message from the Graph-API * - * @see https://developers.facebook.com/docs/graph-api/using-graph-api/v2.5#errors + * @see Graph-API Documentation */ @JsonRootName("error") @JsonPropertyOrder({ "message", "type", "code", "error_subcode", "error_user_title", "error_user_msg", "fbtrace_id" })