-package de.juplo.facebook.exceptions;
-
-
-import com.fasterxml.jackson.core.JsonParser;
-import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.core.JsonToken;
-import com.fasterxml.jackson.databind.DeserializationContext;
-import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
-import java.io.IOException;
-import java.util.HashMap;
-import java.util.Map;
-
-
-/**
- * @author Kai Moritz
- *
- */
-public class GraphApiExceptionJackson2Deserializer
- extends
- StdDeserializer<GraphApiException>
-{
- public GraphApiExceptionJackson2Deserializer()
- {
- super(GraphApiException.class);
- }
-
- @Override
- public GraphApiException deserialize(
- JsonParser jp,
- DeserializationContext ctxt
- )
- throws
- IOException,
- JsonProcessingException
- {
- JsonToken t = jp.getCurrentToken();
- if (t != JsonToken.START_OBJECT)
- return null;
-
- t = jp.nextToken();
- if (t != JsonToken.FIELD_NAME)
- return null;
-
- if (!jp.getCurrentName().equals("error"))
- return null;
-
- t = jp.nextToken();
- if (t != JsonToken.START_OBJECT)
- return null;
-
- String message = null, type = null;
- Integer code = null;
-
- t = jp.nextToken();
- Map<String, String> map = new HashMap<>();
- for (; t == JsonToken.FIELD_NAME; t = jp.nextToken())
- {
- // Must point to field name
- String fieldName = jp.getCurrentName();
- // And then the value...
- t = jp.nextToken();
-
- switch (t)
- {
- case VALUE_STRING:
- switch(fieldName.toLowerCase())
- {
- case "message":
- message = jp.getText();
- break;
- case "type":
- type = jp.getText();
- break;
- default:
- return null;
- }
- break;
- case VALUE_NUMBER_INT:
- if (!fieldName.equalsIgnoreCase("code"))
- return null;
- code = jp.getValueAsInt();
- break;
- default:
- return null;
- }
- }
-
- if (message == null || type == null || code == null)
- return null;
-
- switch (code)
- {
- case 1: return new UnknownErrorException();
- case 2: return new UnexpectedErrorException();
- case 21: return new PageMigratedException(message);
- case 100: return new UnsupportedGetRequestException();
- case 613: return new RateExceededException();
- default: return new GraphApiException(message, type, code);
- }
- }
-}