Voy
Voy

Reputation: 6314

How to create a new DatabaseError instance in Firebase?

Is there a way to generate a new instance of DatabaseError object?

Edit:

I'm using a class implementing the ValueEventListener in which I'd like to catch an exception and route the error through onCancelled(DatabaseError databaseError) method.

public class StandardSingleValueListener<T> implements ValueEventListener {

    protected Class<T> mTypeClass;

    public StandardSingleValueListener(Class<T> typeClass){
        mTypeClass = typeClass;
    }
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        T result = null;
        try {
            result = parseMethod(dataSnapshot);
        } catch (Exception e) {
            onCancelled(new DatabaseError(e.toString()); // I'd like to create the error here
            return;
        }
        // process result
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        // process databaseError
    }

    protected T parseMethod(DataSnapshot snap){
        return snap.getValue(mTypeClass);
    }
}

Where parseMethod is sometimes overridden and mTypeClass is the class of T.

Upvotes: 0

Views: 440

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599766

You can create a DatabaseError from the exception with:

DatabaseError.fromException(e);

But I'm not sure it's a good idea here. The error you're signaling is an application-level error. Raising it as a DatabaseError is bound to lead to confusion to those who maintain the code after you.

Upvotes: 2

Related Questions