Search in sources :

Example 1 with AppException

use of io.realm.mongodb.AppException in project realm-java by realm.

the class RealmResultTaskImpl method getAsync.

@Override
public void getAsync(App.Callback<T> callback) {
    Util.checkNull(callback, "callback");
    Util.checkLooperThread("RealmResultTaskImpl can only run on looper threads.");
    RealmNotifier handler = new AndroidRealmNotifier(null, new AndroidCapabilities());
    pendingTask = service.submit(new Runnable() {

        @Override
        public void run() {
            try {
                postSuccess(handler, executor.run(), callback);
            } catch (AppException e) {
                postError(handler, e, callback);
            } catch (Throwable e) {
                postError(handler, new AppException(ErrorCode.UNKNOWN, "Unexpected error", e), callback);
            }
        }
    });
}
Also used : AppException(io.realm.mongodb.AppException) AndroidRealmNotifier(io.realm.internal.android.AndroidRealmNotifier) RealmNotifier(io.realm.internal.RealmNotifier) AndroidRealmNotifier(io.realm.internal.android.AndroidRealmNotifier) AndroidCapabilities(io.realm.internal.android.AndroidCapabilities)

Example 2 with AppException

use of io.realm.mongodb.AppException in project realm-java by realm.

the class JniBsonProtocol method decode.

public static <T> T decode(String string, Decoder<T> decoder) {
    try {
        StringReader stringReader = new StringReader(string);
        JsonReader jsonReader = new JsonReader(stringReader);
        jsonReader.readStartDocument();
        jsonReader.readName(VALUE);
        T value = decoder.decode(jsonReader, DecoderContext.builder().build());
        jsonReader.readEndDocument();
        return value;
    } catch (CodecConfigurationException e) {
        // might be missing
        throw new AppException(ErrorCode.BSON_CODEC_NOT_FOUND, "Could not resolve decoder for end type" + string, e);
    } catch (Exception e) {
        throw new AppException(ErrorCode.BSON_DECODING, "Error decoding value " + string, e);
    }
}
Also used : AppException(io.realm.mongodb.AppException) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) StringReader(java.io.StringReader) JsonReader(org.bson.json.JsonReader) AppException(io.realm.mongodb.AppException) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException)

Example 3 with AppException

use of io.realm.mongodb.AppException in project realm-java by realm.

the class JniBsonProtocol method encode.

public static <T> String encode(T value, Encoder<T> encoder) {
    try {
        StringWriter stringWriter = new StringWriter();
        JsonWriter jsonWriter = new JsonWriter(stringWriter, writerSettings);
        jsonWriter.writeStartDocument();
        jsonWriter.writeName(VALUE);
        encoder.encode(jsonWriter, value, EncoderContext.builder().build());
        jsonWriter.writeEndDocument();
        return stringWriter.toString();
    } catch (CodecConfigurationException e) {
        // might be missing
        throw new AppException(ErrorCode.BSON_CODEC_NOT_FOUND, "Could not resolve encoder for end type", e);
    } catch (Exception e) {
        throw new AppException(ErrorCode.BSON_ENCODING, "Error encoding value", e);
    }
}
Also used : AppException(io.realm.mongodb.AppException) StringWriter(java.io.StringWriter) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) JsonWriter(org.bson.json.JsonWriter) AppException(io.realm.mongodb.AppException) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException)

Example 4 with AppException

use of io.realm.mongodb.AppException in project realm-java by realm.

the class SyncSession method notifySessionError.

// This callback will happen on the thread running the Sync Client.
void notifySessionError(String nativeErrorCategory, int nativeErrorCode, String errorMessage, String clientResetPathInfo) {
    if (errorHandler == null) {
        return;
    }
    ErrorCode errCode = ErrorCode.fromNativeError(nativeErrorCategory, nativeErrorCode);
    if (errCode == ErrorCode.CLIENT_RESET) {
        // errorMessage contains the path to the backed up file
        if (clientResetPathInfo == null) {
            throw new IllegalStateException("Missing Client Reset info.");
        }
        RealmConfiguration backupRealmConfiguration = configuration.forErrorRecovery(clientResetPathInfo);
        if (clientResetHandler instanceof ManuallyRecoverUnsyncedChangesStrategy) {
            ((ManuallyRecoverUnsyncedChangesStrategy) clientResetHandler).onClientReset(this, new ClientResetRequiredError(appNativePointer, errCode, errorMessage, configuration, backupRealmConfiguration));
        } else if (clientResetHandler instanceof DiscardUnsyncedChangesStrategy) {
            ((DiscardUnsyncedChangesStrategy) clientResetHandler).onError(this, new ClientResetRequiredError(appNativePointer, errCode, errorMessage, configuration, backupRealmConfiguration));
        }
    } else {
        AppException wrappedError;
        if (errCode == ErrorCode.UNKNOWN) {
            wrappedError = new AppException(nativeErrorCategory, nativeErrorCode, errorMessage);
        } else {
            wrappedError = new AppException(errCode, errorMessage);
        }
        errorHandler.onError(this, wrappedError);
    }
}
Also used : RealmConfiguration(io.realm.RealmConfiguration) AppException(io.realm.mongodb.AppException) ErrorCode(io.realm.mongodb.ErrorCode)

Example 5 with AppException

use of io.realm.mongodb.AppException in project realm-java by realm.

the class UpdateDescription method fromBsonDocument.

/**
 * Converts an update description BSON document from a MongoDB Change Event into an
 * UpdateDescription object.
 *
 * @param document the
 * @return the converted UpdateDescription
 */
public static UpdateDescription fromBsonDocument(final BsonDocument document) {
    try {
        checkContainsKey(Fields.UPDATED_FIELDS_FIELD, document, "document");
        checkContainsKey(Fields.REMOVED_FIELDS_FIELD, document, "document");
    } catch (IllegalArgumentException exception) {
        throw new AppException(ErrorCode.EVENT_DESERIALIZING, exception);
    }
    final BsonArray removedFieldsArr = document.getArray(Fields.REMOVED_FIELDS_FIELD);
    final Set<String> removedFields = new HashSet<>(removedFieldsArr.size());
    for (final BsonValue field : removedFieldsArr) {
        removedFields.add(field.asString().getValue());
    }
    return new UpdateDescription(document.getDocument(Fields.UPDATED_FIELDS_FIELD), removedFields);
}
Also used : AppException(io.realm.mongodb.AppException) BsonArray(org.bson.BsonArray) BsonString(org.bson.BsonString) HashSet(java.util.HashSet) BsonValue(org.bson.BsonValue)

Aggregations

AppException (io.realm.mongodb.AppException)9 ErrorCode (io.realm.mongodb.ErrorCode)2 BsonValue (org.bson.BsonValue)2 CodecConfigurationException (org.bson.codecs.configuration.CodecConfigurationException)2 RealmConfiguration (io.realm.RealmConfiguration)1 RealmNotifier (io.realm.internal.RealmNotifier)1 AndroidCapabilities (io.realm.internal.android.AndroidCapabilities)1 AndroidRealmNotifier (io.realm.internal.android.AndroidRealmNotifier)1 MongoNamespace (io.realm.mongodb.mongo.MongoNamespace)1 BaseChangeEvent (io.realm.mongodb.mongo.events.BaseChangeEvent)1 INSERT (io.realm.mongodb.mongo.events.BaseChangeEvent.OperationType.INSERT)1 UpdateDescription (io.realm.mongodb.mongo.events.UpdateDescription)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 HashSet (java.util.HashSet)1 Call (okhttp3.Call)1 OkHttpClient (okhttp3.OkHttpClient)1 BsonArray (org.bson.BsonArray)1 BsonDocument (org.bson.BsonDocument)1