Search in sources :

Example 6 with AppException

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

the class NetworkRequest method onError.

/**
 * Request failed. Will be called by the ObjectStore completion handler.
 */
// Called by JNI
@SuppressWarnings("unused")
@Override
public void onError(String nativeErrorCategory, int nativeErrorCode, String errorMessage) {
    ErrorCode code = ErrorCode.fromNativeError(nativeErrorCategory, nativeErrorCode);
    if (code == ErrorCode.UNKNOWN) {
        // In case of UNKNOWN errors parse as much error information on as possible.
        String detailedErrorMessage = String.format("{%s::%s} %s", nativeErrorCategory, nativeErrorCode, errorMessage);
        error.set(new AppException(code, detailedErrorMessage));
    } else {
        error.set(new AppException(code, errorMessage));
    }
    latch.countDown();
}
Also used : AppException(io.realm.mongodb.AppException) ErrorCode(io.realm.mongodb.ErrorCode)

Example 7 with AppException

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

the class OkHttpNetworkTransport method sendStreamingRequest.

@Override
public OsJavaNetworkTransport.Response sendStreamingRequest(Request request) throws IOException, AppException {
    OkHttpClient client = getStreamClient();
    okhttp3.Request okRequest = createRequest(request.getMethod(), request.getUrl(), request.getHeaders(), request.getBody());
    Call call = client.newCall(okRequest);
    okhttp3.Response response = call.execute();
    if ((response.code() >= 300) || ((response.code() < 200) && (response.code() != 0))) {
        throw new AppException(ErrorCode.fromNativeError(ErrorCode.Type.HTTP, response.code()), response.message());
    }
    return Response.httpResponse(response.code(), parseHeaders(response.headers()), response.body().source());
}
Also used : Call(okhttp3.Call) AppException(io.realm.mongodb.AppException) OkHttpClient(okhttp3.OkHttpClient)

Example 8 with AppException

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

the class RealmEventStreamAsyncTaskImpl method get.

@Override
public synchronized void get(App.Callback<BaseChangeEvent<T>> callback) throws IllegalStateException {
    Util.checkNull(callback, "callback");
    if (thread != null) {
        throw new IllegalStateException("Resource already open");
    } else {
        thread = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    eventStream = executor.run();
                    while (true) {
                        BaseChangeEvent<T> nextEvent = eventStream.getNextEvent();
                        callback.onResult(App.Result.withResult(nextEvent));
                    }
                } catch (AppException exception) {
                    callback.onResult(App.Result.withError(exception));
                } catch (IOException exception) {
                    AppException appException = new AppException(ErrorCode.NETWORK_IO_EXCEPTION, exception);
                    callback.onResult(App.Result.withError(appException));
                }
            }
        }, String.format("RealmStreamTask|%s", name));
        thread.start();
    }
}
Also used : AppException(io.realm.mongodb.AppException) IOException(java.io.IOException)

Example 9 with AppException

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

the class ChangeEvent method fromBsonDocument.

/**
 * Deserializes a {@link BsonDocument} into an instance of change event.
 *
 * @param document the serialized document
 * @return the deserialized change event
 */
static <T> ChangeEvent<T> fromBsonDocument(final BsonDocument document, final Class<T> documentClass, CodecRegistry codecRegistry) {
    try {
        checkContainsKey(Fields.ID_FIELD, document, "document");
        checkContainsKey(Fields.OPERATION_TYPE_FIELD, document, "document");
        checkContainsKey(Fields.NS_FIELD, document, "document");
        checkContainsKey(Fields.DOCUMENT_KEY_FIELD, document, "document");
    } catch (IllegalArgumentException exception) {
        throw new AppException(ErrorCode.EVENT_DESERIALIZING, exception);
    }
    final BsonDocument nsDoc = document.getDocument(Fields.NS_FIELD);
    final UpdateDescription updateDescription;
    if (document.containsKey(Fields.UPDATE_DESCRIPTION_FIELD)) {
        updateDescription = UpdateDescription.fromBsonDocument(document.getDocument(Fields.UPDATE_DESCRIPTION_FIELD));
    } else {
        updateDescription = null;
    }
    final T fullDocument;
    if (document.containsKey(Fields.FULL_DOCUMENT_FIELD)) {
        final BsonValue fdVal = document.get(Fields.FULL_DOCUMENT_FIELD);
        if (fdVal.isDocument()) {
            fullDocument = codecRegistry.get(documentClass).decode(fdVal.asDocument().asBsonReader(), DecoderContext.builder().build());
        } else {
            fullDocument = null;
        }
    } else {
        fullDocument = null;
    }
    return new ChangeEvent<>(document.getDocument(Fields.ID_FIELD), fromRemote(document.getString(Fields.OPERATION_TYPE_FIELD).getValue()), fullDocument, new MongoNamespace(nsDoc.getString(Fields.NS_DB_FIELD).getValue(), nsDoc.getString(Fields.NS_COLL_FIELD).getValue()), document.getDocument(Fields.DOCUMENT_KEY_FIELD), updateDescription, document.getBoolean(Fields.WRITE_PENDING_FIELD, BsonBoolean.FALSE).getValue());
}
Also used : AppException(io.realm.mongodb.AppException) BsonDocument(org.bson.BsonDocument) INSERT(io.realm.mongodb.mongo.events.BaseChangeEvent.OperationType.INSERT) BaseChangeEvent(io.realm.mongodb.mongo.events.BaseChangeEvent) UpdateDescription(io.realm.mongodb.mongo.events.UpdateDescription) MongoNamespace(io.realm.mongodb.mongo.MongoNamespace) 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